Codeigniter3中的图像压缩 [英] Image compress in Codeigniter3

查看:100
本文介绍了Codeigniter3中的图像压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码,通过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’] =假;
$ 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];


解决方案

这甚至会使您试图完成的工作有些混乱阅读注释后,您的代码将指定宽度。 如果您希望将其设置为750x500(具有相同的比率(这样就不会出现压缩的情况)),而不应该具有 $ config ['maintain_ratio'] = true; 会生成:





如果您只是想降低图像质量,以使文件尺寸更小但保持原始图像的尺寸,您可能会认为您可以注释掉图像的宽度和高度配置。但是,在我的测试中,即使将质量降低到10%,也能产生与原始图像完全相同的文件大小。奇怪,虫子?我用 filesize()并在我的操作系统中确认了这一点!



结果不是错误,而是编码功能:

  //如果目标宽度/高度与源匹配,并且新文件名与旧文件不相等name 
//我们将使用新名称简单地复制原始文件...假设动态渲染已关闭。
if($ this-> dynamic_output === FALSE& $ this-> orig_width === $ this-> width&& $ this-> orig_height === $ this ->高度)
{
if($ this-> source_image!== $ this-> new_image&& @copy($ this-> full_src_path,$ this-> full_dst_path))
{
chmod($ this-> full_dst_path,$ this-> file_permissions);
}

返回TRUE;
}

我试图找到一种解决此问题的方法,但那会需要覆盖库功能并使其混乱。现在,如果您确定图片的宽度和高度比原始图片小1像素,则可以执行以下操作:

  $ 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();
}


I have a piece of code where I compress images via Codeigniter gd2 library. However, it turns this photo

into this one

because I set width and height in my code. However, when I remove these 2 lines, $config['quality'] property does not work. How can I solve this problem and save the actual size of the image compressing it?

Here is my code:

 $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"];

解决方案

It's a bit confusing what you are trying to accomplish even after reading the comments, and your code as it specifies the width. If you want it to be 750x500 by have the same ratio (so things don't look "compressed") than you should have $config['maintain_ratio'] = true; which generates:

If you just want to reduce the quality of the image so that the file size is smaller yet keep the same dimensions of the original image than one would think you can comment out the width and height of the config. However, in my testing that yielded the exact same file size as the original image, even when changing the quality down to 10%. Weird, bug? I confirmed this with filesize() and in my os!

Turns out not a bug, but a coded feature:

// 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;
}

I've tried to figure out a way to get around this but that would require overwriting and messing with the libraries functionality. For now, and if your ok with your image being 1px less in width and height than the original, you can do something like:

$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();
}

这篇关于Codeigniter3中的图像压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆