Welcome Guest, Not a member yet? Register   Sign In
Problem Uploading & Resizing Square Images
#1

[eluser]BrandonDurham[/eluser]
I'm not sure why it's happening, but uploading square images breaks the following function. If they're tall or wide, it works fine. Any idea why? I appreciate any help I can get.
Code:
class Functions
{

    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;
    }

}
#2

[eluser]BrandonDurham[/eluser]
Anyone? I unfortunately need to figure this out by 2pm today and can't find the problem.
#3

[eluser]megabyte[/eluser]
I think more of us would be willing to help if we new what erros were being returned? Telling us it "breaks" really doesn't help us. Well at least it doesn't help me. Smile
#4

[eluser]BrandonDurham[/eluser]
Sorry, this is 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.
#5

[eluser]megabyte[/eluser]
Ok, first of all is the image actually making it to your server? Regardless if the resize functions work or not it should be there. If it's not then your path you are saving it to is wrong.

Secondly try echoing

$CI->config->item('live_server_path') . 'mbg/images/' . $directory . '/email/' . $data['raw_name'] . $extension;

and seeif the path makes sense.
#6

[eluser]BrandonDurham[/eluser]
The path is good. It's making it into the "originals" directory and being resized just fine the first time. I believe it's breaking after the "// Crop and make thumb" bit. It only breaks when I try to upload and image that square or almost square. I just tried to upload an image that's 1000x1009 and got the error message. Then I tried one that's 600x1000 and it worked fine.

Thanks for your help.
#7

[eluser]megabyte[/eluser]
Search all my posts, I contributed a script for multiple image resizing and a fix for memory usage issues.
#8

[eluser]JoostV[/eluser]
You could also try setting ImageMagick in stead of GD, if it's installed on your server. In my experience it's less demanding on server resources as GD.




Theme © iAndrew 2016 - Forum software by © MyBB