how to crop image in codeigniter?

こ雲淡風輕ζ 提交于 2019-11-29 05:11:18

I know that the documentation is sparse on this particular function in the image library. The crop function asks you only to supply to axises. It will then cut along these axes and return the part of the image which is closer to the center. So if you set the x axis to 10 and the y axis to 10 it will remove the top 10px of the image and the left 10 px of the image. Similarly, if you set the x axis to the image width - 10 it will crop 10 pixels from the right of the image.

What your four positions tell you are really four different axises. Therefore, you need to do two operations. You just need to change the axises in between each $this->image_lib->crop().

How to figure out these axises depends on how you get this data. In an array, as separate values etc. so I won't go into this.

You will need to set the x-axis (left), width (right), y-axis (top), and height (bottom). You need to be sure to set the width and height of the image.

        list($width, $height, $type, $attr) = getimagesize($img);

        $CI->load->library('image_lib');

        $config['image_library'] = 'gd2';
        $config['source_image'] = $img;
        $config['x_axis'] = '10';
        $config['y_axis'] = '10';
        $config['maintain_ratio'] = FALSE;
        $config['width'] = $width-10;
        $config['height'] = $height-10;

The code above will crop the image by 10 pixels on the left, right, top, and bottom. you can feel free to change the value of '10' to whichever value you prefer ;)

i am not getting any results with gd2 library. it always resizes image, but newer crops.

so here is the solution with imagemagick and works great.

public function resize_prep($path, $file){
        $config['image_library'] = 'imagemagick';
        $config['library_path'] = '/usr/bin';
        $config['source_image'] = $path;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = FALSE;
        $config['x_axis'] = 300;
        $config['y_axis'] = 300;
        //$config['width'] = 650;
        //$config['height'] = 353;      
        $config['new_image'] = './uploads/'.$file;

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

        $this->image_lib->initialize($config); 
        if (!$this->image_lib->crop()){
            echo $this->image_lib->display_errors();
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!