0

I am working on media library for a website at the moment, and one of the features is that user can create a cropped version of an image that they upload.

My problem however is this, when I try and crop the image, I get the following error,

The path to the image is not correct.

Here is my code,

$config['image_library'] = 'gd2';
    $config['source_image'] = "/media/images/products/".$this->data['image'];
    //die($config['source_image']);
    $config['maintain_ratio'] = FALSE;
    $config['x_axis'] = $this->input->post('x');
    $config['y_axis'] = $this->input->post('y');
    $config['width'] = $this->input->post('w');
    $config['height'] = $this->input->post('h');
    $config['dynamic_output'] = FALSE;
    $config['create_thumb'] = TRUE;
    $this->load->library('image_lib', $config);

    if(!$this->image_lib->crop()) {
        if($this->input->post('isAjax') == "1") {
            echo json_encode($this->image_lib->display_errors());
        } else {
            $this->data['image_error'] = $this->image_lib->display_errors();
            $this->template->build('/admin/media/crop', $this->data);
        }       
    } else {
        $filename = base_url()."media/images/products/".$this->data['image'];
        $extension_pos = strrpos($filename, '.'); // find position of the last dot, so where the extension starts
        $thumb = substr($filename, 0, $extension_pos) . '_thumb' . substr($filename, $extension_pos);
        if($this->input->post('isAjax') == 1) {
            echo json_encode($success = array('message' => 'Image Cropped'));
        } else {
            $this->data['success'] = "Image Cropped";
            $this->template->build('/admin/media/crop', $this->data);
        }   
    }

So I though I would change $config['source_image'] to the following,

$config['source_image'] = "./media/images/products/".$this->data['image'];

however that just leaves with this message,

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

Am I doing something fundamentally wrong? I am only trying to crop a simple .png, and the file certainly exists on my server, and I most definatly have GD2 installed.

j0k
  • 22,600
  • 28
  • 79
  • 90
Udders
  • 6,914
  • 24
  • 102
  • 194

1 Answers1

0

The is most likely just a file path issue (CI is pretty good about accurate, relevant error messages). Here are the steps I would take to resolve it:

  1. var_dump(file_exists($config['source_image']) to see if PHP finds the file. I'd be shocked by a "true" response here.
  2. If the filesystem is case-sensitive, make sure that's not the problem
  3. Check the include path (I assume regular includes are working fine if CI gets this far...)
  4. More of an anti-step, but DON'T use $_SERVER['DOCUMENT_ROOT'], as a commenter suggests. FC_PATH (or other CI-defined constants) should get you the base path you need.

Happy hunting.

landons
  • 9,502
  • 3
  • 33
  • 46
  • file_exists return TRUE, any other thoughts? – Udders Feb 02 '12 at 10:37
  • Truly baffled. Maybe put some step-by-step debug output in the relevant image manipulation functions to find where it's arriving at the error state? – landons Feb 02 '12 at 23:05
  • figured it out, I was submitting the form,to `controller/view`, but the `$this->data['image']` was constructed from URI segments, so need to submit the form to `current_url()` – Udders Feb 03 '12 at 09:43