Welcome Guest, Not a member yet? Register   Sign In
Image Manipulation Class question
#1

[eluser]Boris Strahija[/eluser]
I'm not sure if this is possible with the IMC in CI. What I need is to create a thumb for a jpeg photo.
This is one of the possible situations:
- upload a photo with the dimmensions 800x600
- create the thumb with the max dimmensions 140x140

This all works great, except I would need to set the x and y axis for the thumb cutout. It should exactly in the middle. The problem is that not every image will be the same size. Is this even possible with the IMC in CI?
#2

[eluser]TheFuzzy0ne[/eluser]
I might be misunderstanding what you're saying, but I don't understand why images won't be the same size if you're cropping them to 140x140. If you can post some code, I might be able to make a more suitable suggestion.
#3

[eluser]Dam1an[/eluser]
If you want to make a thumbnail, why are you cropping it instead if just scaling?
If you want the middle 140x140 of any size image all you need to do is
- Get the width and height from the database (which you stored when the image was uploaded)
- Do (height - 140) / 2 for the y axis crop
- Do (width - 140) / 2 for the x axis crop
- Do the rest as normal

Or am I totally missing something (having never used the IMC, quite probably)
#4

[eluser]Boris Strahija[/eluser]
The original images wont have the same dimmensions. The thumbs should all be 140x140.
The problem is that the thumb is being cut out on the position 0pxx0px (top/left), but I need it to be in the middle.

I think know the image (800x600) is being resized to 186x140, and the the uper portion is being cut out. I need it to be for this image 23x0.
I can do this manually, but maybe there's an option in the class itself.

I hope it's clearer now Smile
#5

[eluser]Boris Strahija[/eluser]
Here's the example code.

Code:
$config['image_library']     = 'gd2';
$config['source_image']        = BASEPATH.'../'.$media['system_path'].$media['filename'];
$config['maintain_ratio']     = TRUE;
$config['create_thumb']     = TRUE;
$config['thumb_marker']     = '_th';
$config['width']             = 140;
$config['height']            = 140;

$this->image_lib->initialize($config);
            
$this->image_lib->resize();

I now just the x and y axis. I know I can do it manualy, it would be nice though to have something like this Smile
Code:
$config['x_axis']             = '50%';
$config['y_axis']            = '50%';
#6

[eluser]Dam1an[/eluser]
huh, now I'm confused, in your orignal thread, you're talking about cropping, and now you're using resize instead of crop in your code :-S
#7

[eluser]JoostV[/eluser]
See if I get this: You want to create square thumbnails that are cropped from the center of the original image?

For best results, you would crop first, and resize later. Pass the original width and height. I do not use $config['create_thumb'] myself.
Code:
// Set up cropping and load library
if ($original_width > $original_height) { // Crop left & right
    $config['width'] = $original_height;
    $config['height'] = $original_height;
    $config['x_axis'] = ($original_width - $config['width']) / 2;
}
else { // crop top & bottom
    $config['width'] = $original_width;
    $config['height'] = $original_width;
    $config['y_axis'] = ($original_height - $config['height']) / 2;
}
$config['image_library'] = 'GD2';
$config['maintain_ratio'] = false;
$this->image_lib->initialize($config);

// Crop image
if (! $this->image_lib->crop()) {
    show_error($this->image_lib->display_errors());
}

// Clear lib so we can do some more image processing
$this->image_lib->clear();
Use these settings to create a square image that you can resize later on. (This is some code I filtered out of one of my libs - did not test it in its current state)
#8

[eluser]Boris Strahija[/eluser]
[quote author="Dam1an" date="1242660522"]huh, now I'm confused, in your orignal thread, you're talking about cropping, and now you're using resize instead of crop in your code :-S[/quote]
I see now I pasted the wrong part of the code Smile
#9

[eluser]Boris Strahija[/eluser]
@JoostV thanks for the code. So I guess I have to do it manually.
#10

[eluser]JoostV[/eluser]
Not really Smile The code calculates the proper cropping settings
Code:
if ($original_width > $original_height) { // Landscape: Crop left & right
    $config['width'] = $original_height;
    $config['height'] = $original_height;
    $config['x_axis'] = ($original_width - $config['width']) / 2;
}
else { // Portrait: crop top & bottom
    $config['width'] = $original_width;
    $config['height'] = $original_width;
    $config['y_axis'] = ($original_height - $config['height']) / 2;
}




Theme © iAndrew 2016 - Forum software by © MyBB