使用比例调整PHP缩略图大小

3
作为简要介绍,我目前正在制作一种约会类型的网站。用户可以创建帐户并上传个人资料图片(最多8张)。为了在网站的浏览区域中显示这些图片,我正在寻找一种使用PHP(与第三方处理器/脚本)调整所有上传图像大小的方法,以便生成符合某些尺寸的缩略图。
例如,我希望“个人资料”图像(缩略图)的大小不超过120*150px。脚本需要调整上传的图像(无论是纵向还是横向,无论比例如何)以符合这些尺寸,而不会拉伸图像。
宽度(例如120像素)应始终保持不变,但高度(例如150px)可以因图像比例而异。如果是横向照片,则假定脚本需要从图像中间取出一块?
调整所有图片大小的原因是,在网格中显示个人资料时,所有缩略图的大小都大致相同。
非常感谢您的任何建议。
6个回答

11
$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.
这将按比例缩小任何图片,基于其中一边的最大尺寸(宽度或高度),同时将放大小于max的任何图像,并通过检查宽度和高度是否都小于其max值来停止。因此,200x300的图像将被缩小为100x150,而300x200的图像将被缩小为120x80。
嗯,您希望宽度始终为120,因此会有一些变化,并且,对于像200x300这样的图像,它必须删减一些内容,因为如果没有扭曲,它会缩小到120x180,或者必须进一步缩小并设置信封式黑边,但这应该可以很好地启动您。
在这个例子中,设置信封式黑边只涉及到在imagecopyresized()函数中找出开始绘制新图像的正确X和Y是多少。对于像100x150这样的情况,X可能会是10,因此在最终的120x150中,每侧会有10个像素的空白空间。设置信封式黑边120x80时,X将为0,但是Y将为35,因此在120x150上下各有35个像素的空白空间。
您还需要使用$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);

那应该可以,但我还没有尝试过。


2
GD或Imagick函数是根据您的PHP配置需要的。抱歉,我是一个新手,不能在同一条消息中发布两个链接:(

如果您已经安装了imageMagick,那么您不需要使用shell来使用它。这里有一个PHP类: http://fr.php.net/manual/en/book.imagick.php - Nabab

1
我最近需要一个php缩放(缩略图)的解决方案,发现了Zebra_Image library,这是一个用PHP编写的轻量级图像处理库。代码非常干净,也很容易使用。我强烈推荐使用这个库。示例代码足以让您开始使用。
请确保在php.ini文件中设置了足够的内存限制来处理具有相对较大分辨率(例如2560x1600)的图像。我遇到了大图像的错误,但没有错误可以打印出来。我将问题调试到了function _create_from_source中的imagecreatefrom{gif,jpeg,png}调用,并且在126212791288行中静音了这些调用。这些调用被静音了,所以我不能得到错误。我删除了@行并看到了一个PHP错误,即超过了内存限制。原始内存限制设置为32MB,我将其增加到64MB。现在我可以处理2560x1600的图像,而我拒绝处理更大的图像。
以下是控制图像分辨率的示例代码。
    $image_properties = getimagesize($UPLOADED_FILE_PATH);                   
    $file_width = $image_properties[0];                                
    $file_height = $image_properties[1];                               

    if ($file_width > 2560 || $file_height > 1600) {    
        // handle your error whichever you like, I simply 'die' just to show               
        die('Cannot manipulate image bigger than 2560x1600');                                                                                                                                              
    }       

(注意:我使用斑马图片版本2.2.2)

0
你可以使用 ImageMagick 来完成这个任务:
convert susan.jpg -resize x200 susan_thumb.jpg

这个程序需要使用shell命令运行,在PHP中,您可以使用shell_exec()来运行上述命令。我认为您不需要任何PHP扩展。

在ImageMagick文档中可以找到一些标志来控制调整大小的操作。如果我没记错,在数字前面的x200表示“按相同的宽高比缩放到200像素”。

我写了一个安装指南,介绍如何使用ImageMagick、Ghostscript和Windows Vista/7 x64进行安装、测试、转换和调整PDF大小:如何安装、测试、转换和调整PDF大小使用ImageMagick、Ghostscript、Windows Vista/7 x64,基于我的摸索,希望能对您有所帮助。

另一个选择是GD库(在dqhendricks的回答中有详细介绍)。这是更快,而且据说文档更好,用于基本操作。


0

你不需要imagick。这里有一个链接,可以带你到一个使用PHP GD调整任何图像大小的函数。该函数具有使用letterboxing或crop-to-fit方法调整到新宽高比的选项。该函数也有详细的说明。去看看吧。

http://www.spotlesswebdesign.com/blog.php?id=1

如果这正是您要寻找的,请在此答案旁边选择复选标记。谢谢!

0

Some says imagic faster, i use next

    
    function resizeImageProportional($imagePath, $destinationPath, $width = false, $height = false, $filterType = \Imagick::FILTER_POINT, $blur = 1, $bestFit = false, $cropZoom = false)
    {
        if (!$width && !$height) {
            trigger_error("WTF_IMAGE_RESIZE");
            return false;
        }
        //The blur factor where > 1 is blurry, < 1 is sharp.
        $imagick = new \Imagick(realpath($imagePath));

如果(!$width || !$height) { $orig_width = $imagick->getImageWidth(); $orig_height = $imagick->getImageHeight(); $proportion = $orig_height/$orig_width; if (!$width) { $width = $height*$proportion; } elseif (!$height) { $height = $width*$proportion; } }
if ($bestFit) { $imagick->resizeImage($width, $height, $filterType, $blur, $bestFit); } else { $imagick->resizeImage($width, $height, $filterType, $blur); }
if ($cropZoom) { $cropWidth = $imagick->getImageWidth(); $cropHeight = $imagick->getImageHeight();
$newWidth = $cropWidth / 2; $newHeight = $cropHeight / 2;
$imagick->cropimage( $newWidth, $newHeight, ($cropWidth - $newWidth) / 2, ($cropHeight - $newHeight) / 2 );
$imagick->scaleimage( $imagick->getImageWidth() * 4, $imagick->getImageHeight() * 4 ); }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接