简体   繁体   中英

how to crop image in codeigniter?

i'm using codeigniter to build a project and right now i need to create a thumb depending on user choice. like he will give me X1,Y1,X2,Y2,X3,Y3,X4,Y4 i want to crop the image depending on that 4 points. i checked the image manipulation class. the crop function seems to be very strange. any help please ?

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 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.

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();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM