• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
URGENT: Image upload & resize issues

#1
[eluser]BrandonDurham[/eluser]
For some reason the below function fails when the image is only slightly taller than it is wide. I think it's breaking when it gets to the crop bit:
Code:
function uploadHeaderImage($directory) {
    $CI =& get_instance();

    // Upload image
    $config['upload_path'] = $CI->config->item('live_server_path') . 'mbg/images/' . $directory . '/originals/';
    $config['allowed_types'] = 'jpg';
    $config['max_size']    = '6000';
    $CI->load->library('upload', $config);

    if (!$CI->upload->do_upload("cover"))
    {
        echo "Original: ".$CI->upload->display_errors();
        return false;
    }

    // Create various images sizes
    $CI->load->library('image_lib');
    $data = $CI->upload->data();
    $image_path = $data['full_path'];
    $extension = $data['file_ext'];

    $config['image_library'] = 'GD2';
    $config['source_image'] = $image_path;
    $config['maintain_ratio'] = TRUE;

    // Resize image
    $config['new_image'] = $CI->config->item('live_server_path') . 'mbg/images/' . $directory . '/' . $data['raw_name'] . $extension;
    $config['width'] = 590;
    $config['height'] = 2000;
    $CI->image_lib->initialize($config);

    if (!$CI->image_lib->resize()) {
        echo $CI->image_lib->display_errors();
        return false;
    }

    // Crop and make thumb
    $img_width = $data['image_width'];
    $img_height = $data['image_height'];
    $crop_ratio = ($img_width >= $img_height) ? $img_height/150 : $img_width/130;
    $config['new_image'] = $CI->config->item('live_server_path') . 'mbg/images/' . $directory . '/email/' . $data['raw_name'] . $extension;
    $config['width'] = 130 * $crop_ratio;
    $config['height'] = 150 * $crop_ratio;
    $config['x_axis'] = ($img_width - $config['width'])/2;
    $config['y_axis'] = ($img_height - $config['height'])/2;
    $config['maintain_ratio'] = FALSE;
    $CI->image_lib->initialize($config);
    if (!$CI->image_lib->crop()) {
        echo $CI->image_lib->display_errors();
        return false;
    }

    $CI->image_lib->clear();
    $config['image_library'] = 'GD2';
    $config['source_image'] = $CI->config->item('live_server_path') . 'mbg/images/' . $directory . '/email/' . $data['raw_name'] . $extension;
    $config['width'] = 130;
    $config['height'] = 150;
    $CI->image_lib->initialize($config);
    if (!$CI->image_lib->resize()) {
        echo $CI->image_lib->display_errors();
        return false;
    }

    return $data['raw_name'] . $extension;
}

Here's the error I get:
Quote:The path to the image is not correct

Your server does not support the GD function required to process this type of image.

Here's a sample image that makes it break: http://www.amillionthingstodo.com/ci/Hys...gogram.JPG

If I make that image just 100px wder or taller it works fine. Any ideas? This is kind of urgent.

I appreciate any help I can get.

#2
[eluser]BrandonDurham[/eluser]
Any ideas?

#3
[eluser]err403_love[/eluser]
First, try changing:
Code:
$config['width'] = 130 * $crop_ratio;
$config['height'] = 150 * $crop_ratio;
$config['x_axis'] = ($img_width - $config['width'])/2;
$config['y_axis'] = ($img_height - $config['height'])/2;

to:
Code:
$config['width'] = floor(130 * $crop_ratio);
$config['height'] = floor(150 * $crop_ratio);
$config['x_axis'] = floor(($img_width - $config['width'])/2);
$config['y_axis'] = floor(($img_height - $config['height'])/2);

Because just testing out print_r for a "fake" $config array that I put into its own test file gave me:
Code:
Array ( [new_image] => fake image path [width] => 937 [height] => 1081.1538461538 [x_axis] => 0 [y_axis] => -8.5769230769231 [maintain_ratio] => )

It SEEMS that Image_lib.php already does this though (rounds), for the width and height. But you should do this at least to troubleshoot and see if it gets rid of your problem.

I can't seem to find info on whether or not the X and Y axes need to be an integer, and I can't find anywhere where the Image_lib actually rounds it, so you should definitely try this out, at least to troubleshoot.

Also, if you want, try to see if you can use $config['master_dim'] for what you're trying to do. You may be able to get rid of your own ratio code if you use that instead.

#4
[eluser]BrandonDurham[/eluser]
Thanks for the post! Unfortunately the rounding didn't take care of it.

The ratio calculations I have here are meant to crop the image from the center (instead of the top left) in the correct dimensions and then resize to 130x150.

#5
[eluser]BrandonDurham[/eluser]
Okay, so I think I figured it out.

Because each dimension is not being divided equally (w/130 and h/150) then it's not accurate to say:
Code:
$crop_ratio = ($img_width >= $img_height) ? $img_height/150 : $img_width/130;
... because I'm not dividing by the same number, so they shouldn't be treated equally.

SO, I changed it to this and it's now working correctly:
Code:
$crop_ratio = (($img_width/130) > ($img_height/150)) ? $img_height/150 : $img_width/130;
$config['width'] = floor(130 * $crop_ratio);
$config['height'] = floor(150 * $crop_ratio);

#6
[eluser]err403_love[/eluser]
Oh.. duh. Sorry. Smile

I've done crops exactly like that in other image gallery software, too, so now I feel really stupid for not noticing it.

You can remove the floor() from the height/width, too. The Image library does it more accurately by doing it last.

Glad you figured it out! Smile

#7
[eluser]BrandonDurham[/eluser]
Awesome. Thank you for your help!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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