我的CodeIgniter图像处理库没有按我想要的方式工作


My CodeIgniter image manipulation library isn't working the way I want

我有一个图像。我希望它的大小正好是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。我不知道我的代码出了什么问题

我不确定你的image_lib做了什么,但我认为你没有考虑到宽高比在调整大小时小于所需的尺寸。

假设有一张图片:1000 x 300

当你调整大小时,它变成了500 x 150(因为你保持长宽比)

当你裁剪为500 x 250时,你最终会得到不同的大小,或者扭曲的图像。

你需要做的是,动态地决定哪一边(高度或宽度)有较小的值,然后调整到保持长宽比的那一边,然后裁剪它。这样,图像将始终有足够的内容以上述大小裁剪。

图像库给我带来了很多麻烦——不是因为它不好,而是因为每次我尝试用它做一些新的事情时,我都必须重新学习它是如何工作的。至少对我来说,这个文档有点难以理解。

一开始,我也认为在裁剪之前调整大小更有意义。我不记得确切的原因,但后来我发现,最好是反其道而行之。我可能错了,但是我的代码现在运行得很好,所以我要坚持这个策略。

我认为另一件重要的事情是将'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();
}