2013-03-10 70 views
3

我已经建立了一个图片库和保存图像“原样”无需裁剪它。我想在加载到控制器中的同时实时调整图像大小,因此当我在浏览器中加载控制器时,它会根据我的需要显示图像的大小。我已将该方法添加到MY_Loader,这里是代码。调整图像大小和显示,但不保存

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 
} 

从这段代码中,我收到很多错误,包括标头错误。我究竟做错了什么?

错误:(如果有用的话)

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) 
+0

您好!你可以在这里粘贴错误信息吗? 问题可以是previousli发送了头,等... PHP代码之前 HTML代码..等等... – Kovge 2013-03-10 07:30:57

+0

补充一下上面 – 2013-03-10 07:35:50

+0

没有HTML正在装载IM刚刚从控制器测试调用它。 – 2013-03-10 07:49:29

回答

4

好的...所以... 拳头的事情是,你并不想保存图像,只显示 所以你应该imagejpeg只使用一个参数。

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; 
    } 

第一轮我推荐这个改变。请写信给我,有错误... 什么变化和推荐阅读:http://php.net/manual/en/function.imagecopyresized.php

这:

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

说我的东西是错误的例外PHP ... 请检查,是BOM字符在文件的开头......或者是空格,在PHP标签之前的换行符......一些代码编辑器将这些刺激字符放在php代码中...

和其他任何文件这个错误信息)应该像这样检查。 有时在php标签之前留下了一些字符...并且如果在webservice读取文件时关闭输出缓冲,则使用默认标题立即发送到输出。 (最近的html /文本标题)。 (如果要是HTML /文本发送其它头已经发送。例如一些头不能被发送,图像/ JPEG不能被发送)

,如果你不想工作,与此点燃。我不知道你的系统是怎样的。 我认为index.php是你的引导,改变它。

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

但更好的解决方案来检查您的php代码,并删除php标签之前和之后的行/字符。

+0

我得到的错误是消息:imagejpeg():当我关闭标题('')时,26不是有效的图像资源;因为标题说由于错误无法加载图像。 – 2013-03-10 08:01:44

+0

imagedestroy($ thumbImage);我忘了这个。在发送到输出之前不要销毁。 我改变了我评论中的代码。 – Kovge 2013-03-10 08:05:10

+0

tahnk你!它的工作,没有看到。我还发现了一个代码库,它可以实现它http://www.matmoo.com/digital-dribble/codeigniter/image_moo/ – 2013-03-10 10:15:01

相关问题