使用php将图像批量文件夹转换为灰度
谁能发现我在这里出错的地方。它应该在一个文件夹中创建图像的灰度副本并将其保存在另一个文件夹中。这可能与我引用文件位置的方式有关吗?两个文件夹的文件夹权限均为 777。脚本正在运行,没有明显错误,但没有创建图像。
function grayscalecopy($targetfile, $outputfile){
$size = GetImageSize($targetfile);
$width = $size[1];
$height = $size[0];
$canvas = imagecreatetruecolor($width, $height);
$sourceimage = imagecreatefromjpeg($targetfile);
imagefilter($sourceimage, IMG_FILTER_GRAYSCALE);
imagecopy($canvas, $sourceimage, 0, 0, 0, 0, $width, $height);
imagejpeg($canvas, $outputfile, 95);
imagedestroy($sourceimage);
imagedestroy($canvas);
echo "Converted ".$targetfile." to grayscale as ".$outputfile." ".$width."x".$height."<br/>";
}
$serverfiles = glob("artworkimages/thumbs/*.*");
//$numbertocache = count($serverfiles);
$numbertocache = 10;
for ($i=0; $i<$numbertocache; $i++)
{
$serverfilesshort=explode("/",$serverfiles[$i]);
$serverfilesshort=$serverfilesshort[count($serverfilesshort)-1];
grayscalecopy($serverfiles[$i], "artworkimages/thumbs/grays/".$serverfilesshort);
}
Can anyone spot where I'm going wrong here. It should be creating grayscale copies of images in one folder and saving them in another folder. Could it be to do with the way im referencing the file locations. Folder permissions on both folders are 777. Script is running without visible error but no images are being created.
function grayscalecopy($targetfile, $outputfile){
$size = GetImageSize($targetfile);
$width = $size[1];
$height = $size[0];
$canvas = imagecreatetruecolor($width, $height);
$sourceimage = imagecreatefromjpeg($targetfile);
imagefilter($sourceimage, IMG_FILTER_GRAYSCALE);
imagecopy($canvas, $sourceimage, 0, 0, 0, 0, $width, $height);
imagejpeg($canvas, $outputfile, 95);
imagedestroy($sourceimage);
imagedestroy($canvas);
echo "Converted ".$targetfile." to grayscale as ".$outputfile." ".$width."x".$height."<br/>";
}
$serverfiles = glob("artworkimages/thumbs/*.*");
//$numbertocache = count($serverfiles);
$numbertocache = 10;
for ($i=0; $i<$numbertocache; $i++)
{
$serverfilesshort=explode("/",$serverfiles[$i]);
$serverfilesshort=$serverfilesshort[count($serverfilesshort)-1];
grayscalecopy($serverfiles[$i], "artworkimages/thumbs/grays/".$serverfilesshort);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查
imagejpeg
调用的结果。将您的代码更改为:这将帮助我们了解发生了什么。如果出现“路径不可写”,请尝试使用绝对路径而不是相对路径,例如:
Check for the result of the
imagejpeg
call. Change your code to:This will help us see what's going on. If you get "The path was not writable", try using absolute paths instead of relative paths, for example: