【问题标题】:image cropping with codeigniter no GD library - GD2 is intalled使用 codeigniter 进行图像裁剪,没有 GD 库 - 安装了 GD2
【发布时间】:2012-02-01 17:38:10
【问题描述】:

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

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

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。

【问题讨论】:

  • GD2 可能已安装,但编译时是否支持 PNG?仅仅因为有 GD 并不意味着它可以处理所有常见的文件格式。
  • 您是否尝试过文档根目录的绝对路径?试试$_SERVER['DOCUMENT_ROOT'] . 'rest/of/path/to/file'
  • GD_INFO 会告诉它支持什么吗?我以前在这台机器上裁剪过 PNG。
  • 试过 DOCUMENT_ROOT 没有成功

标签: php codeigniter codeigniter-2 crop gd2


【解决方案1】:

这很可能只是文件路径问题(CI 非常适合准确、相关的错误消息)。以下是我将采取的解决方法:

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

狩猎愉快。

【讨论】:

  • file_exists 返回 TRUE,还有其他想法吗?
  • 真的很困惑。也许在相关的图像处理函数中放置一些逐步调试输出,以找到它到达错误状态的位置?
  • 想通了,我是提交表单到controller/view,但是$this->data['image']是由URI段构造的,所以需要提交表单到current_url()
猜你喜欢
  • 1970-01-01
  • 2014-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-06
  • 2020-11-22
  • 2011-11-16
相关资源
最近更新 更多