/**
* @param string $src 源文件地址
* @param int $percent 图片质量
* 图片压缩
*/
function ImgCompress($src,$percent=1){
//配置内存大小
ini_set('memory_limit', '256M');
//typearr存的是一些图片的格式,用作下文getimagesize()获取图片信息中的图片格式进行比对
$typearr = array("gif", "jpeg", "png", "swf", "psd", "bmp",'17'=>'webp');
list($width, $height, $type, $attr) = getimagesize($src); //获取图片信息
/**-------------------------------------------------------------------------------*/
//对imagecreatefrom 系列函数进行拼接从文件或 URL 载入一幅图像,成功返回图像资源,失败则返回一个空字符串
$func = "imagecreatefrom" . $typearr[$type - 1];
$image = $func($src);
/**-----------------------------------------------------------------------------------*/
//创建一个新的真彩色图像
$new_width = $width * $percent;
$new_height = $height * $percent;
$image_thump = imagecreatetruecolor($new_width, $new_height);
/**------------------------------------------------------------------------------*/
//图像处理
imagecopyresampled($image_thump, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
/**-------------------------------------------------------------------------------------------*/
//拼接一个image系列函数(将图片输出到浏览器或文件)
$imgfunc= "image" . $typearr[$type - 1];
$imgfunc($image_thump,$src); //原图覆盖
//销毁图像资源
imagedestroy($image);
//销毁给定的变量
unset($image);
}
优化透明图片的处理
/**
* @param string $src 源文件地址
* @param int $percent 图片质量
* GD库 图片压缩
*/
function ImgCompress($src,$percent=1){
//配置内存大小
ini_set('memory_limit', '256M');
//typearr存的是一些图片的格式,用作下文getimagesize()获取图片信息中的图片格式进行比对
$typearr = array("gif", "jpeg", "png", "swf", "psd", "bmp",'17'=>'webp');
list($width, $height, $type, $attr) = getimagesize($src); //获取图片信息
/**-----------------------------------------------------------------------------------------------------*/
//对imagecreatefrom 系列函数进行拼接从文件或 URL 载入一幅图像,成功返回图像资源,失败则返回一个空字符串
$func = "imagecreatefrom" . $typearr[$type - 1];
$image = @$func($src);
/**-----------------------------------------------------------------------------------------------------*/
//创建一个新的真彩色图像
$new_width = intval($width * $percent);
$new_height = intval($height * $percent);
$image_thump = imagecreatetruecolor($new_width, $new_height);
/**-----------------------------------------------------------------------------------------------------*/
// 处理透明背景图片变成黑色的问题
if(strtolower($typearr[$type - 1])=='png'){
//imageantialias($image_thump, true);
$color = imagecolorallocatealpha($image_thump, 0, 0, 0, 127);
imagecolortransparent($image_thump, $color);
imagefill($image_thump, 0, 0, $color);
imagesavealpha($image_thump,true);
}
/**----------------------------------------------------------------------------------------------------*/
//图像处理
imagecopyresampled($image_thump, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
/**--------------------------------------------------------------------------------------------------------------------*/
//拼接一个image系列函数(将图片输出到浏览器或文件)
$imgfunc= "image" . $typearr[$type - 1];
//原图覆盖
$imgfunc($image_thump,$src);
//销毁图像资源
imagedestroy($image);
//销毁给定的变量
unset($image);
}
THE END
                                                
                                            
                        
                                
            
                    
                                
                                    匿名                                
                             
                            2025-10-22
                        
                    
盖楼盖楼!
                                
                                    匿名                                
                             
                            2025-08-11
                        
                    
沙发沙发
                                
                                    匿名                                
                             
                            2025-08-10
                        
                    
https://at.oiik.cn/bing.html
                                
                                    匿名                                
                             
                            2025-02-21
                        
                    
实用,我在开发https://minmail.app/时候使用到了
                                
                                    王飞翔                                
                             
                            2024-12-30
                        
                    
亲爱的朋友:您好!中国疫情持续蔓延,很多人症状非常严重持久不愈,医院人满为患,各年龄段随地倒猝死的现象暴增,多省感染手足口、甲流、乙流、支原体、合胞及腺病毒的儿童不断攀升,目前各种天灾人祸,天气异象频发。古今中外的很多预言都说了这几年人类有大灾难,如刘伯温在预言中说 “贫者一万留一千,富者一万留二三”,“贫富若不回心转,看看死期到眼前”, 预言中也告诉世人如何逃离劫难的方法,真心希望您能躲过末劫中的劫难,有个美好的未来,请您务必打开下方网址认真了解,内有躲避瘟疫保平安的方法。网址1:https://github.com/1992513/www/blob/master/README.md?abhgc#1 网址2:bitly.net/55bbbb 网址3:https://d3ankibxiji86m.cloudfront.net/30gj 如打不开请多换几个浏览器试
                                
                                    匿名                                
                             
                            2024-12-12
                        
                    
Backdata 搜索引擎网址提交入口:https://backdata.net/submit-site.html