哪种PHP方法可以在不降低图像质量的前提下减少图像大小

时间:2021-07-21 21:49:12

I am trying to develop an image based web site. I am really confused about the image type and compression techniques. Please advice me the best way to compress image sizes.

我正在开发一个基于图像的网站。我对图像类型和压缩技术非常困惑。请告诉我压缩图像大小的最好方法。

4 个解决方案

#1


3  

I'd go for jpeg. Read this post regarding image size reduction and after deciding on the technique, use ImageMagick

我去jpeg。阅读这篇关于缩小图像尺寸的文章,在决定了技术之后,使用ImageMagick

Hope this helps

希望这有助于

#2


55  

If you are looking to reduce the size using coding itself, you can follow this code in php.

如果您希望使用代码本身来减少大小,您可以使用php遵循此代码。

<?php 
function compress($source, $destination, $quality) {

    $info = getimagesize($source);

    if ($info['mime'] == 'image/jpeg') 
        $image = imagecreatefromjpeg($source);

    elseif ($info['mime'] == 'image/gif') 
        $image = imagecreatefromgif($source);

    elseif ($info['mime'] == 'image/png') 
        $image = imagecreatefrompng($source);

    imagejpeg($image, $destination, $quality);

    return $destination;
}

$source_img = 'source.jpg';
$destination_img = 'destination .jpg';

$d = compress($source_img, $destination_img, 90);
?>

$d = compress($source_img, $destination_img, 90);

This is just a php function that passes the source image ( i.e., $source_img ), destination image ( $destination_img ) and quality for the image that will take to compress ( i.e., 90 ).

这只是一个php函数,它传递源映像(例如。, $source_img)、目标映像($destination_img)和要进行压缩的映像的质量(即。,90)。

$info = getimagesize($source);

The getimagesize() function is used to find the size of any given image file and return the dimensions along with the file type.

getimagesize()函数用于查找任何给定图像文件的大小,并返回尺寸和文件类型。

#3


4  

You can resize and then use imagejpeg()

您可以调整大小,然后使用imagejpeg()

Don't pass 100 as the quality for imagejpeg() - anything over 90 is generally overkill and just gets you a bigger JPEG. For a thumbnail, try 75 and work downwards until the quality/size tradeoff is acceptable.

不要超过100作为imagejpeg()的质量——任何超过90的东西通常都是多余的,只会让你得到一个更大的JPEG。对于缩略图,尝试75并向下工作,直到质量/尺寸的权衡是可以接受的。

imagejpeg($tn, $save, 75);

#4


1  

well I think I have something interesting for you... https://github.com/whizzzkid/phpimageresize. I wrote it for the exact same purpose. Highly customizable, and does it in a great way.

我想我有一些有趣的东西给你……https://github.com/whizzzkid/phpimageresize。我写它的目的是一样的。高度可定制,并以一种伟大的方式。

#1


3  

I'd go for jpeg. Read this post regarding image size reduction and after deciding on the technique, use ImageMagick

我去jpeg。阅读这篇关于缩小图像尺寸的文章,在决定了技术之后,使用ImageMagick

Hope this helps

希望这有助于

#2


55  

If you are looking to reduce the size using coding itself, you can follow this code in php.

如果您希望使用代码本身来减少大小,您可以使用php遵循此代码。

<?php 
function compress($source, $destination, $quality) {

    $info = getimagesize($source);

    if ($info['mime'] == 'image/jpeg') 
        $image = imagecreatefromjpeg($source);

    elseif ($info['mime'] == 'image/gif') 
        $image = imagecreatefromgif($source);

    elseif ($info['mime'] == 'image/png') 
        $image = imagecreatefrompng($source);

    imagejpeg($image, $destination, $quality);

    return $destination;
}

$source_img = 'source.jpg';
$destination_img = 'destination .jpg';

$d = compress($source_img, $destination_img, 90);
?>

$d = compress($source_img, $destination_img, 90);

This is just a php function that passes the source image ( i.e., $source_img ), destination image ( $destination_img ) and quality for the image that will take to compress ( i.e., 90 ).

这只是一个php函数,它传递源映像(例如。, $source_img)、目标映像($destination_img)和要进行压缩的映像的质量(即。,90)。

$info = getimagesize($source);

The getimagesize() function is used to find the size of any given image file and return the dimensions along with the file type.

getimagesize()函数用于查找任何给定图像文件的大小,并返回尺寸和文件类型。

#3


4  

You can resize and then use imagejpeg()

您可以调整大小,然后使用imagejpeg()

Don't pass 100 as the quality for imagejpeg() - anything over 90 is generally overkill and just gets you a bigger JPEG. For a thumbnail, try 75 and work downwards until the quality/size tradeoff is acceptable.

不要超过100作为imagejpeg()的质量——任何超过90的东西通常都是多余的,只会让你得到一个更大的JPEG。对于缩略图,尝试75并向下工作,直到质量/尺寸的权衡是可以接受的。

imagejpeg($tn, $save, 75);

#4


1  

well I think I have something interesting for you... https://github.com/whizzzkid/phpimageresize. I wrote it for the exact same purpose. Highly customizable, and does it in a great way.

我想我有一些有趣的东西给你……https://github.com/whizzzkid/phpimageresize。我写它的目的是一样的。高度可定制,并以一种伟大的方式。