使用 codeigniter 进行图像裁剪,没有 GD 库 - 安装了 GD2
我目前正在为一个网站开发媒体库,其功能之一是用户可以创建他们上传的图像的裁剪版本。
然而我的问题是,当我尝试裁剪图像时,出现以下错误,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很可能只是一个文件路径问题(CI 在准确、相关的错误消息方面非常出色)。以下是我解决该问题所采取的步骤:
var_dump(file_exists($config['source_image'])
来查看 PHP 是否找到该文件。这里的“true”响应会让我感到震惊 检查$_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:
var_dump(file_exists($config['source_image'])
to see if PHP finds the file. I'd be shocked by a "true" response here.$_SERVER['DOCUMENT_ROOT']
, as a commenter suggests.FC_PATH
(or other CI-defined constants) should get you the base path you need.Happy hunting.