简体   繁体   English

如何压缩CodeIgniter中的图像,但不改变它们的尺寸?

[英]How to compress images in CodeIgniter, yet do not change their dimensions?

I have a site where users can upload images. 我有一个用户可以上传图片的网站。 I process these images directly and resize them into 5 additional formats using the CodeIgniter Image Manipulation class. 我直接处理这些图像,并使用CodeIgniter Image Manipulation类将它们调整为另外5种格式。 I do this quite efficiently as follow: 我这样做非常有效,如下:

  • I always resize from the previous format, instead of from the original 我总是从以前的格式调整大小,而不是从原始格式调整大小
  • I resize using an image quality of 90% which about halves the file size of jpegs 我使用90%的图像质量调整大小,大约将jpeg的文件大小减半

The above way of doing things I implemented after advise I got from another question I asked. 我提出的上述做事方式是在我提出的另一个问题后提出来的。 My test case is a 1.6MB JPEG in RGB mode with a high resolution of 3872 x 2592. For that image, which is kind of borderline case, the resize process in total takes about 2 secs, which is acceptable to me. 我的测试用例是RGB模式下的1.6MB JPEG,高分辨率为3872 x 2592.对于那种边缘情况的图像,调整大小过程总共需要2秒钟,这对我来说是可以接受的。

Now, only one challenge remains. 现在,只剩下一个挑战了。 I want the original file to be compressed using that 90% quality but without resizing it. 我希望使用90%的质量压缩原始文件,但不需要调整大小。 The idea being that that file too will take half the file size. 想法是该文件也将占用文件大小的一半。 I figured I could simply resize it to its' current dimensions, but that doesn't seem to do anything to the file or its size. 我想我可以简单地将其调整为其当前尺寸,但这似乎对文件或其大小没有任何作用。 Here's my code, somewhat simplified: 这是我的代码,有点简化:

$sourceimage = "test.jpg";
$resize_settings['image_library'] = 'gd2';
$resize_settings['source_image'] = $sourceimage;
$resize_settings['maintain_ratio'] = false;
$resize_settings['quality'] = '90%';
$this->load->library('image_lib', $resize_settings);

$resize_settings['width'] = $imagefile['width'];
$resize_settings['height'] = $imagefile['height'];
$resize_settings['new_image'] = $filename;
$this->image_lib->initialize($resize_settings);
$this->image_lib->resize();

The above code works fine for all formats except the original. 上述代码适用于除原始格式之外的所有格式。 I tried debugging into the CI class to see why nothing happens and I noticed that the script detects that the dimensions did not change. 我尝试调试CI类,看看为什么没有发生,我注意到脚本检测到尺寸没有改变。 Next, it simply makes a copy of that file without processing it at all. 接下来,它只是制作该文件的副本而根本不处理它。 I commented that piece of code to force it to resize but now still nothing happens. 我评论了一段代码来强制它调整大小但现在仍然没有任何反应。

Does anybody know how to compress an image (any image, not just jpegs) to 90% using the CI class without changing the dimensions? 有没有人知道如何在不改变尺寸的情况下使用CI类将图像(任何图像,而不仅仅是jpegs)压缩到90%?

I guess you could do something like this: 我想你可以这样做:

$original_size = getimagesize('/path/to/original.jpg');

And then set the following options like this: 然后设置以下选项,如下所示:

$resize_settings['width'] = $original_size[0];
$resize_settings['height'] = $original_size[1];

Ok, so that doesn't work due to CI trying to be smart, the way I see it you've three possible options: 好吧,因为CI试图变得聪明,所以这不起作用,我认为你有三种可能的选择:

  • Rotate the Image by 360º 将图像旋转360º
  • Watermark the Image (with a 1x1 Transparent Image) 为图像添加水印(使用1x1透明图像)
  • Do It Yourself 自己做

The DIY approach is really simple, I know you don't want to use "custom" functions but take a look: DIY的方法很简单,我知道你不想使用“自定义”功能,但看看:

ImageJPEG(ImageCreateFromString(file_get_contents('/path/to/original.jpg')), '/where/to/save/optimized.jpg', 90);

As you can see, it's even more simpler than using CI. 如您所见,它比使用CI更简单。


PS: The snippet above can open any type of image (GIF, PNG and JPEG) and it always saves the image as JPEG with 90% of quality, I believe this is what you're trying to archive. PS:上面的代码段可以打开任何类型的图像(GIF,PNG和JPEG),它总是以90%的质量保存图像为JPEG,我相信这就是您要存档的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM