image cropping with codeigniter no GD library - GD

2019-08-19 03:00发布

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.

1条回答
小情绪 Triste *
2楼-- · 2019-08-19 03:32

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.

查看更多
登录 后发表回答