Codeigniter Method for Intelligent Picture Clipping
- 2021-06-29 10:27:02
- OfStack
1 pair of 1024*768 pictures, cut to 240*240 size, without distortion, keep the theme meaning as much as possible.
The method I use:
1. First, abbreviate the pictures to the size that can be clipped;
If the picture is wide, zoom to Height= 240px in equal proportion to Height, and zoom to Width in equal proportion to Narrow Picture (Height is larger than Width);
2. Centrally cut in length and width format;
Keep the middle part of the thumbnailed picture;
$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();