Home  >  Article  >  Backend Development  >  Codeigniter smart cropping image sample code

Codeigniter smart cropping image sample code

WBOY
WBOYOriginal
2016-07-25 08:52:53930browse
This article introduces Codeigniter's method of intelligently cropping images. The images are cropped without distortion and the theme meaning of the image is retained as much as possible. Friends in need can refer to it.

Codeigniter smart cropping of pictures

A picture of size 1024*768, cropped to size 240*240, without distortion after cropping, and retain the theme and meaning of the picture as much as possible.

Method: 1. First scale down the image to a size that can be cropped; If it is a wide picture, it will be scaled in proportion to height = 240px. If it is a narrow picture (height is greater than width), it will be scaled in proportion to width;

2. Center crop according to length and width format; Keep the middle part of the thumbnail image;

<?php
$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();
// 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();


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn