【发布时间】:2011-02-25 18:33:36
【问题描述】:
我想出了这个:
<?php
$dir = $_GET['dir'];
header('Content-type: image/jpeg');
$create = imagecreatetruecolor(150, 150);
$img = imagecreatefromjpeg($dir);
imagecopyresampled($create, $img, 0, 0, 0, 0, 150, 150, 150, 150);
imagejpeg($create, null, 100);
?>
它通过访问来工作:
效果很好......但输出很糟糕:
有人可以将我的代码修复为覆盖黑色区域的 150 x 150 图像吗...
谢谢。
解决方案:
<?php
$dir = $_GET['dir'];
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($dir);
$create = imagecreatetruecolor(150, 150);
$img = imagecreatefromjpeg($dir);
$newwidth = 150;
$newheight = 150;
imagecopyresized($create, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($create, null, 100);
?>
【问题讨论】:
-
您也可以使用 .htaccess 来重写 URL 并具有如下路径:domain.com/imagephp/thisistheimage.jpg
-
您应该使用文件名中的宽度/高度参数保存调整大小图像的副本,并在处理之前检查它是否存在并提供该副本,以便降低服务器上的负载。还要在 imagejpeg() 之后使用 imagedestroy($create) 来释放内存。
标签: php image-processing image-manipulation on-the-fly