如何在 PHP 中优化图像?

发布于 2024-11-19 23:27:53 字数 279 浏览 3 评论 0原文

我有一个网站,用户可以在其中保存他们的个人资料图片(头像)。

我想在加载图像时对其进行一些优化。

我的意思是,这是一个头像图像,不需要全分辨率,也不需要很大的尺寸。

我能做些什么?我一直在想:

  • 调整它的大小
  • 降低质量

可能:

  • 将其转换为 GIF
  • 颜色填充为透明 PNG

有一些比 GD 更好(更简单)的库可以做到这一点?

多谢!

I've a website where users can save their profile image (avatar).

I'd like to make some optimization to the image when they load it.

I mean, it's an avatar image, doesn't need full resolution, nor great size.

What can i do? I've been thinking:

  • Resize it
  • Low the quality

Possible:

  • convert it to GIF
  • Color fill to transparent PNGs

There are some library better (simpler) than GD to do this?

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

悟红尘 2024-11-26 23:27:53

GD就是这样做的。这听起来像是一个简单的操作,但如果您要执行此操作,则需要考虑许多因素。总而言之,这最终需要数百行代码来处理所有事情。

我的建议是,尽管您可能希望调整图像大小(如果使用 JPEG,则需要进行有损重新压缩),但将其转换为 GIF 并不是一个好主意。您不知道源类型是什么,因此这样做是有问题的。

这是我推荐的流程:

1) 将图像大小调整为您的输出格式。如果需要,您也可以在此处强制裁剪纵横比。

2) 确定原始源模式:

  • 8 位索引(GIF/PNG8):另存为 PNG8(格式往往比 GIF 小)。
  • 16-24 位:另存为 JPG。质量取决于您,但 70% 是一个很好的基准。
  • 32 位 (PNG24):另存为 PNG24,注意保持透明度。

请注意,此解决方案几乎会破坏所有“动画”GIF,但是......这就是当您尝试调整动画 GIF 大小时会发生的情况。

虽然...我也强烈建议不要将其作为单阶段过程来执行并删除原始文件。这种事以后只会反咬你一口。

如今磁盘空间很便宜......最好以高质量格式存储原始文件(即使是 2K x 2K 分辨率),然后创建一个图像服务,该服务将提供您所需的分辨率/质量并缓存结果。

GD is how this is done. It sounds like a simple operation, but there are a number of factors that you really want to get right if you're going to do this. All in all, this winds up being several hundred lines of code to take care of everything.

My recommendation is that although you may wish to resize an image (which requires a lossy recompression if using JPEG), converting it to a GIF is a bad idea. You don't know what the source type is, so doing that is problematic.

Here's my recommended flow:

1) Resize the image to your output format. You can force a cropping aspect ratio here as well, if you want.

2) Determine original source mode:

  • 8 bit indexed (GIF/PNG8): Save as PNG8 (format tends to be smaller than GIF).
  • 16-24 bit: Save as JPG. Quality is up to you, but 70% is a good baseline.
  • 32 bit (PNG24): Save as PNG24, taking care to maintain the transparency.

Note, this solution pretty much destroys any 'animated' gifs, but... that's what happens when you try to resize an animated gif.

Although... I also highly recommend to NOT do this as a single stage process and removing the original files. This is the kind of thing that will only come back to bite you later.

Disk space is cheap these days... far better to store the original in a high quality format (even at 2K x 2K resolution), then create an image service which will serve the resolution/quality you need and cache the result.

虐人心 2024-11-26 23:27:53

您可以使用 适用于 PHP 的 Asido 图像库 来调整图像大小。不过这个库使用了 GD。这是一些示例使用代码

调整大小和其他图像操作最好在上传新图像后完成(除非您想为其他目的保存更高分辨率)。

You could use the Asido imaging library for PHP to resize your images. This library makes use of GD though. Here is some example usage code.

The resizing and other imaging operations are preferably done after the uploading of new images (except if you want to save the higher resolution for some other purpose).

虚拟世界 2024-11-26 23:27:53
<p>
//This function will proportionally resize image 
function resizeImage($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{
//Check Image size is not 0
if($CurWidth <= 0 || $CurHeight <= 0) 
{
return false;
}   
//Construct a proportional size of new image
$ImageScale         = min($MaxSize/$CurWidth, $MaxSize/$CurHeight); 
$NewWidth           = ceil($ImageScale*$CurWidth);
$NewHeight          = ceil($ImageScale*$CurHeight);
$NewCanves          = imagecreatetruecolor($NewWidth, $NewHeight);
// Resize Image
if(imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight))
{
switch(strtolower($ImageType))
{
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;          
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
//Destroy image, frees memory   
if(is_resource($NewCanves)) {imagedestroy($NewCanves);} 
return true;
}
}
</p>
<p>
//This function will proportionally resize image 
function resizeImage($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{
//Check Image size is not 0
if($CurWidth <= 0 || $CurHeight <= 0) 
{
return false;
}   
//Construct a proportional size of new image
$ImageScale         = min($MaxSize/$CurWidth, $MaxSize/$CurHeight); 
$NewWidth           = ceil($ImageScale*$CurWidth);
$NewHeight          = ceil($ImageScale*$CurHeight);
$NewCanves          = imagecreatetruecolor($NewWidth, $NewHeight);
// Resize Image
if(imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight))
{
switch(strtolower($ImageType))
{
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;          
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
//Destroy image, frees memory   
if(is_resource($NewCanves)) {imagedestroy($NewCanves);} 
return true;
}
}
</p>
蓝天白云 2024-11-26 23:27:53

我会选择您的页面所需的一些标准头像图像尺寸,例如

  • 个人资料页面的中等尺寸,如果您有一个和
  • 小尺寸出现在用户帖子旁边,
  • 您就会明白,正是您所需要

的用户上传新头像,您可以通过合理的质量设置将其转换为您需要的格式。我假设您要使用 JPEG,因为对于此用例而言,这是一种很好的包罗万象的格式。 PNG 不太适合摄影内容,JPEG 不太适合绘图,但你看到的大多数头像都是照片。这些天我不会再使用 GIF,它们限制为 256 种颜色,并且只有 1 位 Alpha 通道。

I'd pick some standard avatar image sizes you'll need for your page, like

  • medium size for a profile page, if you have one and
  • small size which appears next to the user's post
  • you get the idea, just what you need

And when the user uploads a new avatar, you convert it to the formats you'll need with a reasonable quality setting. I'm assuming you're going for JPEGs, because this is a good catch-all format for this use case. PNGs do poor with photographic content, JPEGs are not so great for drawings, but then most avatars you see are photos. I wouldn't use GIFs any more these days, they limit to 256 colors and have only a 1-bit alpha channel.

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