【问题标题】:php: create custom-sized thumbnailphp:创建自定义大小的缩略图
【发布时间】:2011-12-14 00:29:33
【问题描述】:

我熟悉使用 imagecopyresampled 在 php 下调整大小和裁剪图像,但现在我遇到了一个特殊问题: 任务是从例如裁剪大图像。 1600x1200 到 500x120,这意味着将大小调整为 500 像素并将其高度裁剪为 120 像素。有什么简单的方法还是我需要自己计算裁剪值?谢谢

【问题讨论】:

    标签: php image resize


    【解决方案1】:

    有一个叫做 PHPThumb 的 PHP 库可以帮助你。你可以在这里找到https://github.com/masterexploder/PHPThumb

    他们有一种自适应调整大小的方法,可以满足您的需求。 https://github.com/masterexploder/PHPThumb/wiki/Basic-Usage

    【讨论】:

      【解决方案2】:

      你必须自己做。

      我不知道你是否要裁剪,所以这里是如何计算两者的值:

      缩放图像:调整大小以适应新的 w x h 保持纵横比(因此 1 边可能比指定的短)

      function calc_scale_dims($width_orig, $height_orig, $max_width, $max_height) { 
      
          $new_width=$width_orig;
          $new_height=$height_orig;
          $ratioh = $max_height/$new_height;
          $ratiow = $max_width/$new_width;
          $ratio = min($ratioh, $ratiow);
          // New dimensions
          $dims["w"] = intval($ratio*$new_width);
          $dims["h"] = intval($ratio*$new_height); 
      
          return $dims;
      }
      

      调整大小和裁剪:如果新的纵横比不同,则调整图像大小并裁剪以适合指定的 w x h(例如,如果纵横比不同,图像将调整大小以匹配短片上的指定大小如果在中间裁剪,尺寸和较长的尺寸)

      function calc_crop_resize_dims($width_orig, $height_orig, $new_width, $new_height) { 
      
          //Calculate scaling
          $ratio_orig = $width_orig/$height_orig;
          $ratio_new = $new_width/$new_height;
      
          if ($ratio_new < $ratio_orig) {
             $copy_width = $height_orig*$ratio_new;
             $copy_height = $height_orig;
          } else {
             $copy_width = $width_orig;
             $copy_height = $width_orig/$ratio_new;
          }
      
          //point to start copying from (to copy centre of image if we are cropping)
          $dims["src_x"] = ($width_orig - $copy_width)/2;
          $dims["src_y"] = ($height_orig - $copy_height)/2;
          $dims["copy_width"] = $copy_width;
          $dims["copy_height"] = $copy_height;
      
          return $dims;
      }
      

      【讨论】:

        猜你喜欢
        • 2014-05-17
        • 2014-07-24
        • 2014-10-04
        • 2016-10-24
        • 2016-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多