resize() 无法使用图像操作类

发布于 2024-09-09 08:13:17 字数 667 浏览 3 评论 0原文

我一直在阅读文档并尝试一切方法来从上传的图像中取出拇指,但我似乎找不到问题。

图像已正确上传并保存,但拇指则不然,失败且没有错误输出。 这是我正在使用的代码:

$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 技术交流群。

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

发布评论

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

评论(5

眼藏柔 2024-09-16 08:13:17

我知道这有点太晚了,但我最近遇到了同样的问题,并通过初始化类来解决它。

CodeIgniter 示例不起作用,您必须先加载该类:

$this->load->library('image_lib');

然后在设置所需的配置后,然后像这样初始化该类:

$this->image_lib->initialize($config);

这立即对我有用。

我发布此内容是为了防止其他人遇到同样的问题,因此他们使用 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:

$this->load->library('image_lib');

Then after setting the config you need, then you initialize the class like so:

$this->image_lib->initialize($config);

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.

时间海 2024-09-16 08:13:17

也许宽度和高度都必须指定?只需尝试使用固定高度(并在第二步中进行纵横比感知缩放)。

为了保持比例,很简单,首先计算图像的纵横比,然后计算目标高度:

$target_width = 750;
$image_aspect_ratio = $image_width / $image_height;
$target_height = $target_width / $image_aspect_ratio;

您可以从简单的数学中推导出来: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:

$target_width = 750;
$image_aspect_ratio = $image_width / $image_height;
$target_height = $target_width / $image_aspect_ratio;

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).

薄荷→糖丶微凉 2024-09-16 08:13:17

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.

谎言月老 2024-09-16 08:13:17

删除 $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.

行雁书 2024-09-16 08:13:17

简单的问题,文件权限是否正确?另一个技巧是将 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.

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