使用 codeigniter 进行图像裁剪,没有 GD 库 - 安装了 GD2

发布于 2025-01-02 07:29:17 字数 2072 浏览 0 评论 0原文

我目前正在为一个网站开发媒体库,其功能之一是用户可以创建他们上传的图像的裁剪版本。

然而我的问题是,当我尝试裁剪图像时,出现以下错误,

The path to the image is not correct.

这是我的代码,

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

所以我想将 $config['source_image'] 更改为以下内容,

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

但是留下的信息是:

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

我是不是做错了什么?我只是想裁剪一个简单的 .png,该文件肯定存在于我的服务器上,而且我肯定已经安装了 GD2。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离鸿 2025-01-09 07:29:17

这很可能只是一个文件路径问题(CI 在准确、相关的错误消息方面非常出色)。以下是我解决该问题所采取的步骤:

  1. var_dump(file_exists($config['source_image']) 来查看 PHP 是否找到该文件。这里的“true”响应会让我感到震惊 检查
  2. 如果文件系统区分大小写,请确保这不是问题
  3. 包含路径(我认为如果 CI 到目前为止,常规包含工作正常...)
  4. 更多的是反步骤,但不要使用$_SERVER['DOCUMENT_ROOT'],正如评论者所建议的那样,FC_PATH(或其他 CI 定义的常量)应该可以为您提供所需的基本路径

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文