Resize image and display without saving it

五迷三道 提交于 2019-12-01 00:04:41

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!