resize() not working using Image manipulation class

半腔热情 提交于 2019-12-13 07:25:01

问题


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!


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/3224315/resize-not-working-using-image-manipulation-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!