Exploit
Exploit

Reputation: 6386

Resize image and display without saving it

I've built an image gallery and saved the image "as is" without cropping it. I want to resize the image on the fly while it's loaded in the controller, so when I load the controller in the browser it displays the image re-sized to whatever I want. I've added the method to MY_Loader, here is the code.

function show_image($image, $width, $height) {
    $this->helper('file');
    $image_content = read_file($image);

    //resize image
    $image = imagecreatefromjpeg($image);
    $thumbImage = imagecreatetruecolor(50, 50);
    imagecopyresized($thumbImage, $image, 0, 0, 0, 0, 50, 50, $width, $height);
    imagejpeg($thumbImage,"",85);
    imagedestroy($image);
    imagedestroy($thumbImage);

    header('Content-Length: '.strlen($image_content)); // sends filesize header
    header('Content-Type: '. get_mime_by_extension($image)); // send mime-type header
    header('Content-Disposition: inline; filename="'.basename($image).'";'); // sends filename header
    exit($image_content); // reads and outputs the file onto the output buffer
}

From this code, I'm getting many errors including header errors. What am I doing wrong?

Errors: (if useful)

Message: imagejpeg(): Filename cannot be empty

Message: Cannot modify header information - headers already sent by (output started at /Volumes/www/vhosts/ci/system/core/Exceptions.php:185)

Message: strrchr() expects parameter 1 to be string, resource given

Message: Cannot modify header information - headers already sent by (output started at /Volumes/www/vhosts/ci/system/core/Exceptions.php:185)

Message: basename() expects parameter 1 to be string, resource given

Message: Cannot modify header information - headers already sent by (output started at /Volumes/www/vhosts/ci/system/core/Exceptions.php:185)

Upvotes: 4

Views: 14002

Answers (1)

Kovge
Kovge

Reputation: 2019

Ok... so... the fist thing is, that you does not want to save image, just display so you should use imagejpeg with only one parameter.

   function show_image($image, $width, $height) {
        //$this->helper('file');                   why need this?
        //$image_content = read_file($image);      We does not want to use this as output.

        //resize image           
        $image = imagecreatefromjpeg($image);
        $thumbImage = imagecreatetruecolor(50, 50);
        imagecopyresized($thumbImage, $image, 0, 0, 0, 0, 50, 50, $width, $height);
        imagedestroy($image);
        //imagedestroy($thumbImage); do not destroy before display :)
        ob_end_clean();  // clean the output buffer ... if turned on.
        header('Content-Type: image/jpeg');  
        imagejpeg($thumbImage); //you does not want to save.. just display
        imagedestroy($thumbImage); //but not needed, cause the script exit in next line and free the used memory
        exit;
  }

fist round i recommend this changes. Please write me, what changes with errors... And recommend to read: http://php.net/manual/en/function.imagecopyresized.php

And this :

       Message: Cannot modify header information - headers already sent by (output started at /Volumes/www/vhosts/ci/system/core/Exceptions.php:185)

Says me that something is wrong with the exception php... please check, is BOM character at the begining of the file... or is spaces, newlines after .. or before the php tag.. some code editor puts these irritatign characters in php code...

And any other files (that appears this error message) should be checked like this. Sometimes some characters left before the php tag... and if output bufering is turned off as the webservice reads the file, send to the output inmedietly with the default header. ( recently html/text header ) . ( some headers can't be sent if other header sent already.. for example if html/text sent, image/jpeg cannot be sent )

if you do not want to work a lit with this. I doesnt know how your system looks like. I assume that index.php is your bootstrap, change it.

   <?php
       ob_start();
       .... 
       ob_end_flush();
   ?>

But better solution to examine your php codes, and delete lines / characters before and after php tag.

Upvotes: 5

Related Questions