gif文件压缩,php 压缩动态gif,php 压缩图片处理png、gif背景变黑问题

 2023-09-25 阅读 13 评论 0

摘要:/**gif文件压缩,* 压缩图片* @param $imgsrc 图片路径gif图片怎么压缩、* @param string $imgdst 压缩后保存路径* @param int $quality 压缩图片的质量* @return string*/public function compressedImage($imgsrc, $imgdst="",$quality=75

/**

gif文件压缩,* 压缩图片

* @param $imgsrc 图片路径

gif图片怎么压缩、* @param string $imgdst 压缩后保存路径

* @param int $quality 压缩图片的质量

* @return string

*/

public function compressedImage($imgsrc, $imgdst="",$quality=75) {

//绝对路径 或者相对路径去掉前面的/

//if (!is_file($imgsrc))$imgsrc= __DIR__ . '/'.$imgsrc;

list($width, $height, $type) = getimagesize($imgsrc);

if ($imgdst==""){

$position1 = strrpos($imgsrc,'/');

$path1 = substr($imgsrc,0,$position1);

$name1=substr($imgsrc,$position1+1);

$imgdst=$path1."/thumb/".$name1;

// $arrtmp=explode("/",$imgsrc);

// array_splice($arrtmp, count($arrtmp)-1, 0, array("thumb"));

// $imgdst=implode("/",$arrtmp);

}

if (is_file($imgdst)){return $imgdst;}

$new_width = $width;//压缩后的图片宽

$new_height = $height;//压缩后的图片高

if($width >= 620){

$per = 620 / $width;//计算比例

$new_width = $width * $per;

$new_height = $height * $per;

}

//print_r($type);exit();

//创建文件交

$position = strrpos($imgdst,'/');

$path = substr($imgdst,0,$position);

if(!file_exists($path)){mkdir($path,0777,true);}

switch ($type) {

case 1:

$giftype = check_gifcartoon($imgsrc);

if ($giftype) {

//header('Content-Type:image/gif');

$image_wp = imagecreatetruecolor($new_width, $new_height);

/* --- 用以处理缩放gif和png图透明背景变黑色问题 开始 --- */

$color = imagecolorallocate($image_wp,255,255,255);

imagecolortransparent($image_wp,$color);

imagefill($image_wp,0,0,$color);

/* --- 用以处理缩放gif和png图透明背景变黑色问题 结束 --- */

$image = imagecreatefromgif($imgsrc);

imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

//90代表的是质量、压缩图片容量大小

imagejpeg($image_wp, $imgdst, $quality);

imagedestroy($image_wp);

imagedestroy($image);

}

break;

case 2:

//header('Content-Type:image/jpeg');

$image_wp = imagecreatetruecolor($new_width, $new_height);

$image = imagecreatefromjpeg($imgsrc);

imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

//90代表的是质量、压缩图片容量大小

imagejpeg($image_wp, $imgdst, $quality);

imagedestroy($image_wp);

imagedestroy($image);

break;

case 3:

//header('Content-Type:image/png');

$image_wp = imagecreatetruecolor($new_width, $new_height);

/* --- 用以处理缩放gif和png图透明背景变黑色问题 开始 --- */

$color = imagecolorallocate($image_wp,255,255,255);

imagecolortransparent($image_wp,$color);

imagefill($image_wp,0,0,$color);

/* --- 用以处理缩放gif和png图透明背景变黑色问题 结束 --- */

$image = imagecreatefrompng($imgsrc);

imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

//90代表的是质量、压缩图片容量大小

imagejpeg($image_wp, $imgdst, $quality);

imagedestroy($image_wp);

imagedestroy($image);

break;

}

return $imgdst;

}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/93894.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息