PHP缩略图图像按比例调整大小

问题描述:

作为一个简短的故事,我目前正在建立一个约会类型的网站。用户可以创建帐户并上传个人资料照片(最多8个)。为了在网站的浏览区域中显示这些内容,我正在寻找一种PHP(使用第三方处理器/脚本)的方式来调整上传的所有图像的大小,使其具有符合某些尺寸的缩略图。

As a brief run down, I am currently making a dating type site. Users can create accounts and upload profile pictures (up to 8). In order to display these in the browse area of the website, I am looking for a way in PHP (with third party processor/scripts) to resize all images uploaded to have thumbnails that adhere to certain dimensions.

作为一个例子,我希望个人资料图像(缩略图)不大于120 * 150px。脚本需要调整上传图像的大小(无论是纵向还是横向,无论比例如何)都要坚持这些尺寸而不会拉伸。

As an example, I will want "profile" images (thumbnails) to be NO larger than 120*150px. The scripting needs to resize uploaded images (regardless of whether they are portrait or landscape, and regardless of proportions) to adhere to these dimensions without getting stretched.

宽度(例如。120像素)应始终保持不变,但高度(例如150px)可以变化,以保持图像成比例。如果它是风景照片,我假设脚本需要从图像中间取出一块?

The width (eg. 120pixels) should always remain the same, but the height (eg. 150px) can vary in order to keep the image in proportion. If it's a landscape photo, I'm assuming the script would need to take a chunk out of the middle of the image?

所有要调整大小的图像的原因是因此,当在网格中显示所有缩略图大小相同的配置文件时。

The reason that all images to be resized is so that when profiles are display in a grid that all thumbnails are roughly the same size.

任何输入都将非常感激。

Any input would be greatly appreciated.

$maxwidth = 120;
$maxheight = 150;

$img = imagecreatefromjpeg($jpgimage); 
//or imagecreatefrompng,imagecreatefromgif,etc. depending on user's uploaded file extension

$width = imagesx($img); //get width and height of original image
$height = imagesy($img);

//determine which side is the longest to use in calculating length of the shorter side, since the longest will be the max size for whichever side is longest.    
if ($height > $width) 
{   
$ratio = $maxheight / $height;  
$newheight = $maxheight;
$newwidth = $width * $ratio; 
}
else 
{
$ratio = $maxwidth / $width;   
$newwidth = $maxwidth;  
$newheight = $height * $ratio;   
}

//create new image resource to hold the resized image
$newimg = imagecreatetruecolor($newwidth,$newheight); 

$palsize = ImageColorsTotal($img);  //Get palette size for original image
for ($i = 0; $i < $palsize; $i++) //Assign color palette to new image
{ 
$colors = ImageColorsForIndex($img, $i);   
ImageColorAllocate($newimg, $colors['red'], $colors['green'], $colors['blue']);
} 

//copy original image into new image at new size.
imagecopyresized($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($newimg,$outputfile); //$output file is the path/filename where you wish to save the file.  
//Have to figure that one out yourself using whatever rules you want.  Can use imagegif() or imagepng() or whatever.

这将根据较长的一侧(宽度或高度)按比例缩小任何图像最大尺寸。它还会炸掉任何小于最大值的图像,您可以通过检查宽度和高度是否小于最大值来停止。因此,200x300图像将缩小至100x150,300x200图像将缩小至120x80。

This will shrink any images down proportionally based on whichever side is longer (width or height), to the maximum size. It will also blow up any images smaller than max, which you can stop with a bit of checking on whether or not both width and height are less than their max. So, a 200x300 image will be shrunk to 100x150, and a 300x200 image will be shrunk to 120x80.

嗯,您希望宽度始终为120,所以它会改变一点,是的,它必须在像200x300这样的图像的情况下削减一些东西,因为它会缩小到120x180而没有任何失真,或者它必须将它缩小得更远并且信箱它,但那应该得到你开始很好。

Hmm, you want the width to always be 120, so it would change a bit, and yeah, it would have to cut something out in the case of an image like 200x300, because that would shrink to 120x180 without any distortion, or it would have to shrink it farther and letterbox it, but that should get you started nicely.

信箱这个例子只需要弄清楚开始绘制到新图像的正确x和y是在imagecopyresized()函数中。在像100x150这样的情况下,我想X会是10,所以最终每侧有10px的空白区域为120x150。 Letterboxing 120x80 X将为0,但Y为35,因此在120x150上方和下方将有35px的空白区域。

Letterboxing this example would just involve figuring out what the proper x and y to start the drawing to the new image would be in the imagecopyresized() function. In the case of something like 100x150, the X would be 10, I think, so there would be 10px of blank space on each side for 120x150 in the end. Letterboxing 120x80 X would be 0 but Y would be 35, so there would be 35px of blank space above and below for 120x150.

您还想制作$ newimg使用$ maxwidth,$ maxheight而不是$ newwidth,$ newheight,但imagecopyresized()仍然会使用两个$ new值。

You'd also want to make $newimg with $maxwidth,$maxheight rather than $newwidth,$newheight, but the imagecopyresized() would still use both $new values.

因为我很无聊而且不喜欢还有别的办法,这些改变就可以做到:

Since I'm bored and don't have anything else to do, these changes would do it:

if ($height > $width) 
{   
$ratio = $maxheight / $height;  
$newheight = $maxheight;
$newwidth = $width * $ratio; 
$writex = round(($maxwidth - $newwidth) / 2);
$writey = 0;
{
else 
{
$ratio = $maxwidth / $width;   
$newwidth = $maxwidth;  
$newheight = $height * $ratio;   
$writex = 0;
$writey = round(($maxheight - $newheight) / 2);
}

$newimg = imagecreatetruecolor($maxwidth,$maxheight);

//Since you probably will want to set a color for the letter box do this
//Assign a color for the letterbox to the new image, 
//since this is the first call, for imagecolorallocate, it will set the background color
//in this case, black rgb(0,0,0)
imagecolorallocate($newimg,0,0,0);

//Loop Palette assignment stuff here

imagecopyresized($newimg, $img, $writex, $writey, 0, 0, $newwidth, $newheight, $width, $height);

这应该有效,尚未尝试过。

That should work, haven't tried it yet.