一些使用考慮,你想使一個縮略圖並保存它,你可以實現方便的調整大小功能,如下所示:
<?php
function resize($sourcefile, $endfile, $thumbwidth, $thumbheight, $quality){
$ext1 = explode(".",trim($sourcefile));
$ext = strtolower(trim(array_slice($sext1,-1)));
switch($ext):
case 'jpg' or 'jpeg':
$img = imagecreatefromjpeg($sourcefile);
break;
case 'gif':
$img = imagecreatefromgif($sourcefile);
break;
case 'png':
$img = imagecreatefrompng($sourcefile);
break;
endswitch;
$width = imagesx($img);
$height = imagesy($img);
if ($width > $height) {
$newwidth = $thumbwidth;
$divisor = $width/$thumbwidth;
$newheight = floor($height/$divisor);
}
else {
$newheight = $thumbheight;
$divisor = $height/$thumbheight;
$newwidth = floor($width/$divisor);
}
// Create a new temporary image.
$tmpimg = imagecreatetruecolor($newwidth, $newheight);
// Copy and resize old image into new image.
imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Save thumbnail into a file.
switch($ext):
case 'jpg' or 'jpeg':
$makeimg = imagejpeg($tmpimg, $endfile, $quality);
break;
case 'gif':
$makeimg = imagegif($tmpimg, $endfile, $quality);
break;
case 'png':
$makeimg = imagepng($tmpimg, $endfile, $quality);
break;
endswitch;
// release the memory
imagedestroy($tmpimg);
imagedestroy($img);
if($makeimg){
chmod($endfile,0755);
return true;
}else{
return false;
}
}
?>
然後,複製完t後他要的文件在我的答案高於此使用我的方法之一,你的服務器,你可以簡單地套用功能如下:
$doresize = resize($sourcefile, $endfile, $thumbwidth, $thumbheight, $quality);
echo ($doresize == true ? "IT WORKED" : "IT FAILED");
此功能提供了我很好。我每天將它應用於1000張圖像,並且它像魅力一樣運作。
是否有權限設置此參數INI?這個警告是否會導致腳本終止? – SimonDowdles 2011-04-08 08:38:23
順便說一句,你只是想顯示這個圖像,或者你真的想將它複製到你的服務器? – SimonDowdles 2011-04-08 08:39:09
我沒有收到有關INI文件設置的任何警告,它仍然是gdcreate函數的致命錯誤。 – Dave 2011-04-08 08:52:23