У меня есть следующий код для создания миниатюр из пользовательской загрузки.
Делает миниатюру, сохраняет пропорции и добавляет белый фон.
Но это выравнивает его по левому верхнему углу.
Мне нужно отцентрировать его по горизонтали и вертикали.
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));