• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Image Cropping & Resizing

#1
[eluser]Nikhil Vijayan[/eluser]
i a website i recently build the admin need a facility to create albums and upload photos in it..

But the photos need to be resized to 140 x 90 size..

The prblm i am facing is when admin uploads a photo of different proportion.
i know that cropping the image before resizing would work.

but how can i crop an image to 140 x 90 proportion using CI Image library ?

#2
[eluser]Pygon[/eluser]
http://ellislab.com/codeigniter/user-gui...e_lib.html

#3
[eluser]Phil Sturgeon[/eluser]
Set the width and height values in config, turn on resize and if they have to be EXACTLY 140 x 90 turn off maintain_ration to FALSE.

Its all in there man Wink

#4
[eluser]Nikhil Vijayan[/eluser]
@ thepyromaniac if turn maintain_ratio to False the images will be distorted. What i need is a cropping algortihm that can crop it in a size which has same width and height proportion to 140 x 90. after that i can easily resize the image to 140 x 90 correctly.

#5
[eluser]Pygon[/eluser]
Code:
$fCrop = min( floor($imgWidth / 140), floor($imgHeight / 90)); //Calculate max crop factor
//Crop to:
$config['width'] = 140*$fCrop;
$config['height'] = 90*$fCrop;
$config['x_axis'] = ($imgWidth - $config['width'])/2; //Center crop on X
$config['y_axis'] = ($imgHeight - $config['height'])/2; //Center crop on Y
//[rest of options....]

#6
[eluser]Phil Sturgeon[/eluser]
Come on, what do we know about boolean structures in programming? They are based on the English language so lets re-eavaluate my post shall we?

Quote:if they have to be EXACTLY 140 x 90 turn maintain_ratio to FALSE.

lets see if we can use logic to help your problem, lets swap the sentance to the opposite form.

Quote:if they dont have to be EXACTLY 140 x 90 turn maintain_ratio to TRUE.

#7
[eluser]Bluemill Media[/eluser]
You're misunderstanding what he's asking, thepyromaniac. I'm having the same problem.

What we need to do is resize the image AND crop it. So, if an image has a width of 400px and a height of 800px, then we'd need to first resize it to 140px width and 280px height, then crop the height to 90px width. So the end result is an UNDISTORTED image (which turning maintain_ratio off can't provide), with a width of 140px and a height of 90px, all within one image call.

Does anybody have a solution for this yet?

#8
[eluser]funkmyer[/eluser]
I have the same issue and I tried Pygon's code and it doesn't work. Someone must have figured out how to do this.

SOLVED! Here you go fellas, cheers

Code:
$config['source_image'] = "/some/path/my_orig_img.jpg";
$config['maintain_ratio'] = false;

$target_aspect_ratio = $target_w / $target_h;
                            
if($w / $target_w >= $h / $target_h)
{
    $new_h = $h;
    $new_w = (int) $h * $target_aspect_ratio;
    $new_w_offset = (int) ($w - $new_w) / 2;
    $new_h_offset = 0;
}
else
{
    $new_w = $w;
    $new_h = (int) $w / $target_aspect_ratio;
    $new_h_offset = (int) ($h - $new_h) / 2;
    $new_w_offset = 0;
}
                            
// Crop it down to match our target aspect ratio
//
$config['width'] = $new_w;
$config['height'] = $new_h;
$config['x_axis'] = $new_w_offset;
$config['y_axis'] = $new_h_offset;
$config['new_image'] = "/some/path/my_new_img.jpg";
                            
$this->image_lib->initialize($config);
$this->image_lib->crop();
$this->image_lib->clear();
                            
// resize it
//
$config['source_image'] = "/some/path/my_new_img.jpg";
$config['width'] = $target_w;
$config['height'] = $target_h;
                            
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();

#9
[eluser]Bluemill Media[/eluser]
Here's a decent alternative to use until somebody takes the time to solve this problem and update the internal Image Library.

http://www.phpclasses.org/browse/package/2728.html

#10
[eluser]funkmyer[/eluser]
[quote author="Bluemill Media" date="1205842207"]Here's a decent alternative to use until somebody takes the time to solve this problem and update the internal Image Library.

http://www.phpclasses.org/browse/package/2728.html[/quote]

Fixed see above Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.