Code example of Codeigniter smart image cropping
This article describes how to use Codeigniter to intelligently crop an image. This method keeps the subject meaning of the image as much as possible. For more information, see. Codeigniter smart image cropping Crop an image of 1024x768 to 240x240. the cropped image is not distorted and the topic meaning of the image is retained as much as possible. Method: 1. scale down the image proportion to the size that can be cropped. for a wide image, scale the image to a height of PX and a narrow image (the height is greater than the width) scales according to the width or other proportions; 2. crop the image in the center of the length and width format, and retain the middle part of the scaled image;
load->library('image_lib'); list($width, $height) = getimagesize("upload/123.jpg");$config['image_library'] = 'gd2';$config['source_image'] = 'upload/123.jpg';$config['maintain_ratio'] = TRUE;if($width >= $height){$config['master_dim'] = 'height';}else{$config['master_dim'] = 'width';}$config['width'] = 240;$config['height'] = 240;$this->image_lib->initialize($config);$this->image_lib->resize();// by bbs.it-home.org$config['maintain_ratio'] = FALSE;if($width >= $height){$config['x_axis'] = floor(($width * 240 / $height - 240)/2);}else{$config['y_axis'] = floor(($height * 240 / $width - 240)/2);}$this->image_lib->initialize($config);$this->image_lib->crop(); |