Home >Backend Development >PHP Tutorial >How to implement smart cropping of images using php Codeigniter
CodeIgniter is a set of application development frameworks and toolkits for PHP website developers.
CodeIgniter is a simple and fast PHP MVC framework. The folks at EllisLab released CodeIgniter. After many businesses have tried out all the PHP MVC frameworks, CodeIgniter emerges as the winner, mainly because it provides enough freedom to the organization, allowing developers to work faster.
Freedom means that when using CodeIgniter you don't have to name your database tables a certain way, nor models after the tables. This makes CodeIgniter ideal for refactoring legacy PHP applications, where there may be all the weird structures that need to be ported.
This article mainly introduces the method of Codeigniter to realize intelligent cropping of pictures, which can achieve no distortion after cropping and retain the theme meaning of the picture as much as possible. Friends in need can refer to
for a picture of 1024*768 size, cropped to 240*240 size, without distortion after cropping, and retain the theme and meaning of the picture as much as possible.
The method I used:
1. First reduce the picture to a size that can be cropped;
If it is a wide picture , then scale the height to height = 240px, and the narrow picture (height is greater than the width) scale to the width;
2. Center crop according to the length and width format;
Keep the middle part of the abbreviated image;
$this->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(); $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();
The above is the detailed content of How to implement smart cropping of images using php Codeigniter. For more information, please follow other related articles on the PHP Chinese website!