Welcome Guest, Not a member yet? Register   Sign In
Getting black background from cropping with the image library?
#1

[eluser]Althalos[/eluser]
Hello.

I am trying to create a square thumbnail by taking an image, scaling it down to the selected width (210px) and then cropping the image as to remove what's over on the side that is longer than the other.

Here's my code:
$config['source_image'] = dirname($path).'/'.$newName.'/'.$newName.'_645.png';
list($width, $height) = getimagesize($config['source_image']);

if($width < $height)
{
$config['master_dim'] = 'width';
$height_crop = $height-210;
$width_crop = 0;
}
else
{
$config['master_dim'] = 'height';
$width_crop = $width-210;
$height_crop = 0;
}

$config['width'] = 210;
$config['height'] = 210;
$config['new_image'] = dirname($path).'/'.$newName.'/'.$newName.'_210.png';

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

$this->image_lib->resize();

//Crop the smallest image to the size 210x210
$this->image_lib->clear();

$config['x_axis'] = $width_crop/2;
$config['y_axis'] = $height_crop/2;
$config['source_image'] = dirname($path).'/'.$newName.'/'.$newName.'_210.png';

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

$config['x_axis'] = $width-$width_crop/2;
$config['y_axis'] = $height-$height_crop/2;

$this->image_lib->initialize();
$this->image_lib->crop();

I know the image I start out with is right. The image I end up with is a black square with the exact same dimensions as the image I started out with.

So,
1) Why don't the dimensions change
2) why is the image just a black rectangle, where did the photo go?

best regards
#2

[eluser]KingSkippus[/eluser]
It looks like you are calculating the crop size based on the size of the original image instead of the resized one. Here's my whack at accomplishing what you're trying to do:

Code:
$config['source_image'] = dirname($path).'/'.$newName.'/'.$newName.'_645.png';
$config['new_image'] = dirname($path).'/'.$newName.'/'.$newName.'_210.png';

list($width, $height) = getimagesize($config['source_image']);
if ($width < $height) {
    $config['master_dim'] = 'width';
    $config['x_axis'] = 0;
    $config['y_axis'] = 105 * ($height / $width - 1);
}
else {
    $config['master_dim'] = 'height';
    $config['x_axis'] = 105 * ($width / $height - 1);
    $config['y_axis'] = 0;
}
$config['width'] = 210;
$config['height'] = 210;

$this->image_lib->initialize($config);
$this->image_lib->resize();
$config['maintain_ratio'] = false;
$this->image_lib->crop();

Edit: After futzing around with the math and the code a bit more, I've shortened up the code and combined the operations.

The math is basically plugging in the resized width or height, which is 210 * A / B, into the crop width/height formula (C - 210) / 2. You get:
( 210 * A / B - 210 ) / 2
= 210 ( A / B - 1 ) / 2
= 105 ( A / B - 1 )

A and B are the width and height, depending on whether the image is taller or wider.

Give it a shot and let me know how it works.
#3

[eluser]Althalos[/eluser]
You are a genius. Thank you very much.




Theme © iAndrew 2016 - Forum software by © MyBB