【问题标题】:PHP Thumbnail Image Resizing with proportionsPHP缩略图图像按比例调整大小
【发布时间】:2011-06-03 04:34:17
【问题描述】:

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

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

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

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

任何意见将不胜感激。

【问题讨论】:

    标签: php image-processing resize thumbnails autoresize


    【解决方案1】:
    $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】:

      GD 或 Imagick 函数是您需要的,具体取决于您的 PHP 配置。
      对不起,我是新手,我不能在同一条消息中发布两个链接:(

      【讨论】:

      【解决方案3】:

      我最近需要php调整大小(缩略图)的解决方案,发现Zebra_Image library,这是一个用PHP编写的轻量级图像处理库。代码真的很干净,也很容易使用。我强烈推荐使用这个库。示例代码足以让您入门。

      请确保您在 php.ini 文件中设置了足够的内存限制来处理分辨率相对较高的图像(例如 2560x1600)。大图像出现错误,打印时没有错误。我在 126212791288 行中将问题调试到 function _create_from_source 中的 imagecreatefrom{gif,jpeg,png} 调用。呼叫被 @ 静音,所以我无法更改以获取错误。我删除了 @ 行并看到一个 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)

      【讨论】:

        【解决方案4】:

        您可以使用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 ,显然有更好的文档记录,用于基本操作。

        【讨论】:

          【解决方案5】:

          你不需要想象。这是一个链接,它将带您到一个函数,该函数将使用 PHP GD 将任何图像调整为任意大小。该功能可以选择使用信箱或裁剪以适应方法来调整新的纵横比。该功能也进行了详尽的解释。看看吧。

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

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

          【讨论】:

            【解决方案6】:

            有人说想象得更快,我用下一个

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

            【讨论】:

              猜你喜欢
              • 2011-09-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-01-06
              • 2012-08-29
              • 1970-01-01
              • 2017-09-04
              • 2011-10-22
              相关资源
              最近更新 更多