我将图片上传到我的网站。图像宽度为498,高度为402。 我需要制作一个预定图像,其最大宽度为250px,最大高度为250px,但图像应为250到250,并且必须与250像素的宽度成比例。
怎么做?
修改
我将图片上传到您的服务器。我想要制作250个宽度和250个高度的尺寸限制。 这并不意味着如果我上传1000h500的图像,那么它必须做250x250,这意味着我们做的宽度为250像素,高度与第一维度成正比为125.最后,我应该得到一张图片250x125。 第二个例子:我的图像是100h800。我的意思是应该改变
答案 0 :(得分:3)
这是我为使用GD生成缩略图而编写的函数。您可以传递最大宽度或高度,或两者(如果为零,表示不受限制),缩略图将缩放为$dest
(+文件扩展名),比例保持不变。它也适用于透明图像。剩下的任何额外空间应该是完全透明的;如果您想要其他背景,请在$img
之前修改imagecopyresampled()
。
function picThumb($src, $dest, $width = 0, $height = 0, $quality = 100)
{
$srcType = exif_imagetype($src);
if (!$width && !$height)
{
$ext = image_type_to_extension($srcType, false);
copy($src, $dest . '.' . $ext);
return $ext;
}
ini_set('memory_limit', '134217728');
try
{
switch ($srcType)
{
case IMAGETYPE_JPEG:
$srcImg = imagecreatefromjpeg($src);
break;
case IMAGETYPE_PNG:
$srcImg = imagecreatefrompng($src);
break;
case IMAGETYPE_GIF:
$srcImg = imagecreatefromgif($src);
break;
default:
throw new Exception();
}
$srcWidth = imagesx($srcImg);
$srcHeight = imagesy($srcImg);
if (!$srcWidth || !$srcHeight)
{
throw new Exception();
}
if ($width && $height)
{
$ratio = min($srcWidth / $width, $srcHeight / $height);
$areaWidth = round($width * $ratio);
$areaHeight = round($height * $ratio);
$areaX = round(($srcWidth - $areaWidth) / 2);
$areaY = round(($srcHeight - $areaHeight) / 2);
}
else // if (!$width || !$height)
{
if ($width)
{
$height = round($width / $srcWidth * $srcHeight);
}
else // if ($height)
{
$width = round($height / $srcHeight * $srcWidth);
}
$areaWidth = $srcWidth;
$areaHeight = $srcHeight;
$areaX = 0;
$areaY = 0;
}
$img = imagecreatetruecolor($width, $height);
imagealphablending($img, false);
imagecopyresampled($img, $srcImg, 0, 0, $areaX, $areaY, $width, $height, $areaWidth, $areaHeight);
switch ($srcType)
{
case IMAGETYPE_JPEG:
$ext = 'jpg';
imagejpeg($img, $dest . '.' . $ext, $quality);
break;
case IMAGETYPE_PNG:
case IMAGETYPE_GIF:
$ext = 'png';
imagesavealpha($img, true);
imagepng($img, $dest . '.' . $ext, 9);
break;
default:
throw new Exception();
}
imagedestroy($srcImg);
imagedestroy($img);
}
catch (Exception $e)
{
ini_restore('memory_limit');
throw $e;
}
ini_restore('memory_limit');
return $ext;
}
答案 1 :(得分:1)
我建议使用ImageMagick类来实现此目的。 一些代码串,如何制作250x250图像并保存它:
$img = new Imagick('/path/to/image/image.jpg'); //image.jpg - your file
$img->cropThumbnailImage(250, 250); //make thumbnail 250x250
$img->writeImage('/newptah/newfilename.jpg'); //write thumbnail to new path
$img->destroy(); //free resources
newfilename.jpg - 将是250x250平方而不会失去比例。
答案 2 :(得分:0)
你可以使用getimagesize功能。它将返回一个数组
$ size = getimagesize($ filename);
$ width = $ size [0];
$ height => $尺寸[1];
然后,由于您的图像宽度大于高度,请将原始宽度和高度乘以250/498
答案 3 :(得分:-1)
function makeThumbnail($image, $dest)
{
$imageType = exif_imagetype($image);
switch ($imageType)
{
case IMAGETYPE_JPEG:
$img = imagecreatefromjpeg($image);
break;
case IMAGETYPE_PNG:
$img = imagecreatefrompng($image);
break;
case IMAGETYPE_GIF:
$img = imagecreatefromgif($image);
break;
default:
throw new Im_Exception('Bad extension');
}
$width = imagesx($img);
$height = imagesy($img);
if ($height > $width)
{
$ratio = 250 / $height;
$newHeight = 250;
$newWidth = $width * $ratio;
}
else
{
$ratio = 250 / $width;
$newWidth = 250;
$newHeight = $height * $ratio;
}
$previewImg = imagecreatetruecolor($newWidth, $newHeight);
$palsize = ImageColorsTotal($img);
for ($i = 0; $i < $palsize; $i++)
{
$colors = ImageColorsForIndex($img, $i);
ImageColorAllocate($previewImg, $colors['red'], $colors['green'], $colors['blue']);
}
imagecopyresized($previewImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
switch ($imageType)
{
case IMAGETYPE_JPEG:
$ext = 'jpg';
imagejpeg($previewImg, $dest . '.' . $ext);
break;
case IMAGETYPE_PNG:
case IMAGETYPE_GIF:
$ext = 'png';
imagesavealpha($previewImg, true);
imagepng($previewImg, $dest . '.' . $ext, 9);
break;
default:
throw new Im_Exception();
}
imagedestroy($previewImg);
imagedestroy($img);
}