Cannot Redirect After Resizing Image

I’m using Yii2 and I’m new on the framework but I’ve never had a problem that wasn’t solved here before, until now.

I’m working in Ubuntu Mate and running it through XAMPP’s apache.

The form I have requires saving an image and, to save space in the DB, which is mysql, I resize the image before saving it as I found it here.

The record saves just fine but during the process instead of redirecting me where its supposed to it shows an echo of the blob in text.

The code in question is




...

    public function beforeSave($insert)

    {


        if ($file = UploadedFile::getInstance($this, 'foto')) {

            //$this->foto = file_get_contents($file->tempName); //old way of saving without compressing


            $src_img= imagecreatefromstring(file_get_contents($file->tempName));

            $dst_img = imagecreatetruecolor(413, 531);

            //resize  to 413 x 531 pixels

            header('content-type:image/JPEG'); //using this I can show the user an image instead of pure BLOB but I still have no control over the redirect


            imagecopyresampled($dst_img, $src_img, 0,0,0,0, 413,531, imagesx($src_img), imagesy($src_img));

            imagejpeg($dst_img);


            ob_start();

            imagejpeg($dst_img);

            $image_string = ob_get_contents();

            ob_end_flush();

            //end resize image


            $this->foto=$image_string;


        }

...



I have tried redirecting through the controller but it doesn’t change anything. Somehow the redirection happens after saving but before the controller tries to redirect.

The question is what is causing this and how can I control where its redirecting it.

Sorry but is not very clear to me your scenario…

  1. you have an upload form I guess

  2. the controller load the form in the model

  3. but you load the image in the model from the model (instead of the controller)

  4. after saving you want to redirect to another page (I guess the redirect is in the controller)

If this is the scenario at first your problem is the function imagejpeg, from php manual

imagejpeg — Output image to browser or file

This function send the content back to the browser (if a file path is not specified) therefore is impossible to do a redirect afterwards. Once you output content to the browser a redirect is not allowed. If you still want to use this function you should buffer the output and discard the buffer after reading it.

Eventually to manage image resizing use an extension, there are several on packagist, just search for "yii2 image"

Secondly I strongly suggest you to change your code. You putted control logic in the model.

  1. move the image load in the controller

  2. If you need to resize every image before saving do this as a filter or validation rule. If you put it in beforeSave any error that happen there can’t be managed and sent back to the user.

If the user send you an invalid file, like a txt (even if you check the extension maybe a malicious user can change .txt to .jpg) the resize goes in error breaking the application. If you load the binary in the controller and then you validate the model you can still trow an error to the user.

Thanks you, by cleaning the buffer it works as intended now, I will however find better ways to resize this images, preferably in the client’s side