Welcome Guest, Not a member yet? Register   Sign In
Square cropping from the center, is there an easier way? RESOLVED
#1

[eluser]JoostV[/eluser]
Hi,

I need to generate square thumbs for an image gallery. The cropping has to be done from the center. E.g. horizontal cropping would take a bit of the left side and off the right side of the image.

I tried using negative values for cropping, but it didn't work.

I found a solution for now, but I am not happy with it. I am sure there must be an easier way.

The way I do it now:
* Flip the image using $this->image_lib->rotate() ('hor' for cropping on x-axis, 'vrt' for cropping on y-axis)
* Crop from one side using $this->image_lib->crop()
* Flip again
* Crop from the other side

Anyone know a better solution?

Tnx in advance.
#2

[eluser]Référencement Google[/eluser]
Your way to do it is too complex.

Look at this thread:
http://ellislab.com/forums/viewthread/73906/

I think the best is to get first the image properties (width and height), you can use something like :
Code:
// Get the Image properties (this function is not documented in CI manual)
$imageSize = $this->image_lib->get_image_properties($config['source_image'], TRUE);

Then the next is mathematics, divide by 2 the width and height and add to this the width and height of the desired cropped size. If you don't find, please post some code here.
#3

[eluser]JoostV[/eluser]
Ah I see!

The trick is to set width and height and adding $config['maintain_ratio'] = false;

I never tried this because, according to the User Guide, maintain_ratio is not used by $this->image_lib->crop();

Now it's just a matter of one or two extra lines of code. Thanks very much!

Code:
//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);

$fileData is result array from uploading image using libary File Upload




Theme © iAndrew 2016 - Forum software by © MyBB