4

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)
Mike Rockétt
  • 8,947
  • 4
  • 45
  • 81
Exploit
  • 6,278
  • 19
  • 70
  • 103
  • Hello! Could you please paste here the error messages? Problems can be the previousli sent headers, etc ... html code before php code .. etc... – Kovge Mar 10 '13 at 07:30
  • there is no html being loaded im just calling it from the controller for testing. – Exploit Mar 10 '13 at 07:49

1 Answers1

5

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.

Kovge
  • 2,019
  • 1
  • 14
  • 13
  • the error i get is Message: imagejpeg(): 26 is not a valid Image resource when i turn off header(''); because header was saying could not load image due to errors. – Exploit Mar 10 '13 at 08:01
  • imagedestroy($thumbImage); ups, i forgot this. do not destroy before send it to output. I changed the code in my comment. – Kovge Mar 10 '13 at 08:05
  • tahnk you! it worked, didnt see that. i also found a codeigniter library that does it http://www.matmoo.com/digital-dribble/codeigniter/image_moo/ – Exploit Mar 10 '13 at 10:15
  • 1
    Yes, sometimes better to search a good tool, than write the same. But for lerning this is a good thing, that we write again codes. And when we use a tool , we know what can be inside. Or if the tool does not work good, we can fix it. – Kovge Mar 11 '13 at 20:14