-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_reduce.php
52 lines (50 loc) · 1.78 KB
/
image_reduce.php
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
* @param $imgSrc string 压缩图片路径
* @param $imgDst string 压缩后图片路径
*/
function image_png_size_add($imgSrc,$imgDst)
{
list($width,$height,$type) = getimagesize($imgSrc);
$newWidth = ($width > 600 ? 600 : $width)* 0.9;
$newHeight = ($height > 600 ? 600 : $height)*0.9;
switch ($type){
case 1:
$gifType = checkGif($imgSrc);
if ($gifType){
header('Content-Type:image/gif');
$imageWp = imagecreatetruecolor($newWidth,$newHeight);
$image = imagecreatefromgif($imgSrc);
imagecopyresampled($imageWp,$image,0,0,0,0,$newWidth,$newHeight,$width,$height);
imagejpeg($imageWp,$imgDst,75);
imagedestroy($imageWp);
}
break;
case 2:
header('Content-Type:image/jpeg');
$imageWp = imagecreatetruecolor($newWidth,$newHeight);
$image = imagecreatefromjpeg($imgSrc);
imagecopyresampled($imageWp,$image,0,0,0,0,$newWidth,$newHeight,$width,$height);
imagejpeg($imageWp,$imgDst,75);
imagedestroy($imageWp);
break;
case 3:
header('Content-type:image/png');
$imageWp = imagecreatetruecolor($newWidth,$newHeight);
$image = imagecreatefrompng($imgSrc);
imagecopyresampled($imageWp,$image,0,0,0,0,$newWidth,$newHeight,$width,$height);
imagejpeg($imageWp,75);
imagedestroy($imageWp);
}
}
/**
* 判断是否gif动画
* @param $image_file
* @return bool
*/
function checkGif($image_file){
$fp = fopen($image_file,'rb');
$image_head = fread($fp,1024);
fclose($fp);
return preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$image_head)?false:true;
}