Welcome Guest, Not a member yet? Register   Sign In
Image Manipulation, resize working, crop not
#1

[eluser]lefrog[/eluser]
My code is working for resize but for some reason the crop is being ignored.

here's my image functions in my model

Code:
function resize_width($path, $width, $height)
        {
            // resize the image to a width but maintain the ratio
            $rules['image_library'] = 'GD2';
            $rules['source_image'] = $path;
            $rules['maintain_ratio'] = TRUE;
            $rules['master_dim'] = 'width';
            $rules['width'] = $width;
            $rules['height'] = $height;
            
            $this->load->library('image_lib', $rules);

            if ( ! $this->image_lib->resize())
            {
                die($this->image_lib->display_errors());
            }
        }


        function crop_image($source, $x, $y)
        {
            $rules['image_library'] = 'GD2';
            $rules['source_image']    = $source;
            $rules['x_axis'] = $x;
            $rules['y_axis'] = $y;
            $this->image_lib->clear();
            $this->image_lib->initialize($rules);
            if ( ! $this->image_lib->crop())
            {
                die($this->image_lib->display_errors());
            }
        }

And this is my controller

Code:
function add_staff()
        {
            $data = array(
                    'name' => $this->input->post('name'),
                    'job_title' => $this->input->post('job_title'),
                    'description' => $this->input->post('details')
                    );
            $this->db->insert('staff', $data);
            $last_id = mysql_insert_id();
            
            $file_name = $_FILES['userfile']['name'];
            if(!$file_name == "")
            {
                // set the image path and name file
                $update_path = './global_assets/images/staff/';
                $img_name = $last_id . ".jpg";
                // load the image model
                $this->load->model('image');
                // call upload image function
                $this->image->upload_image($update_path, $img_name);
                // resize if required
                list($width, $height) = getimagesize($update_path . $img_name);
                $file_path = $update_path . $img_name;
                if ($width > 113)
                {
                    $this->image->resize_width($file_path, 113, 76);
                }
                if ($height > 76) {
                    $this->image->crop_image($file_path, 60, 50);
                }
            }
            redirect("admin/edit_staff_list/");
        }

Hope someone can spot something. It's doing me head in.
#2

[eluser]Michael Wales[/eluser]
Nevermind - I forgot variable scope, this would have no effect.

Quote:Try unsetting the $rules array prior to calling the crop_image function.
#3

[eluser]lefrog[/eluser]
Yeah I tried calling the array a different name in the crop_image function.

Cheers anyhow.
#4

[eluser]lefrog[/eluser]
I can get the crop_image function to crop by changing the image library to imagemagick. My server company won't have imagemagick installed on the boxes so I can use it locally but not in my application.

So this shows it must be something to do with GD2. Anyone have any ideas.

Code:
function crop_image($source, $x, $y)
        {
            /*
            $rules['image_library'] = 'GD2';
            $rules['source_image']    = $source;
            $rules['x_axis'] = '76';
            $rules['y_axis'] = '76';
            $this->image_lib->clear();
            $this->image_lib->initialize($rules);
            if ( ! $this->image_lib->crop())
            {
                die($this->image_lib->display_errors());
            }
            die($source);
            */
                $rules = array();
                $rules['image_library'] = 'imagemagick';
                $rules['library_path'] = '/usr/local/bin';
                $rules['source_image']    = $source;
                $rules['x_axis'] = $x;
                $rules['y_axis'] = $y;
                $this->image_lib->initialize($rules);
                if ( ! $this->image_lib->crop())
                {
                    die($this->image_lib->display_errors());
                }
        }
#5

[eluser]lefrog[/eluser]
Stayed up late last night and tried all manner of things to get the crop function to work in GD2 to no avail.

There must be something simple I'm missing as it works when I use imagemagick. ARRRGGGGHHHHH




Theme © iAndrew 2016 - Forum software by © MyBB