我建立了一个图片库,并按原样保存了图片,但没有对其进行裁剪.我想在将图像加载到控制器中时即时调整图像的大小,因此当我在浏览器中加载控制器时,它会显示图像调整大小后的大小.我已将方法添加到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)
解决方法:
好吧…
第一件事是,您不想保存图像,只显示
因此,应仅将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标记之前还有一些字符…,并且如果在Web服务读取文件时关闭了输出缓冲,则使用默认标头将其发送到输出.
(最近的html / text标头). (如果已经发送了其他标头,则无法发送某些标头.例如,如果发送了html / text,则无法发送image / jpeg)
如果您不想为此工作.我不知道您的系统是什么样的.
我认为index.php是您的引导程序,请对其进行更改.
<?php
ob_start();
....
ob_end_flush();
?>
但是更好的解决方案是检查您的php代码,并删除php标记前后的行/字符.