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

问题描述 投票:3回答:6

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

作为一个例子,我希望“配置文件”图像(缩略图)不大于120 * 150px。脚本需要调整上传图像的大小(无论是纵向还是横向,无论比例如何)都要遵守这些尺寸而不会被拉伸。

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

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

任何投入将不胜感激。

php image-processing resize thumbnails autoresize
6个回答
10
投票
$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,所以最终每侧有10px的空白区域为120x150。 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);

这应该工作,尚未尝试过。


2
投票

根据您的PHP配置,GD或Imagick功能是您所需要的。 对不起,我是新手,我无法在同一条消息中发布这两个链接:(


1
投票

我最近需要php resizing(缩略图)解决方案,并找到了Zebra_Image library,这是一个用PHP编写的轻量级图像处理库。代码很干净,也很容易使用。我强烈建议使用这个库。示例代码足以让您入门。

确保在php.ini文件中设置了足够的内存限制来操作分辨率相对较高的图像(例如2560x1600)。我有一个大图像错误,没有错误打印。我在第1262,1279和1288行调试imagecreatefrom{gif,jpeg,png}中的function _create_from_source调用问题。调用@用静音调用,所以我无法进行更改以获取错误。我删除了@ lines并看到了一个超出内存限制的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');                                                                                                                                              
    }       

(注意:我使用的是Zebra Image 2.2.2版)


0
投票

你可以用ImageMagick做到这一点:

convert susan.jpg -resize x200 susan_thumb.jpg

这是使用shell命令运行的,所以在PHP中你可以使用shell_exec()来运行上面的命令。我认为你不需要任何PHP扩展。

可以在ImageMagick文档中找到一些标志来控制调整大小操作。如果我没记错的话,数字之前的x200意味着“具有相同宽高比的比例为200px”。

我为ImageMagick(和ghostscript)编写了一个安装指南:How to install, test, convert, resize PDF using ImageMagick, Ghostscript, Windows Vista/7 x64基于我的笨手笨脚,也许它可以帮助你。

另一个选项是GD库(详见dqhendricks答案)。这是faster ,显然有更好的记录,用于基本操作。


0
投票

你不需要想象力。这里有一个链接,它将带您进入一个函数,该函数将使用PHP GD将任何图像调整为任意大小。该函数具有使用letterboxing或crop-to-fit方法调整大小以适应新宽高比的选项。该功能也得到了彻底的解释。看看这个。

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

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


0
投票

有些人说想要更快,我接下来会用

    
    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));

    if (!$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
        );
    }
© www.soinside.com 2019 - 2024. All rights reserved.