Reputation: 3840
I am uploading some images to my webpage and would like their thumbnails to be squares cropped from the centre. I am using Codeigniter and gd2.
Here is my code so far:
$config['image_library'] = 'gd2';
$config['source_image'] = $this->userlibrary->picturesdir . $newfilename;
$config['new_image'] = $this->userlibrary->thumbsdir;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width']= 150;
$config['height']= 150;
The images are scaled nicely but they maintain their aspect ratio and only their width OR height gets set to 150, they are not cropped.
Setting maintain_ratio
will still not crop the image but skew it instead.
How could I do this?
Upvotes: 2
Views: 4752
Reputation: 43
Super late response, but I came across this post when I was looking for the same thing. Thought I'd share the solution I went with for anyone else searching for the same.
$config['source_image'] = 'path/to/image';
//Find smallest dimension
$imageSize = $this->image_lib->get_image_properties($config['source_image'], TRUE);
$newSize = min($imageSize);
$config['image_library'] = 'gd2';
$config['width'] = $newSize;
$config['height'] = $newSize;
$config['y_axis'] = ($imageSize['height'] - $newSize) / 2;
$config['x_axis'] = ($imageSize['width'] - $newSize) / 2;
$this->image_lib->initialize($config);
if(!$this->image_lib->crop())
{
echo $this->image_lib->display_errors();
}
Upvotes: 2
Reputation: 428
//Set config for img library
$config['image_library'] = 'ImageMagick';
$config['library_path'] = '/usr/bin/';
$config['source_image'] = $filePath . $fileOldName;
$config['maintain_ratio'] = false;
//Set cropping for y or x axis, depending on image orientation
if ($fileData['image_width'] > $fileData['image_height']) {
$config['width'] = $fileData['image_height'];
$config['height'] = $fileData['image_height'];
$config['x_axis'] = (($fileData['image_width'] / 2) - ($config['width'] / 2));
}
else {
$config['height'] = $fileData['image_width'];
$config['width'] = $fileData['image_width'];
$config['y_axis'] = (($fileData['image_height'] / 2) - ($config['height'] / 2));
}
//Load image library and crop
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
if ($this->image_lib->crop()) {
$error = $this->image_lib->display_errors();
}
//Clear image library settings so we can do some more image
//manipulations if we have to
$this->image_lib->clear();
unset($config);
Source: https://forum.codeigniter.com/thread-7286.html
Upvotes: 1