How to do a gravity zoom in CodeIgniter with Imagemagick?
I am trying to crop an image (grabbed via Curl) in such a manner so that the cropped image is 171 x 118px. Instead of force re-sizing that image to be 171x118px, I am trying to have it work in such a manner so that it grabs any 171x118px area of the image and crops that. So of the original image is http://www.mirzar.com/ember/actual-image.png, it should output http://www.mirzar.com/ember/desired-crop.png instead of http://www.mirzar.com/ember/cropped.png.
Here's my code so far:
$config['image_library'] = 'ImageMagick';
$config['library_path'] = '/usr/bin/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$this->load->library('image_lib', $config);
$dimensions[0] = array('x'=>171, 'y'=>118, 'dir'=>'small');
$dimensions[1] = array('x'=>154, 'y'=>105, 'dir'=&g开发者_Go百科t;'medium');
$dimensions[2] = array('x'=>312, 'y'=>164, 'dir'=>'large');
for ($i=0; $i<3;$i++){
$filesize = filesize($filename);
$config['image_library'] = 'ImageMagick';
$config['library_path'] = '/usr/bin/';
$config['source_image'] = $filename;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = $dimensions[$i]['x'];
/* $config['height'] = $dimensions[$i]['y']; */
$config['master_dim'] = 'width';
$this->image_lib->test();
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->saveThumbnail($imageid, $ext, $dimensions[$i]['dir'], $i);
}
// regular site pic
$config['width'] = 171;
$config['height'] = 218;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
Firstly, it seems your 'small' and 'medium' dimensions are the wrong way round in the dimensions array.
As for the task itself, you could simply use the 'crop' function of codeigniter to give you the correct dimensions for the thumbnail. There are a few methods you could use:
1) Resize the image using the method you describe except maintain the image ratio and use $config['master_dim'] = 'auto';
. Then use the crop function to crop this newly resized image to the exact dimensions you desire. This method has the added bonus of compensating for images that may be smaller than your thumbnails!
2) Crop the image from the start and ignore the resize altogether.
Either way, you would be adding something like so:
$config['x_axis'] = 0;
$config['y_axis'] = 0;
$config['width'] = $dimensions[$i]['x'];
$config['height'] = $dimensions[$i]['y'];
$this->image_lib->initialize($config);
$this->image_lib->crop();
A third method which would create much neater thumbnails would be to work out the 'best fit' for each image. Something like the following:
$size = getimagesize($filename);
$width_ratio = floor($size[0] / $dimensions[$i]['x']);
$height_ratio = floor($size[1] / $dimensions[$i]['y']);
$min_ratio - min($width_ratio, $height_ratio);
$left = (($size[0] - ($dimensions[$i]['x'] * $min_ratio)) / 2);
$right = (($size[1] - ($dimensions[$i]['y'] * $min_ratio)) / 2);
$config['width'] = ($dimensions[$i]['x'] * $min_ratio);
$config['height'] = ($dimensions[$i]['y'] * $min_ratio);
$config['x_axis'] = $left;
$config['y_axis'] = $right;
$this->image_lib->initialize($config);
$this->image_lib->crop();
Then resize this new image to your desired thumbnail size.
Bare in mind, I haven't checked for ratios less than 0, I'll leave that to you.
Regards, eyaka1
精彩评论