resize() 无法使用图像操作类
我一直在阅读文档并尝试一切方法来从上传的图像中取出拇指,但我似乎找不到问题。
图像已正确上传并保存,但拇指则不然,失败且没有错误输出。 这是我正在使用的代码:
$data = $this->upload->data();
$config_image['image_library'] = 'gd';
$config_image['source_image'] = $data['full_path'];
$config_image['new_image'] = 'uploads/thumbs/';
$config_image['create_thumb'] = TRUE;
$config_image['maintain_ratio'] = TRUE;
$config_image['width'] = 750;
$this->load->library('image_lib', $config_image);
if ( !$this->image_lib->resize())
{
$this->session->set_flashdata('message', $this->image_lib->display_errors());
}
另外,我想调整图像大小以适合 max-width=750,但保持比例。我为实现这一目标所做的事情正确吗? 谢谢!
I’ve been reading the docs and trying everything to make thumbs out of uploaded images but I can’t seem to find the problem.
Images are uploaded and saved correctly but thumbs are not, it fails with no error output.
This is the code I’m using:
$data = $this->upload->data();
$config_image['image_library'] = 'gd';
$config_image['source_image'] = $data['full_path'];
$config_image['new_image'] = 'uploads/thumbs/';
$config_image['create_thumb'] = TRUE;
$config_image['maintain_ratio'] = TRUE;
$config_image['width'] = 750;
$this->load->library('image_lib', $config_image);
if ( !$this->image_lib->resize())
{
$this->session->set_flashdata('message', $this->image_lib->display_errors());
}
Also, I want to resize images to fit max-width=750, but maintain the ratio. Is what I’m doing correct to achieve that?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我知道这有点太晚了,但我最近遇到了同样的问题,并通过初始化类来解决它。
CodeIgniter 示例不起作用,您必须先加载该类:
然后在设置所需的配置后,然后像这样初始化该类:
这立即对我有用。
我发布此内容是为了防止其他人遇到同样的问题,因此他们使用 CodeIgniter 而不是其他库来解决它。
I know this is a little too late, but I recently had the same issue and solve it by initializing the class.
The CodeIgniter example does not work, you have to load the class first:
Then after setting the config you need, then you initialize the class like so:
This worked for me right away.
I'm posting this in case anyone else is having the same issue, so they solve it with CodeIgniter and not another library.
也许宽度和高度都必须指定?只需尝试使用固定高度(并在第二步中进行纵横比感知缩放)。
为了保持比例,很简单,首先计算图像的纵横比,然后计算目标高度:
您可以从简单的数学中推导出来:r = w/h。因此,w=r*h(=目标宽度,如果您有固定高度)和 h = w/r(=目标高度,如果您有固定宽度)。
Maybe both, width AND height must be specified? Just try it with a fixed height (and do aspect ratio aware scaling in a second step).
For maintaining the ratio, its simple, first compute the aspect ratio of the image, then compute the target height:
You can deduce this from simple math: r = w/h. So, w=r*h (=the target width, if you have a fixed height) and h = w/r (=the target height, if you have a fixed width).
你一定要用GD2。
CI的图像处理类有时有点脾气暴躁。如果您无法正常工作,请查看 timthumb< /a>.这是一个小型 1 文件 php 脚本,可以动态调整图像大小并使用缓存系统。您只需通过图像标签的 src 属性传递所需的参数即可。非常简单,工作得很好,而且比 CI 的图像处理类要少得多。
You definitely have to use GD2.
CI's image manipulation class is a bit temperamental sometimes. If you can not get things to work, check out timthumb. It's a small 1 file php script that resizes images on the fly and uses a caching system. You just pass the required paramters through the src attribute of your image tag. Very easy, works very well and much less of a headache than CI's image manipulation class.
删除
$config_image['new_image'] = 'uploads/thumbs/';
并将$config['image_library'] = 'gd';
更改为$config ['image_library'] = 'gd2';
还要确保 $data 数组包含 full_path。
remove
$config_image['new_image'] = 'uploads/thumbs/';
and change$config['image_library'] = 'gd';
to$config['image_library'] = 'gd2';
Also make sure the $data array contains the full_path.
简单的问题,文件权限是否正确?另一个技巧是将 CI 错误日志记录设置为最高级别,看看它是否输出任何内容。
Simple question, are the file permissions correct? Another tip would be to set CIs error logging to the highest leven and see if it outputs anything.