如何在 CodeIgniter 中压缩图像,但不改变其尺寸?
我有一个用户可以上传图像的网站。我直接处理这些图像,并使用 CodeIgniter Image Manipulation 类将它们调整为 5 种附加格式。我这样做非常有效,如下所示:
- 我总是从以前的格式调整大小,而不是从原始格式
- 调整大小,我使用 90% 的图像质量调整大小,这大约使 jpeg 的文件大小减半
以上我在得到建议后实施的做法来自我问的另一个问题。我的测试用例是 RGB 模式下的 1.6MB JPEG,高分辨率为 3872 x 2592。对于该图像(属于边界情况),调整大小过程总共需要大约 2 秒,这对我来说是可以接受的。
现在,只剩下一个挑战了。我希望使用 90% 的质量来压缩原始文件,但不调整其大小。这个想法是该文件也将占用文件大小的一半。我想我可以简单地将其大小调整为当前尺寸,但这似乎对文件或其大小没有任何影响。这是我的代码,稍微简化了:
$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();
上面的代码适用于除原始格式之外的所有格式。我尝试调试 CI 类,看看为什么没有任何反应,我注意到脚本检测到尺寸没有改变。接下来,它只是复制该文件而不对其进行任何处理。我评论了那段代码以强制其调整大小,但现在仍然没有任何反应。
有谁知道如何使用 CI 类将图像(任何图像,不仅仅是 jpeg)压缩到 90% 而不改变尺寸?
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. 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
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.
Now, only one challenge remains. I want the original file to be compressed using that 90% quality but without resizing it. 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. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你可以这样做:
然后设置以下选项:
好的,所以这不起作用,因为 CI 试图变得聪明,我认为你有三个可能的选项:
DIY 方法非常简单,我知道您不想使用“自定义”功能,但看一下:
正如您所看到的,它比使用更简单CI。
PS:上面的代码片段可以打开任何类型的图像(GIF、PNG 和 JPEG),并且它始终将图像保存为 JPEG,质量为 90%,我相信这就是您要存档的内容。
I guess you could do something like this:
And then set the following options like this:
Ok, so that doesn't work due to CI trying to be smart, the way I see it you've three possible options:
The DIY approach is really simple, I know you don't want to use "custom" functions but take a look:
As you can see, it's even more simpler than using 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.