php 减少图片体积,如何在没有Imagick的情况下在PHP中调整图像大小并降低质量

本文概述

你可能需要调整用户上载文件的大小, 以节省服务器上的空间。

调整图像大小和降低图像质量的方法有很多, 创建自己的函数可能会更好, 因为它可能更适合你的需求。

在这种情况下, 我们将向你展示一种使用纯PHP而不使用Imagick库来调整图像大小并降低质量的方法。

调整图像大小并返回原始数据

要调整文件中图像的大小, 请使用以下方法。它支持png, jpg和gif格式, 你可以扩展支持的方法来修改函数中的switch语句。

/**

* Resize image given a height and width and return raw image data.

*

* Note : You can add more supported image formats adding more parameters to the switch statement.

*

* @param type $file filepath

* @param type $w width in px

* @param type $h height in px

* @param type $crop Crop or not

* @return type

*/

function resize_image($file, $w, $h, $crop=false) {

list($width, $height) = getimagesize($file);

$r = $width / $height;

if ($crop) {

if ($width > $height) {

$width = ceil($width-($width*abs($r-$w/$h)));

} else {

$height = ceil($height-($height*abs($r-$w/$h)));

}

$newwidth = $w;

$newheight = $h;

} else {

if ($w/$h > $r) {

$newwidth = $h*$r;

$newheight = $h;

} else {

$newheight = $w/$r;

$newwidth = $w;

}

}

//Get file extension

$exploding = explode(".", $file);

$ext = end($exploding);

switch($ext){

case "png":

$src = imagecreatefrompng($file);

break;

case "jpeg":

case "jpg":

$src = imagecreatefromjpeg($file);

break;

case "gif":

$src = imagecreatefromgif($file);

break;

default:

$src = imagecreatefromjpeg($file);

break;

}

$dst = imagecreatetruecolor($newwidth, $newheight);

imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

return $dst;

}

先前的方法会从图片中返回变量原始数据, 因此你需要根据文件类型手动保存它, 即:

$filename = "/var/www/images/mynormalimage.png";

$resizedFilename = "/var/www/images/myresizedimage.png";

// resize the image with 300x300

$imgData = resize_image($filename, 300, 300);

// save the image on the given filename

imagepng($imgData, $resizedFilename);

// or according to the original format, use another method

// imagejpeg($imgData, $resizedFilename);

// imagegif($imgData, $resizedFilename);

这是一种非常简单的解决方案, 可以快速调整正常图像的大小, 但是请注意, 建议你使用上一个功能的逻辑并实施一个符合你需要的新功能。

降低或提高质量

请注意, 使用imagejpeg, imagepng等方法, 你可以在第三个参数中使用数值(INT)指定图像的质量:

// Get the data from a png file

$imageData = resize_image("myfolder/myfile.png", 500, 500);

// Give a name to your file with the full patj

$filepath = "myfolder/myresizedimage.png";

$quality = 0;

imagepng($imageData, $filepath, $quality);

每个保存方法都支持第三个参数, 该参数指定图像的质量。此值设置压缩级别:0-9或-1, 其中0根本没有压缩, 1是最快, 但是产生较大的文件, 9提供最佳压缩(最小的文件), 但是压缩时间长, 以及-1选择编译到zlib库中的默认值。

你甚至可以创建compress方法, 如下所示:

/**

* Decrease or increase the quality of an image without resize it.

*

* @param type $source

* @param type $destination

* @param type $quality

* @return type

*/

function compress($source, $destination, $quality) {

//Get file extension

$exploding = explode(".", $source);

$ext = end($exploding);

switch($ext){

case "png":

$src = imagecreatefrompng($source);

break;

case "jpeg":

case "jpg":

$src = imagecreatefromjpeg($source);

break;

case "gif":

$src = imagecreatefromgif($source);

break;

default:

$src = imagecreatefromjpeg($source);

break;

}

switch($ext){

case "png":

imagepng($src, $destination, $quality);

break;

case "jpeg":

case "jpg":

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

break;

case "gif":

imagegif($src, $destination, $quality);

break;

default:

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

break;

}

return $destination;

}

它期望需要修改的文件的文件路径。第二个参数是文件的目标路径, 可以与第一个参数相同以便替换它。

信息

请记住, 有许多受支持的图像类型需要处理, PHP支持以下方法:

imagecreatefromgd。

imagecreatefromgd2。

imagecreatefromgd2part。

imagecreatefromgif。

imagecreatefromjpeg。

imagecreatefrompng。

imagecreatefromstring。

imagecreatefromwbmp。

imagecreatefromwebp。

imagecreatefromxbm。

imagecreatefromxpm。

并使用以前的方法创建图像, 请使用:

imagegd。

imagegd2。

imagegd2part。

imagecreatefromgif。

imagejpeg。

imagepng。

图像字符串。

imagewbmp。

imagewebp。

imagexbm。

imagexpm。

并分别保存带有前缀image (imagefrom , filepath)的方法。玩得开心

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值