3
我有以下用於從用戶上傳中生成縮略圖的代碼。 它使縮略圖,保持寬高比和增加白色背景。 但它將其與左上方對齊。 我需要居中對齊,水平和垂直。需要居中縮放的縮略圖
function makethumbnail($thumbw,$thumbh,$thumbName,$sourceName){
$ext=getExtension($sourceName);
//echo $ext;
$sourcePath = 'images/logos/deals/'; // Path of original image
$sourceUrl = 'http://www.malldeals.com/admin/convert/';
$thumbPath = $sourcePath; // Writeable thumb path
$thumbUrl = $sourceUrl . $thumbPath ;
$thumbHeight=0;
$thumbWidth=0;
// Beyond this point is simply code.
if(!strcmp("png",$ext))
$sourceImage = imagecreatefrompng("$sourcePath/$sourceName");
else if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
$sourceImage = imagecreatefromjpeg("$sourcePath/$sourceName");
else if(!strcmp("gif",$ext))
$sourceImage = imagecreatefromgif("$sourcePath/$sourceName");
global $sourceWidth, $sourceHeight;
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);
$ratio1=$sourceWidth/$thumbw;
$ratio2=$sourceHeight/$thumbh;
if($ratio1>$ratio2) {
$thumbWidth=$thumbw;
$thumbHeight=$sourceHeight/$ratio1;
}
else {
$thumbHeight=$thumbh;
$thumbWidth=$sourceWidth/$ratio2;
}
$targetImage = imagecreatetruecolor($thumbw,$thumbh);
// get the color white
$color = imagecolorallocate($targetImage, 255, 255, 255);
// fill entire image
imagefill($targetImage, 0, 0, $color);
imagecopyresampled($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbHeight,imagesx($sourceImage),imagesy($sourceImage));
這似乎並不奏效。請注意,我創建了兩個縮略圖,一個是180x60,另一個是50x50。上面的代碼產生的結果是: original:(http://malldeals.com/admin/images/logos/1321988719ymca-ywca.jpg) thumbnail1:(http://malldeals.com/admin/images/logos /big1321988719ymca-ywca.jpg) thumbnail2:(http://malldeals.com/admin/images/logos/small1321988719ymca-ywca.jpg) –