How to intelligently crop images using Codeigniter

Source: Internet
Author: User
Tags codeigniter
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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.