如何在 CodeIgniter 中压缩图像,但不改变其尺寸?

发布于 2024-08-16 09:06:56 字数 1130 浏览 2 评论 0原文

我有一个用户可以上传图像的网站。我直接处理这些图像,并使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪操控生活 2024-08-23 09:06:56

我想你可以这样做:

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

然后设置以下选项:

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

好的,所以这不起作用,因为 CI 试图变得聪明,我认为你有三个可能的选项:

  • 旋转图像360°
  • 为图像添加水印(使用 1x1 透明图像)
  • 自己动手

DIY 方法非常简单,我知道您不想使用“自定义”功能,但看一下:

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

正如您所看到的,它比使用更简单CI。


PS:上面的代码片段可以打开任何类型的图像(GIF、PNG 和 JPEG),并且它始终将图像保存为 JPEG,质量为 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:

  • Rotate the Image by 360º
  • Watermark the Image (with a 1x1 Transparent Image)
  • Do It Yourself

The DIY approach is really simple, I know you don't want to use "custom" functions but take a look:

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.


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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文