PHP 缩略图按比例调整大小 [英] PHP Thumbnail Image Resizing with proportions

查看:35
本文介绍了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*150 像素.脚本需要调整上传图像的大小(无论它们是纵向还是横向,也不管比例)以符合这些尺寸而不会被拉伸.

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 像素)应始终保持不变,但高度(例如 150 像素)可以变化以保持图像的比例.如果是风景照片,我假设脚本需要从图像中间取出一大块?

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.

任何输入将不胜感激.

推荐答案

$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.

Letterboxing 这个例子只涉及在 imagecopyresized() 函数中找出合适的 x 和 y 来开始绘制新图像.在像 100x150 这样的情况下,我认为 X 将是 10,所以最终 120x150 的每一边都会有 10px 的空白空间.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.

您还想使用 $maxwidth,$maxheight 而不是 $newwidth,$newheight 制作 $newimg,但 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);

应该可以,还没试过.

这篇关于PHP 缩略图按比例调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆