使用php将图像批量文件夹转换为灰度

发布于 2024-08-25 10:27:22 字数 1024 浏览 1 评论 0原文

谁能发现我在这里出错的地方。它应该在一个文件夹中创建图像的灰度副本并将其保存在另一个文件夹中。这可能与我引用文件位置的方式有关吗?两个文件夹的文件夹权限均为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是伱的 2024-09-01 10:27:22

检查 imagejpeg 调用的结果。将您的代码更改为:

$result = imagejpeg($canvas, $outputfile, 95);
if($result)
{
    echo "Converted ".$targetfile." to grayscale as ".$outputfile." ".$width."x".$height."<br/>";
}
else
{
    echo "Could not save to $outputfile.<br>"
    if(!is_writable($outputfile)
        echo "The path was not writable";
}
imagedestroy($sourceimage);
imagedestroy($canvas);

这将帮助我们了解发生了什么。如果出现“路径不可写”,请尝试使用绝对路径而不是相对路径,例如:

grayscalecopy($serverfiles[$i], dirname(__FILE__)."/artworkimages/thumbs/grays/".$serverfilesshort);

Check for the result of the imagejpeg call. Change your code to:

$result = imagejpeg($canvas, $outputfile, 95);
if($result)
{
    echo "Converted ".$targetfile." to grayscale as ".$outputfile." ".$width."x".$height."<br/>";
}
else
{
    echo "Could not save to $outputfile.<br>"
    if(!is_writable($outputfile)
        echo "The path was not writable";
}
imagedestroy($sourceimage);
imagedestroy($canvas);

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:

grayscalecopy($serverfiles[$i], dirname(__FILE__)."/artworkimages/thumbs/grays/".$serverfilesshort);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文