Php 调整图像大小并在不保存的情况下显示
我已经建立了一个图像库,并保存了图像“原样”,而没有裁剪它。我想在控制器中加载图像时动态调整图像大小,因此当我在浏览器中加载控制器时,它会显示重新调整为我想要的大小的图像。我已将该方法添加到我的\u加载程序中,下面是代码Php 调整图像大小并在不保存的情况下显示,php,codeigniter,gd,Php,Codeigniter,Gd,我已经建立了一个图像库,并保存了图像“原样”,而没有裁剪它。我想在控制器中加载图像时动态调整图像大小,因此当我在浏览器中加载控制器时,它会显示重新调整为我想要的大小的图像。我已将该方法添加到我的\u加载程序中,下面是代码 function show_image($image, $width, $height) { $this->helper('file'); $image_content = read_file($image); //resize image
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
}
从这段代码中,我得到了许多错误,包括标题错误。我做错了什么
错误:(如果有用)
好的。。。所以
首先,您不想保存图像,只需显示
因此,您应该只使用一个参数的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;
}
首先,我推荐这种改变。请写信给我,有什么变化与错误。。。
并建议阅读:
这是:
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读取文件时关闭了输出bufering,则使用默认头以Medietly方式发送到输出。
(最近的html/文本标题)。(如果已发送其他标题,则无法发送某些标题。例如,如果已发送html/文本,则无法发送图像/jpeg)
如果您不想使用此工具进行照明。我不知道你的系统是什么样子。
我假设index.php是您的引导程序,请更改它
<?php
ob_start();
....
ob_end_flush();
?>
但更好的解决方案是检查php代码,删除php标记前后的行/字符。Hello!你能把错误信息贴在这里吗?问题可能是以前发送的标题等。。。php代码之前的html代码。。等等…没有加载html,我只是从控制器调用它进行测试。我得到的错误是消息:imagejpeg():26在我关闭标头(“”)时不是有效的图像资源;因为标头说由于错误而无法加载图像。imagedestroy($thumbImage);ups,我忘了这个。在将其发送到输出之前不要销毁。我在评论中更改了代码。谢谢!它起作用了,我没看到。我还发现了一个codeigniter库,它可以做到这一点是的,有时候搜索一个好的工具比编写同样的工具要好。但对于勒宁来说,这是一件好事,我们再次编写代码。当我们使用工具时,我们知道里面可能有什么。或者,如果工具工作不好,我们可以修复它。
<?php
ob_start();
....
ob_end_flush();
?>