我们正在创建一个允许图像上传的应用程序,现在我们想减小图像大小以减少页面加载时间,现在这是我正在使用的代码,它不起作用,图像大小保持 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
}
}
我正在使用代码点火器 3.0
你应该像这样使用没有%
作为整数:
$config['quality'] = 50;
$config['image_library'] = 'gd2';
$config['quality'] = 60;
效果很好,调整质量并检查它保存的图像文件大小。 会有所不同。
在质量更改开始工作之前,我还必须将image_library
参数设置为"gd"。我想知道为什么它不适用于"gd2"
$config['image_library'] = 'gd';
$config['quality'] = 40;
我发现这不适合我使用 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();
}