PHP图片压缩

转自:http://www.blhere.com/1166.html

图片压缩即图片剪裁,其中的制作过程和图片水印很类似,不同点在于图片压缩需要将现有图片按一定比例复制到内存中。

下面给出代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php  
   
/*打开图片*/
$src = "bg.jpg";  
$info = getimagesize($src);  
$type = image_type_to_extension($info[2],false);  
$fun = "imagecreatefrom".$type;  
$image = $fun($src);  
   
/*操作图片*/
//1.内存中建立一个300,200真色彩图片  
$image_thumb = imagecreatetruecolor(300,200);  
//2.核心步骤,将原图复制到真色彩图片上  
imagecopyresampled($image_thumb, $image, 0, 0, 0, 0, 300, 200, $info[0], $info[1]);  
//3.销毁原始图片  
imagedestroy($image);  
   
/*输出图片*/
//浏览器  
header("Content-type:".$info['mime']);  
$fun = "image".$type;  
$fun($image_thumb);  
//保存图片  
$fun($image_thumb,'bg_tb.'.$type);  
/*销毁图片*/
imagedestroy($image_thumb);
原文地址:https://www.cnblogs.com/lvchenfeng/p/5261039.html