0

This is my controller:

 public function actionFind($id, $c, $t, $width = false, $height = false)
      {

        $image = \app\models\Picture::finding($id, $c, $t, $width, $height);


        \Yii::$app->response->format = yii\web\Response::FORMAT_RAW;
        \Yii::$app->response->headers->add('content-type', 'image/jpeg');
        \Yii::$app->response->data = file_get_contents($image);
        return \Yii::$app->response;

        }

This is my model:

public static function finding($id, $c, $t, $width = false, $height = false)
        {

        $query = new Query;
        // compose the query
        $query->select('*')
                ->from($t)
                ->where("Active=1 AND Code = $id")
                ->limit(1);
        $query->orderBy('Code desc');
        // build and execute the query
        $rows = $query->all();
        // alternatively, you can create DB command and execute it
        $command = $query->createCommand();
        // $command->sql returns the actual SQL
        $rows = $command->queryAll();

        if (isset($rows[0]["$c"]))
            {

            return $image = "data:image/jpeg;base64," . base64_encode($rows[0]["$c"]);
            }

        }

and this is my error:

cannot be displayed because it contains an error

my image save in database!!

Picture : longblob

in another project with database this code is run correct:

header("Content-Type: image/jpeg");
if (strlen($image) == 0 || $id == "")
    {
    if (!$w)
        $w = $w1;
    if (!$h)
        $h = $h1;
    if (!$w)
        $w = 120;
    if (!$h)
        $h = 140;
    $dst = imagecreatetruecolor($w, $h);

    $gray = imagecolorallocate($dst, 255, 255, 255);
    imagefill($dst, 1, 1, $gray);
    if ($q)
        imagejpeg($dst, NULL, $q);
    else
        imagejpeg($dst);
    }
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
Saltern
  • 1,305
  • 2
  • 16
  • 42
  • Have you checked that the data in your database is correctly encoded? – Pablo Flores Jul 02 '17 at 05:58
  • that image is correctly load with another source! – Saltern Jul 02 '17 at 06:56
  • I've tried the display image code (lines 8-11) with a 64 base encoded image (from here https://stackoverflow.com/a/8499716/4648184) and it displays it correctly, so I asume the problem is the image data. Chek if your model is not getting an empty string in $rows[0]["$c"] (i checked an i've got the same error when the content is empty) – Pablo Flores Jul 02 '17 at 18:03
  • and you header is image/jpeg?? because I do not want display with echo this: – Saltern Jul 03 '17 at 06:02
  • \Yii::$app->response->format = yii\web\Response::FORMAT_RAW; \Yii::$app->response->headers->add('content-type','image/png'); \Yii::$app->response->data = file_get_contents('file.png'); return \Yii::$app->response; – Saltern Jul 03 '17 at 06:18
  • i want use this but my image saved in database ! – Saltern Jul 03 '17 at 06:19

1 Answers1

0

As I mentioned in the comments, I used your code to display an image, here is an example:

/**
 * Displays homepage.
 *
 * @return string
 */
public function actionIndex()
{
        \Yii::$app->response->format = yii\web\Response::FORMAT_RAW;
        \Yii::$app->response->headers->add('content-type', 'image/png');
        \Yii::$app->response->data = file_get_contents("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4
    9TXL0Y4OHwAAAABJRU5ErkJggg==");
        return \Yii::$app->response;
}

Eventhough the data is saved in the database, when you retrieve it you just encode it in a 64-base string, which is something similar to the hardcoded string I used. Again, check your data.

Note: I also used the image/jpeg header with the png image and it worked, not recommended though

Pablo Flores
  • 1,350
  • 13
  • 15
  • lets try something else, could you just remove the \Yii::$app->response->format = yii\web\Response::FORMAT_RAW; to see if yii is outputting something to your page like an exception or something that can give us a hint what's going on? – Pablo Flores Jul 05 '17 at 04:08
  • result has image (with charachter) such as -> ����JFIFdd��Ducky<��Adobed���� �������!1AQ"aq2����BR��br#3.... – Saltern Jul 09 '17 at 07:35
  • I thought that an exception was being raised and corrupting your image data, but if it just displays the data as text, then certainly is a problem with the image – Pablo Flores Jul 09 '17 at 16:25
  • \Yii::$app->response->headers->add('content-type', 'image/png'); \Yii::$app->response->data = file_get_contents("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA AAAFCAYAAACNbyblAAAAHElEQVQI12P4 9TXL0Y4OHwAAAABJRU5ErkJggg=="); return \Yii::$app->response; – Saltern Jul 10 '17 at 04:37
  • result for this code is : �PNG IHDR�o&�IDAT�c��5�юIEND�B`� – Saltern Jul 10 '17 at 04:37