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) –