真让我大吃一惊,我已经研究了2天了.
目标?单击/选择一个包含图像的子目录;在Submit上,批处理将在选定的整个DIR上使用GD运行,并在同一服务器的/ thumbs文件夹中创建缩略图.
状态?我可以一次为一个文件执行此操作,需要一次执行多个文件.
这是我正常运行的一次性代码:
$filename = "images/r13.jpg";
list($width,$height) = getimagesize($filename);
$width_ratio = 166 / $width;
if ($height * $width_ratio <= 103)
{
$adjusted_width = 166;
$adjusted_height = $height * $width_ratio;
}
else
{
$height_ratio = 103 / $height;
$adjusted_width = $width * $height_ratio;
$adjusted_height = 103;
}
$image_p = imagecreatetruecolor(166,103);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p,$image,ceil((166 - $adjusted_width) / 2),ceil((103 - $adjusted_height) / 2),0,0,ceil($adjusted_width),ceil($adjusted_height),$width,$height);
imagejpeg($image_p,"images/thumbs/r13.jpg",70);
如您所见,该脚本针对的是单个文件,我想遍历目录而不是指定名称.
(我还将研究imagemagick,但目前这不是一个选择.)
我将继续进行SO等操作,但是任何帮助都是巨大的.
谢谢.
解决方法:
您需要通过以下代码创建函数:
function processImage($filename){
list($width,$height) = getimagesize($filename);
$width_ratio = 166 / $width;
if ($height * $width_ratio <= 103)
{
$adjusted_width = 166;
$adjusted_height = $height * $width_ratio;
}
else
{
$height_ratio = 103 / $height;
$adjusted_width = $width * $height_ratio;
$adjusted_height = 103;
}
$image_p = imagecreatetruecolor(166,103);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p,$image,ceil((166 - $adjusted_width) / 2),ceil((103 - $adjusted_height) / 2),0,0,ceil($adjusted_width),ceil($adjusted_height),$width,$height);
imagejpeg($image_p,"images/thumbs/".basename($filename),70);
imagedestroy($image_p);
}
请注意此函数的最后两行:它基于传递的文件名写入thumb,并破坏资源以释放内存.
现在将其应用于目录中的所有文件:
foreach(glob('images/*.jpg') AS $filename){
processImage($filename);
}
基本上就是这样.