Welcome Guest, Not a member yet? Register   Sign In
Problem cropping image with image_lib library
#1

[eluser]luismartin[/eluser]
I'm trying to crop a rectangular image (1000 x 400 px) so that I get rid of about a fith part of its width from both sides, while keeping all of its height. Thus getting a centered frame.

As far as I know, based in the sparse documentation of CI on this, I had to firstly get rid of the left edge:

Code:
$config['source_image'] = $src;
$config['x_axis'] = 200;
$config['y_axis'] = 0;
$this->image_lib->initialize($config);
$this->image_lib->crop();
$this->image_lib->clear();

Then, flip horizontally and next remove the right portion by repeating the first process:

Code:
$config['source_image'] = $src;
$config['rotation_angle'] = 'hor';
$this->image_lib->initialize($config);
$this->image_lib->rotate();
$this->image_lib->clear();

// Code for cropping again...

And flip again to turn it back to the initial state:

Code:
// Code for flipping again...

I'm using every single operation in a function, so note that $config is not the same variable for each one, and every operation should be completely independent.

The resulting image is this:

The right side is actually cropped. However the left side, instead of being cropped, has been displaced to the right by a black background stripe. So, the resulting width is just the same as the original picture, with the difference that looks like it has been displaced to the right, moving the rightmost portion outside, and covering the left portion with black.

What am I missing?
#2

[eluser]aquary[/eluser]
I had done something like that as my custom library. I'm not quite sure what is wrong there (hadn't read throughly), but you could use only one crop to do the job with some crop setting like...

Code:
$crop_setting=array(
'source_image'=>$src,
'x_axis'=>200, // Let say this is (image_width-new_width)/2 which is (1000-600)-2
'y_axis'=>0, // The same thing could be apply here, but to follow you code, 0
'width'=>600, // Width after cropping, in your case, 600
'maintain_ratio'=>FALSE // maybe not required, just something from my code
);

It was working on my case (which has more stuff about crop horizontally, vertically, etc.), so it should work on yours

If you want to check what is wrong whit your code, you could check step by step:
1. exit(); after the first cropping, and check if the image is cropped correctly.
2. If(1) is OK, then exit(); after the flipping, is it still ok?
3. If(2) is also ok, then crop the 2nd time, exit() then check the image.
The last flipping shoulding be the problem...

Edit:
I have try your code on mine, and it seems ythe first cropping is not working. After adding width config, the cropping works.

Code:
$config['source_image'] = $src;
$config['x_axis'] = 200;
$config['y_axis'] = 0;
$config['width']=1440-200; // My picture is 1440
#3

[eluser]luismartin[/eluser]
Thank you Aquary, I finally did it using "width" and "maintain_ratio", as you suggested.

This means the CI documentation is wrong in this line, since it says "width" is not available in the crop function:

Quote:All preferences listed in the table above are available for this function except these: rotation_angle, width, height, create_thumb, new_image.

http://ellislab.com/codeigniter/user-gui...e_lib.html

Someone should correct it.

Cheers!




Theme © iAndrew 2016 - Forum software by © MyBB