【问题标题】:How can I crop/scale user images so I can display fixed sized thumbnails without skewing and stretching?如何裁剪/缩放用户图像,以便显示固定大小的缩略图而不会倾斜和拉伸?
【发布时间】:2010-11-02 18:11:42
【问题描述】:

我正在努力让用户为我的网站上传个人资料图片。我试图避免的经典例子是plentyoffish.com,每个用户的图像都是倾斜的,看起来很丑:

那么,如何在没有上述倾斜的情况下以编程方式裁剪/创建图像的标准尺寸版本?

【问题讨论】:

  • 您使用什么语言/平台?
  • 将图片替换为来自小鸡百科的图片。它们看起来都很漂亮。

标签: image image-processing crop image-scaling user-profile


【解决方案1】:

嗯,你必须有一个最大的高度和宽度,让我们假设你可用的图像尺寸是正方形的,比如 100x100。

当用户上传一张图片时,获取它的尺寸,然后计算出哪个更大,高度或宽度。

然后进行最大测量并获得该测量值与目标测量值的比率,然后使用该比率来缩放高度和宽度。

因此,如果用户上传一张 500 高和 450 宽的图片,由于高度最大,您需要将 100 除以 500,即您的缩略图大小。这给了我们 0.2 作为比率。这意味着宽度将变为90,因此您将缩小到100x90,并且不会发生变形。

【讨论】:

    【解决方案2】:

    这是我用来调整大小的一些代码(C#),类似于 blowdart 建议的方法。在您的情况下,只需将“300”替换为一侧的最大尺寸:

     private Bitmap ScaleImage(Image oldImage)
        {
            double resizeFactor = 1;
    
            if (oldImage.Width > 300 || oldImage.Height > 300)
            {
                double widthFactor = Convert.ToDouble(oldImage.Width) / 300;
                double heightFactor = Convert.ToDouble(oldImage.Height) / 300;
                resizeFactor = Math.Max(widthFactor, heightFactor);
    
            }
            int width = Convert.ToInt32(oldImage.Width / resizeFactor);
            int height = Convert.ToInt32(oldImage.Height / resizeFactor);
            Bitmap newImage = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(newImage);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(oldImage, 0, 0, newImage.Width, newImage.Height);
            return newImage;
        }
    

    【讨论】:

      【解决方案3】:

      或者:如果您仍然想要固定尺寸,请按照吹镖的说明进行,但改为计算最大比例:100px / 450px = .22..

      • 宽度:100px
      • 高度:111.11..px -> 从顶部裁剪 ((111.11 - 100) / 2) 向下 100px。

      EDIT:或者让用户选择如何裁剪最大尺寸。

      【讨论】:

        【解决方案4】:

        使用ImageMagick。在命令行使用:

        convert -thumbnail geometry
        

        【讨论】:

          【解决方案5】:

          我不久前为 PHP 制作了这个函数,它非常适用于这个场景和其他一些场景:

          <?php
          
          function Image($source, $crop = null, $resize = null)
          {
              $source = ImageCreateFromString(file_get_contents($source));
          
              if (is_resource($source) === true)
              {
                  $width = imagesx($source);
                  $height = imagesy($source);
          
                  if (isset($crop) === true)
                  {
                      $crop = array_filter(explode('/', $crop), 'is_numeric');
          
                      if (count($crop) == 2)
                      {
                          if (($width / $height) > ($crop[0] / $crop[1]))
                          {
                              $width = $height * ($crop[0] / $crop[1]);
                              $crop = array((imagesx($source) - $width) / 2, 0);
                          }
          
                          else if (($width / $height) < ($crop[0] / $crop[1]))
                          {
                              $height = $width / ($crop[0] / $crop[1]);
                              $crop = array(0, (imagesy($source) - $height) / 2);
                          }
                      }
          
                      else
                      {
                          $crop = array(0, 0);
                      }
                  }
          
                  else
                  {
                      $crop = array(0, 0);
                  }
          
                  if (isset($resize) === true)
                  {
                      $resize = array_filter(explode('*', $resize), 'is_numeric');
          
                      if (count($resize) >= 1)
                      {
                          if (empty($resize[0]) === true)
                          {
                              $resize[0] = round($resize[1] * $width / $height);
                          }
          
                          else if (empty($resize[1]) === true)
                          {
                              $resize[1] = round($resize[0] * $height / $width);
                          }
                      }
          
                      else
                      {
                          $resize = array($width, $height);
                      }
                  }
          
                  else
                  {
                      $resize = array($width, $height);
                  }
          
                  $result = ImageCreateTrueColor($resize[0], $resize[1]);
          
                  if (is_resource($result) === true)
                  {
                      ImageCopyResampled($result, $source, 0, 0, $crop[0], $crop[1], $resize[0], $resize[1], $width, $height);
                      ImageDestroy($source);
          
                      header('Content-Type: image/jpeg');
          
                      ImageJPEG($result, null, 90);
                      ImageDestroy($result);
                  }
              }
          
              return false;
          }
          
          Image('/path/to/your/image.jpg', '1/1', '100*');
          Image('/path/to/your/image.jpg', '1/1', '100*100');
          Image('/path/to/your/image.jpg', '1/1', '100*500');
          
          ?>
          

          【讨论】:

            【解决方案6】:

            这是我使用 ImageMagick 的转换工具拼凑起来的一个 bash 命令。对于位于父目录中的一组图像,一些肖像,一些风景,在当前目录中创建缩放到 600x400 的图像,从中心裁剪肖像图像并简单地缩放风景图像:

            for f in ../*jpg; do
                echo $f; 
                size=`identify $f|cut -d' ' -f 3`; 
                w=`echo $size|cut -dx -f 1`; 
                h=`echo $size|cut -dx -f 2`; 
                if [ $w -gt $h ]; then 
                    convert $f -thumbnail 600x400 `basename $f`; 
                else 
                    convert $f -scale 600x -crop 600x400+0+`echo "((600*($h/$w))/2)" | bc | sed 's/\..*//'` `basename $f`; 
                fi;
            done;
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-06-18
              • 2023-03-28
              • 1970-01-01
              相关资源
              最近更新 更多