我们正在创建一个允许图片上传的应用程序,现在我们想减少图像大小以减少页面加载时间,现在这是我正在使用的代码,它不起作用,图像大小仍然是100%它不减少,请帮助我可能错过了什么或者我做错了什么
if(!empty($_FILES['image_field']['name']))
{
$config['upload_path'] = 'image_folder';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '262144';
$config['file_name'] = 'my_image_name';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('image_field'))
{
$this->session->set_flashdata('error','<div class="alert alert-warning">Something went wrong...try again...</div>');
redirect('Controller/function_name');
}
else
{
$type = array('image/png'=>'png','image/jpg'=>'jpg','image/jpeg'=>'jpg');
$config['image_library'] = 'gd2';
$config['source_image'] = base_url().'image_folder'.$config['file_name'].'.'.$type[$_FILES['image_field']['type']];
$config['quality'] = '50%';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
// save to database
}
}
我正在使用codeigniter 3.0
答案 0 :(得分:3)
你应该在没有%
的情况下使用整数:
$config['quality'] = 50;
答案 1 :(得分:0)
在质量变更开始工作之前,我还必须将image_library
参数设置为“gd”。我想知道为什么它不适用于“gd2”
$config['image_library'] = 'gd';
$config['quality'] = 40;
答案 2 :(得分:0)
$config['image_library'] = 'gd2';
$config['quality'] = 60;
可以很好地调整质量并检查它保存的图像文件大小。会有所不同。
答案 3 :(得分:0)
我发现使用 GD或GD2库对我来说不起作用。 为了影响“ 质量”,我需要使用“ ImageMagick ”库。
如文档中所述-如果您使用“ ImageMagick ”或“ NetPBM ”库-“ 您必须提供路径”:https://codeigniter.com/userguide3/libraries/image_lib.html
在我的GoDaddy服务器上,路径为“ / usr / bin ”
$config['source_image'] = $source_image;
$config['library_path'] = '/usr/bin';
$config['image_library'] = 'ImageMagick';
$config['maintain_ratio'] = TRUE;
$config['quality'] = "20%"; // Maintained the aspect ratio and dimensions but reduced the file size by about 60%
$this->image_lib->initialize($config);
if(!$this->image_lib->resize()){
echo "QUCKVIEW Re-sizing - ";
echo $this->image_lib->display_errors();
echo "Image Resize for Grooming Quickview Failed! Contact System Administrator.";
die();
}