开发者

Batch folder of images into grayscale using php

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);
}


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜