需要将调整大小的缩略图居中 [英] Need to center a resized thumbnail
本文介绍了需要将调整大小的缩略图居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码用于根据用户上传的内容生成缩略图. 它制作缩略图,保持宽高比并添加白色背景. 但是它将其对齐到左上方. 我需要水平和垂直居中.
I have the following code for generating thumbnails from a user upload. It makes the thumbnail, keeps aspect ratio and adds a white background. But it aligns it to the top left. I need to center it, both horizontally and vertically.
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));
推荐答案
我制作了一个用于PHP GD图像编辑的函数
PHP-GD-Imagestyle
您可以使用自动调整大小样式创建居中的缩略图.
I made a function for PHP GD image editing
PHP-GD-Imagestyle
You can create centered thumbnails by using the autosize style.
$thumb = imagestyle($image,'autosize:250 250');
这篇关于需要将调整大小的缩略图居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文