【问题标题】:My CodeIgniter image manipulation library isn't working the way I want我的 CodeIgniter 图像处理库没有按我想要的方式工作
【发布时间】:2013-05-02 18:01:17
【问题描述】:

我有一张图片。我希望它的大小正好是 500x250。我也想保持图像比例。所以我的计划是重新调整大小然后裁剪。我调整图像大小的代码如下。

$config['image_library'] = 'gd2';
$config['source_image'] = './pictures/'.$pic_name;
$config['maintain_ratio'] = TRUE;
$config['width'] = 500;
$this->load->library('image_lib', $config);
$this->image_lib->resize();

调整大小后,图像大小为 500x768。然后我试图裁剪它。裁剪代码如下。

$config['image_library'] = 'gd2';
$config['source_image'] = './pictures/'.$pic_name;
$config['x_axis'] = '0';
$config['y_axis'] = '0';
$config['height'] = 250;
$config['width'] = 500;
$this->image_lib->initialize($config); 
$this->image_lib->crop();

现在图像的大小变成了 163x250。我无法弄清楚我的代码有什么问题。

【问题讨论】:

    标签: php codeigniter image-manipulation crop


    【解决方案1】:

    我不确定您的 image_lib 做了什么,但我认为您没有考虑到在调整大小时纵横比变得小于所需大小。

    假设有一张图片说:1000 x 300

    当您调整大小时,它变为 500 x 150(因为您要保持纵横比)

    当您将其裁剪为 500 x 250 时,您最终会得到不同尺寸或歪斜的图像。

    您需要做的是,动态确定哪一侧(高度或宽度)具有较小的值,然后在保持纵横比的情况下调整到该一侧,然后对其进行裁剪。这样,图像将始终有足够的内容以上述尺寸进行裁剪。

    【讨论】:

      【解决方案2】:

      图像库让我很头疼——不是因为它不好,而是因为每次我尝试用它做一些新的事情时,我都必须重新学习它的工作原理。至少对我来说,文档有点难以理解。

      一开始,我还认为在裁剪之前调整大小更有意义。我不记得确切的原因,但后来我发现最好做相反的事情。我可能错了,但我的代码现在可以正常工作了,所以我会坚持这个策略。

      我认为重要的另一件事是将“maintain_ratio”设置为 FALSE 并自己进行计算。

      我最近重写了我的调整大小函数,我认为它主要是不言自明的,除了我的变量 $top_crop。那是我的“理发师”变量,它试图假设要从顶部脱掉多少。在我的配置文件“设置”中,我将其设置为 20。这意味着在要裁剪的总量中,从顶部减去 20%。换句话说,如果您要裁剪 100px,则从顶部取 20,从底部取 80。

      无论如何,这是我的裁剪代码。您可以使用它并根据您的需要进行调整:

      function resize_img($data){
          if ($data['width'] == 0 || $data['height'] == 0){
              return FALSE;
          }
          $this->config->load('settings');
          $ratio = $data['height']/$data['width'];
          $targ_ratio = $data['max_ht']/$data['max_wd'];
          $top_crop = $this->config->item('crop_top');
          if ($targ_ratio >= $ratio){
              //too wide
              $crop_width = floor($data['height'] / $targ_ratio);
              $crop_height = $data['height'];
          } else {
              //too tall
              $crop_width = $data['width'];
              $crop_height = floor($data['width'] * $targ_ratio);
          }
          $img_data = array(  'source_image'      =>  $data['full_path'],
                              'maintain_ratio'    =>  FALSE,
                              'x_axis'            =>  round(($data['width'] - $crop_width)/2),
                              'y_axis'            =>  round(($data['height'] - $crop_height)*$top_crop/100),
                              'width'             =>  $crop_width,
                              'height'            =>  $crop_height);
          //thumbs have a target path
          if ($data['target_path']){
                  $img_data['new_image'] = $data['target_path'];
                  //set source for the crop, because for thumbs it will be the thumb folder
                  $source = $data['target_path'].$data['file_name'];
          } else {
                  $source = $data['full_path'];
          }
          $this->image_lib->clear();
          $this->image_lib->initialize($img_data);
          if ($this->image_lib->crop()){
              $img_data = array(  'source_image'  =>  $source,
                                  'maintain_ratio'    =>  FALSE,
                                  'width'             =>  $data['max_wd'],
                                  'height'        =>  $data['max_ht']);
              $this->image_lib->clear();
              $this->image_lib->initialize($img_data);
              if($this->image_lib->resize()){
                  return array('height' => $data['max_ht'], 'width' => $data['max_wd']);
              }
          }
          return $this->image_lib->display_errors();
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        • 2021-12-15
        • 2019-01-18
        • 1970-01-01
        • 1970-01-01
        • 2023-01-23
        • 2011-04-18
        相关资源
        最近更新 更多