This article mainly introduces Codeigniter's method of intelligently cropping images, which ensures that the image is not distorted after cropping and retains the topic meaning of the image as much as possible. For more information, see
This article mainly introduces Codeigniter's method of intelligently cropping images, which ensures that the image is not distorted after cropping and retains the topic meaning of the image as much as possible. For more information, see
Crop an image of 1024x768 to 240x240. The cropped image is not distorted and its topic meaning is retained as much as possible.
The method I used:
1. scale down the image proportion to the cropped size;
For a wide image, scale the image to a height equal to PX. For a narrow image (the height is greater than the width), scale the image to a width equal to the width;
2. Center crop by length/width format;
Reserve the middle part of the scaled image;
The Code is as follows:
$ 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 ['high'] = 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 ();