使用php(jpeg)优化上传的图片 [英] Optimize uploaded images with php (jpeg)

查看:262
本文介绍了使用php(jpeg)优化上传的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google Chrome中运行Page Speed时,建议优化/压缩图像.这些图像主要由用户上传,因此我需要在上传过程中对其进行优化.我发现使用php优化jpeg图像的过程类似于使用以下GD函数:

When running Page Speed in Google Chrome it suggests to optimize/compress the images. These images are mostly uploaded by users, so I would need to optimize them during uploading. What I find about optimizing jpeg images with php is something like using the following GD functions:

getimagesize()
imagecreatefromjpeg()
imagejpeg()

由于我要在上传后调整图像大小,因此我已经通过这些功能拉出图像,此外,我在imagecreatefromjpeg()之后使用imagecopyresampled()来调整其大小.

Since I am resizing the images after upload I'm already pulling the image through these functions and in addition I use imagecopyresampled() after imagecreatefromjpeg() to resize it.

但是随后,Page Speed仍然告诉我可以优化这些图像.如何在php脚本中完成此优化?在imagejpeg()中设置较低的质量也无济于事.

But then, Page Speed is still telling me these images can be optimized. How can I accomplish this optimisation in a php script? Set the quality lower in imagejpeg() doesn't make a difference either.

推荐答案

imagejpeg函数是您分配质量的地方.如果您已经将其设置为适当的值,那么您将无能为力.

The imagejpeg function is where you assign the quality. If you're already setting that to an appropriate value then there is little else you can do.

页面速度可能认为高于一定大小的所有图像都是需要压缩"的,也许只是确保它们都尽可能小(就高度/宽度而言)并进行压缩.

Page speed probably considers all images above a certain size to be "needing compression", perhaps just ensure they are all as small as reasonable (in terms of height/width) and compressed.

您可以在pagespeed文档 http://code.google.com/speed/page-speed/docs/payload.html#CompressImages ,其中介绍了一些适当压缩的技术/工具.

You can find more about page speed and it's compression suggestions on the pagespeed docs http://code.google.com/speed/page-speed/docs/payload.html#CompressImages which describes some of the techniques/tools to compress appropriately.

我也刚刚阅读以下内容:

I've also just read the following:

有几种工具可以对JPEG和PNG文件进行进一步的无损压缩,而不会影响图像质量.对于JPEG,我们建议 jpegtran jpegoptim (仅在Linux上可用;使用--strip-all选项运行).对于PNG,我们建议 OptiPNG PNGOUT .

Several tools are available that perform further, lossless compression on JPEG and PNG files, with no effect on image quality. For JPEG, we recommend jpegtran or jpegoptim (available on Linux only; run with the --strip-all option). For PNG, we recommend OptiPNG or PNGOUT.

因此,也许(如果您确实要坚持Google的建议),您可以使用PHP的exec在文件上传时对其中的一种工具运行.

So perhaps (if you really want to stick to Google's suggestions) you could use PHP's exec to run one of those tools on files as they are uploaded.

要使用php压缩,请执行以下操作(听起来已经在执行此操作了):

To compress with php you do the following (sounds like you are already doing this):

$source_url是图像,$destination_url是保存位置,$quality是1到100之间的数字,用于选择要使用的jpeg压缩量.

Where $source_url is the image, $destination_url is where to save and $quality is a number between 1 and 100 choosing how much jpeg compression to use.

function compressImage($source_url, $destination_url, $quality) {
    $info = getimagesize($source_url);

    if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
    elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
    elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);

    //save file
    imagejpeg($image, $destination_url, $quality);

    //return destination file
    return $destination_url;
}

这篇关于使用php(jpeg)优化上传的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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