ImageUtils.php
5.25 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace common\helpers;
/**
* Class ImageUrl
* @package app\wx\helpers
*/
class ImageUtils
{
/**
* 图像等比缩放处理函数
* @param $im
* @param $scaleWidth
* @param $scaleHeight
* @param $name
* @param $filetype
*/
public static function resizeImage($im, $scaleWidth, $scaleHeight, $savePath = '')
{
if (!$im) {
return null;
}
$pic_width = imagesx($im);
$pic_height = imagesy($im);
// 尺寸不合法, 直接返回原图
if ($scaleWidth <= 0 || $scaleHeight <= 0) {
return $im;
}
// 计算缩放比例
$widthRatio = $scaleWidth / $pic_width;
$heightRatio = $scaleHeight / $pic_height;
$scaleRatio = ($widthRatio < $heightRatio) ? $widthRatio : $heightRatio;
// 计算新图像宽高
$newWidth = $pic_width * $scaleRatio;
$newHeight = $pic_height * $scaleRatio;
// 缩放处理
if(function_exists("imagecopyresampled")) {
$newim = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newim, $im, 0, 0, 0, 0,$newWidth, $newHeight, $pic_width, $pic_height);
} else {
$newim = imagecreate($newWidth, $newHeight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newWidth, $newHeight, $pic_width, $pic_height);
}
// 如果传入了保存路径, 则将缩放后的图片写入保存
if (!empty($savePath)) {
imagejpeg($newim, $savePath);
imagedestroy($newim);
}
else { // 不写入文件直接使用, 记得使用完释放掉
return $newim;
}
}
/**
* 按照指定的尺寸压缩图片
* @param $source_path 原图路径
* @param $target_path 保存路径
* @param $imgWidth 目标宽度
* @param $imgHeight 目标高度
* @return bool|string
*/
function resize_image($im, $scaleWidth, $scaleHeight, $savePath = '')
{
if (!$im) {
return null;
}
$pic_width = imagesx($im);
$pic_height = imagesy($im);
// 尺寸不合法, 直接返回原图
if ($scaleWidth <= 0 || $scaleHeight <= 0) {
return $im;
}
// 计算新图像宽高
$newWidth = $scaleWidth;
$newHeight = $scaleHeight;
// 缩放处理
if(function_exists("imagecopyresampled")) {
$newim = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newim, $im, 0, 0, 0, 0,$newWidth, $newHeight, $pic_width, $pic_height);
} else {
$newim = imagecreate($newWidth, $newHeight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newWidth, $newHeight, $pic_width, $pic_height);
}
// 如果传入了保存路径, 则将缩放后的图片写入保存
if (!empty($savePath)) {
imagejpeg($newim, $savePath);
imagedestroy($newim);
}
else { // 不写入文件直接使用, 记得使用完释放掉
return $newim;
}
}
/** 方法参数为图片名
* @param $str_file
* @param int $max
* @return bool
*/
static function changeSize($str_file, $max = 600)
{
$size = getimagesize($str_file);
//这个方法会返回一个数组,第一个元素 (索引值 0) 是图片的高度,单位是像素 (pixel)。第二个元素 (索引值 1) 是图片的宽度。第三个元素 (索引值 2) 是图片的文件格式,其值 1 为 GIF 格式、 2 为 JPEG/JPG 格式、3 为 PNG 格式。第四个元素 (索引值 3) 为图片的高与宽字符串,height=xxx width=yyy。
//因为PHP只能对资源进行操作,所以要对需要进行缩放的图片进行拷贝,创建为新的资源
//根据图片的格式使用不同的方法
if($size[2]==1)
$src=imagecreatefromgif($str_file);
if($size[2]==2)
$src=imagecreatefromjpeg($str_file);
if($size[2]==3)
$src=imagecreatefrompng($str_file);
//取得源图片的宽度和高度
$w = $size['0'];
$h = $size['1'];
//根据最大值为300,算出另一个边的长度,得到缩放后的图片宽度和高度
if ($w > $h) {
$w = $max;
$h = $h * ($max / $size['0']);
} else {
$h = $max;
$w = $w * ($max / $size['1']);
}
//声明一个$w宽,$h高的真彩图片资源,此时只是一个有宽高的黑白图片。
$image = imagecreatetruecolor($w, $h);
//关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)返回值为bool
imagecopyresampled($image, $src, 0, 0, 0, 0, $w, $h, $size['0'], $size['1']);
//将图片塞进去,也是根据图片不同的格式选用不同的方法,返回值为bool。
if($size[2]==1)
$result = imagegif($image,$str_file);
if($size[2]==2)
$result = imagejpeg($image,$str_file);
if($size[2]==3)
$result = imagepng($image,$str_file);
//销毁资源
imagedestroy($image);
return $result ;
}
}