php codeigniter 调整图像大小并创建缩略图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11504350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
codeigniter resize image and create thumbnail
提问by max
hi according to the ci document you can resize images with image_lib and there are options that suggest we can create additional thumbnail from that image
嗨,根据 ci 文档,您可以使用 image_lib 调整图像大小,并且有选项建议我们可以从该图像创建其他缩略图
create_thumb FALSE TRUE/FALSE (boolean) Tells the image processing function to create a thumb. R
thumb_marker _thumb None Specifies the thumbnail indicator. It will be inserted just before the file extension, so mypic.jpg would become mypic_thumb.jpg R
so here is my code
所以这是我的代码
$config_manip = array(
'image_library' => 'gd2',
'source_image' => "./uploads/avatar/tmp/{$this->input->post('new_val')}",
'new_image' => "./uploads/avatar/{$this->input->post('new_val')}",
'maintain_ratio'=> TRUE ,
'create_thumb' => TRUE ,
'thumb_marker' => '_thumb' ,
'width' => 150,
'height' => 150
);
$this->load->library('image_lib', $config_manip);
$this->image_lib->resize();
i would assume this code resizes my image and also creates a thumbnail , but i only get one image with specified dimensions and _tump postfix
我假设这段代码调整了我的图像大小并创建了一个缩略图,但我只得到了一个具有指定尺寸和 _tump 后缀的图像
i've also tried to add this code to create second image manually but still it doesn't work and i get only one image
我也尝试添加此代码以手动创建第二张图片,但仍然无法正常工作,而且我只得到一张图片
$this->image_lib->clear();
$config_manip['new_image'] =
"./uploads/avatar/thumbnail_{$this->input->post('new_val')}";
$config_manip['width'] = 30 ;
$config_manip['height'] = 30 ;
$this->load->library('image_lib', $config_manip);
$this->image_lib->resize();
回答by Madan Sapkota
It seems path is the issue in your code. I modified and tested myself it works.
似乎路径是您代码中的问题。我自己修改并测试了它的工作原理。
public function do_resize()
{
$filename = $this->input->post('new_val');
$source_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/avatar/tmp/' . $filename;
$target_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/avatar/';
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => TRUE,
'create_thumb' => TRUE,
'thumb_marker' => '_thumb',
'width' => 150,
'height' => 150
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
// clear //
$this->image_lib->clear();
}
Hope this helps you. Thanks!!
希望这对你有帮助。谢谢!!
回答by shah
Your code is Okay but you need to do a small change.
你的代码没问题,但你需要做一个小的改变。
$this->load->library('image_lib');
$this->image_lib->initialize($config_manip);
回答by Alexandru Sirbu
A simple way to create a thumbnail.
创建缩略图的简单方法。
function _create_thumbnail($fileName, $width, $height)
{
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = $_SERVER['DOCUMENT_ROOT']. $fileName;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['new_image'] = $_SERVER['DOCUMENT_ROOT']. $fileName;
$this->image_lib->initialize($config);
if (! $this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
}
回答by aphoe
If you want to create more than one image using resize()method, you need to call $this->image_lib->initialize($config);each time you attempt a resize.
如果要使用resize()方法创建多个图像,则$this->image_lib->initialize($config);每次尝试调整大小时都需要调用。
This tutorial solved it for me Upload Image and Create Multiple Thumbnail Sizes in CodeIgniter
本教程为我解决了在 CodeIgniter 中上传图像和创建多个缩略图大小的问题

