2010-03-12 26 views
0

任何人都可以在這裏找到我要去的地方。它應該在一個文件夾中創建圖像的灰度副本並將其保存在另一個文件夾中。這可能與我引用文件位置的方式有關。兩個文件夾上的文件夾權限均爲777.腳本運行時沒有可見的錯誤,但沒有創建圖像。使用php將圖像批處理文件夾變成灰度

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

您看到的具體問題是什麼?請編輯您的答案並描述問題。 – Josh 2010-03-12 14:35:30

+0

腳本運行時沒有可見的錯誤,但沒有創建圖像。 – James 2010-03-12 14:38:40

+0

您是否在輸出中看到許多「將xxx轉換爲灰度爲yyy 123x456」行? – Josh 2010-03-12 15:59:24

回答

1

檢查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); 
+0

絕對路徑似乎解決了這個問題。非常感謝josh – James 2010-03-12 18:11:45

+0

很高興我能幫忙! – Josh 2010-03-12 19:25:25