PHP 缩略图按比例调整大小

标签 php image-processing resize thumbnails autoresize

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

例如,我希望“个人资料”图像(缩略图)不大于 120*150 像素。脚本需要调整上传图像的大小(无论它们是纵向还是横向,也无论比例如何)以符合这些尺寸而不会被拉伸(stretch)。

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

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

任何输入将不胜感激。

最佳答案

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

嗯,您希望宽度始终为 120,所以它会稍微改变,是的,对于像 200x300 这样的图像,它必须剪掉一些东西,因为那样会缩小到 120x180 而没有任何失真,否则它必须将它缩小得更远并用信箱装箱,但这应该能让你很好地开始。

Letterboxing 这个例子只涉及确定在 imagecopyresized() 函数中开始绘制新图像的正确 x 和 y 是什么。在像 100x150 这样的情况下,X 应该是 10,我想,所以最终 120x150 的每边会有 10px 的空白空间。 Letterboxing 120x80 X 为 0 但 Y 为 35,因此对于 120x150,上下各有 35px 的空白区域。

您还希望使用 $maxwidth,$maxheight 而不是 $newwidth,$newheight 来制作 $newimg,但是 imagecopyresized() 仍会同时使用这两个 $new 值。

因为我很无聊而且没有别的事可做,所以这些改变就可以了:

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 缩略图按比例调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4590441/

相关文章:

Java 高级成像 GradientMagnitudeDescriptor

php - 使用php对sql表执行操作

php - 使用 Doctrine Symfony2 中的查询生成器实现 SQL 联合查询

php - codeigniter 应用程序在新应用程序中查看文件夹位置问题

OpenCV - 如何将像素跟随轮廓转换为顶点跟随轮廓

iphone - 确定图像亮度/亮度

javascript - 动态调整导航 div 的大小以适应主要内容

javascript - 如何防止 jquery 可调整大小的 div 接收调整大小点击事件

在 Tensorflow 中调整 MNIST 的大小

php - 使用 httpclient 从 webservice 读取