Can't crop image when resizing first

2019-08-09 04:48发布

问题:

I am using the Image Manipulation library in Codeigniter and I need to resize an image to be a max of 278px in width while maintaining ratio. I also need to make sure the image does not exceed 400px.

I am attempting to do this by using $this->image_lib->resize() and then running it again using $this->image_lib->crop(), but I am having trouble with the resize interfering with the crop.

Here are the two models:

public function create_thumb($path) {

    $data = $this->upload->data();

    if ($data['image_width'] >= 278):

        $config['image_library'] = 'gd2';
        $config['source_image'] = $path;
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 278;
        $config['height'] = 400;
        $config['quality'] = '90%';
        $config['master_dim'] = 'width';

        $this->load->library('image_lib', $config);

        if ($this->image_lib->resize()):

            $this->image_lib->clear();

        endif;

    endif;

    $this->crop_image($path);

    return false;
}

// Make max image size 278x400
public function crop_image($path) {

    list($width, $height) = getimagesize($path);

    $config['image_library'] = 'gd2';
    $config['source_image'] = $path;
    $config['x_axis'] = '0';
    $config['y_axis'] = '0';
    $config['maintain_ratio'] = FALSE;
    $config['width'] = $width;
    $config['height'] = 400;
    $config['quality'] = '100%';

    $this->load->library('image_lib', $config);

    if ($this->image_lib->crop())
    {
        return true;
    }
    return false;
}

If I call crop_image() directly from a controller, it crops as expected. However, when it's called from create_thumb(), I get the error Your server does not support the GD function required to process this type of image. Since I am able to crop the image previously and GD is installed according to phpinfo(), I am confused on why I am getting this error.

I think the problem is related to loading the image_lib twice, but I thought that $this->image_lib->clear(); would solve that problem?

What am I doing wrong? Is there a better way for me to resize an image to a maximum of 278px width and a maximum of 400px in height?

回答1:

Try this :-

            //Resize Image
            $config = array();
            $config['image_library'] = 'gd2';
            $config['source_image'] = './assets/uploaded_files/gallery/original/'.$image_name;
            $config['new_image'] = './assets/uploaded_files/gallery/banner/'.$image_name;
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
            $config['master_dim']= 'width';
            $config['quality']  = '100';
            $config['width'] = 1260;
            $config['height']= 645;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            //Crop image
            $config = array();
            $config['image_library'] = 'gd2';
            $config['source_image'] = './assets/uploaded_files/gallery/banner/'.$image_name;
            $config['new_image'] = './assets/uploaded_files/gallery/banner/'.$image_name;
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = FALSE;
            $config['quality']  = '100';
            $config['x_axis'] = 0;
            $config['y_axis'] = 0;
            $config['width'] = 1260;
            $config['height']= 645;
            $this->image_lib->initialize($config);
            $this->image_lib->crop();

Adjust width and height according to your need.