首页 / PHP / php-目录中的GD图像批处理
php-目录中的GD图像批处理
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-目录中的GD图像批处理,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1884字,纯文字阅读大概需要3分钟。
内容图文
![php-目录中的GD图像批处理](/upload/InfoBanner/zyjiaocheng/676/ab99fb7dad694472a4e620a774d0da0d.jpg)
真让我大吃一惊,我已经研究了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);
}
基本上就是这样.
内容总结
以上是互联网集市为您收集整理的php-目录中的GD图像批处理全部内容,希望文章能够帮你解决php-目录中的GD图像批处理所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。