[eluser]MihaiD[/eluser]
Hello everybody
This is my first contribution here and it's pretty modest so please go easy on me
I created this library because i needed to force image uploads to look the same in a project of mine, without limiting the user to certain image dimensions.
So my approach was 3 steps:
1. accept any image from user (limited only by php.ini max upload size setting)
2. crop image to desired ratio
3. resize image to my desired sizes
Notes:
- steps 2 and 3 could probably be done in just one step - but by doing so I had sometimes problems with the result - GD2 would throw in some ugly black backgrounds on ocasion.
- you repeat steps more times for saving an image with different aspects and sizes
Usage:
1. load library
Code:
$this->load->library('md_image');
2. crop image to desired ratio (defaults to 4:3)
Code:
$new_img = $this->md_image->crop_to_ratio('path/to/source.jpg', $source_width, $source_height);
optionally you can specify different ratio and / or specify a target image (by default source gets replaced)
Code:
$new_img = $this->md_image->crop_to_ratio('path/to/source.jpg', $source_width, $source_height,16,9,'path/to/target.jpg');
No worries if source is portrait - the ratio will be applied on the long side
3. resize cropped image
Code:
$this->md_image->resize_image('path/to/source.jpg',$desired_width,$desired_height,'path/to/target.jpg');
again - target is optional, if not specified source will be replaced
for example you can use in step 2 the default ratio of 4:3 and specify desired_with = 600 / desired_height = 450 in step 3
I tested this both in both local and production environments and it works great.
Cropping function will work no matter what image size it gets (although really large sizes might get GD2 messed up) but be careful with resize - it might give you the black frames problem if you will try to resize from small to large.
I hope this will help somebody out there.