我有一段代码,我通过Codeigniter gd2库压缩图像。但是,它会变成这张照片
进入这个
因为我在代码中设置了宽度和高度。但是,当我删除这两行时,$config['quality']
属性不起作用。如何解决此问题并保存压缩图像的实际大小?
这是我的代码:
$image_datar = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = './assets/img/single_courses/'.$image_datar["file_name"];
$config['maintain_ratio'] = false;
$config['quality'] = '60%';
$config['width'] = '750';
$config['height'] = '500';
$config['new_image'] = './assets/img/single_courses/'.$image_datar["file_name"];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$post_image = $image_datar["file_name"];
答案 0 :(得分:0)
即使在阅读了评论之后你想要完成的事情也有点令人困惑,而你的代码却指定了宽度。 如果你希望它是750x500,那么具有相同的比例(因此事情看起来并不“压缩”),而不是你应该产生的$config['maintain_ratio'] = true;
:
如果你只是想降低图像的质量,使原始图像的文件大小更小但保持相同的尺寸,你会认为你可以注释掉它的宽度和高度。配置。但是,在我的测试中,即使将质量降低到10%,也会产生与原始图像相同的精确文件大小。奇怪,虫子?我用filesize()
和我的操作系统证实了这一点!
原来不是错误,而是编码功能:
// If the target width/height match the source, AND if the new file name is not equal to the old file name
// we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
if ($this->dynamic_output === FALSE && $this->orig_width === $this->width && $this->orig_height === $this->height)
{
if ($this->source_image !== $this->new_image && @copy($this->full_src_path, $this->full_dst_path))
{
chmod($this->full_dst_path, $this->file_permissions);
}
return TRUE;
}
我试图想办法解决这个问题,但这需要覆盖和搞乱库的功能。现在,如果您的图像宽度和高度比原始图像低1px,那么您可以执行以下操作:
$source = $this->frontend_image_upload_path . 'org/upload_original.jpg';
list($width, $height) = getimagesize($source);
$config['image_library'] = 'gd2';
$config['source_image'] = $source;
//$config['maintain_ratio'] = true;
$config['quality'] = '20%';
$config['width'] = $width - 1;
$config['height'] = $height - 1;
$config['new_image'] = $this->frontend_image_upload_path . 'org/new_image.jpg';
$this->load->library('image_lib', $config);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}