|
|
|
1
|
+<?php
|
|
|
|
2
|
+// +----------------------------------------------------------------------
|
|
|
|
3
|
+// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
|
|
|
4
|
+// +----------------------------------------------------------------------
|
|
|
|
5
|
+// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
|
|
|
6
|
+// +----------------------------------------------------------------------
|
|
|
|
7
|
+// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
|
|
|
8
|
+// +----------------------------------------------------------------------
|
|
|
|
9
|
+// | Author: liu21st <liu21st@gmail.com>
|
|
|
|
10
|
+// +----------------------------------------------------------------------
|
|
|
|
11
|
+
|
|
|
|
12
|
+namespace think;
|
|
|
|
13
|
+/**
|
|
|
|
14
|
+ 图片压缩操作类
|
|
|
|
15
|
+ v1.0
|
|
|
|
16
|
+*/
|
|
|
|
17
|
+class Compression
|
|
|
|
18
|
+{
|
|
|
|
19
|
+ private $src;
|
|
|
|
20
|
+ public $percent = 0.1;
|
|
|
|
21
|
+ public function __construct($src){
|
|
|
|
22
|
+
|
|
|
|
23
|
+ $this->src = $src;
|
|
|
|
24
|
+
|
|
|
|
25
|
+ }
|
|
|
|
26
|
+ /**
|
|
|
|
27
|
+ * 图片压缩处理
|
|
|
|
28
|
+ * @param string $sFile 图片路径
|
|
|
|
29
|
+ * @param int $iWidth 自定义图片宽度
|
|
|
|
30
|
+ * @param int $iHeight 自定义图片高度
|
|
|
|
31
|
+ */
|
|
|
|
32
|
+ function getThumb(){
|
|
|
|
33
|
+ $iWidth = 375;
|
|
|
|
34
|
+ $iHeight = 240;
|
|
|
|
35
|
+ $sFile = $this->src;
|
|
|
|
36
|
+ //判断该图片是否存在
|
|
|
|
37
|
+ if(!file_exists($sFile)) return $sFile;
|
|
|
|
38
|
+ //判断图片格式
|
|
|
|
39
|
+ $attach_fileext = $this->get_filetype($sFile);
|
|
|
|
40
|
+ if (!in_array($attach_fileext, array('jpg','png','jpeg'))){
|
|
|
|
41
|
+ return $sFile;
|
|
|
|
42
|
+ }
|
|
|
|
43
|
+ //压缩图片
|
|
|
|
44
|
+ $sFileNameS = str_replace(".".$attach_fileext, "_".$iWidth.'_'.$iHeight.'.'.$attach_fileext, $sFile);
|
|
|
|
45
|
+ //判断是否已压缩图片,若是则返回压缩图片路径
|
|
|
|
46
|
+ if(file_exists($sFileNameS)){
|
|
|
|
47
|
+ return $sFileNameS;
|
|
|
|
48
|
+ }
|
|
|
|
49
|
+ //解决手机端上传图片被旋转问题
|
|
|
|
50
|
+ /*if (in_array($attach_fileext, array('jpeg')) ){
|
|
|
|
51
|
+ $this->adjustPicOrientation($sFile);
|
|
|
|
52
|
+ }*/
|
|
|
|
53
|
+ //生成压缩图片,并存储到原图同路径下
|
|
|
|
54
|
+ $this->resizeImage($sFile, $sFileNameS, $iWidth, $iHeight);
|
|
|
|
55
|
+ if(!file_exists($sFileNameS)){
|
|
|
|
56
|
+ return $sFile;
|
|
|
|
57
|
+ }
|
|
|
|
58
|
+ return $sFileNameS;
|
|
|
|
59
|
+ }
|
|
|
|
60
|
+ /**
|
|
|
|
61
|
+ *获取文件后缀名
|
|
|
|
62
|
+ */
|
|
|
|
63
|
+ function get_filetype($filename) {
|
|
|
|
64
|
+ $extend = explode("." , $filename);
|
|
|
|
65
|
+ return strtolower($extend[count($extend) - 1]);
|
|
|
|
66
|
+ }
|
|
|
|
67
|
+ /**
|
|
|
|
68
|
+ * 生成图片
|
|
|
|
69
|
+ * @param string $im 源图片路径
|
|
|
|
70
|
+ * @param string $dest 目标图片路径
|
|
|
|
71
|
+ * @param int $maxwidth 生成图片宽
|
|
|
|
72
|
+ * @param int $maxheight 生成图片高
|
|
|
|
73
|
+ */
|
|
|
|
74
|
+ function resizeImage($im, $dest, $maxwidth, $maxheight) {
|
|
|
|
75
|
+ $img = getimagesize($im);
|
|
|
|
76
|
+ switch ($img[2]) {
|
|
|
|
77
|
+ case 1:
|
|
|
|
78
|
+ $im = @imagecreatefromgif($im);
|
|
|
|
79
|
+ break;
|
|
|
|
80
|
+ case 2:
|
|
|
|
81
|
+ $im = @imagecreatefromjpeg($im);
|
|
|
|
82
|
+ break;
|
|
|
|
83
|
+ case 3:
|
|
|
|
84
|
+ $im = @imagecreatefrompng($im);
|
|
|
|
85
|
+ break;
|
|
|
|
86
|
+ }
|
|
|
|
87
|
+
|
|
|
|
88
|
+ $pic_width = imagesx($im);
|
|
|
|
89
|
+ $pic_height = imagesy($im);
|
|
|
|
90
|
+ $resizewidth_tag = false;
|
|
|
|
91
|
+ $resizeheight_tag = false;
|
|
|
|
92
|
+ if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
|
|
|
|
93
|
+ if ($maxwidth && $pic_width > $maxwidth) {
|
|
|
|
94
|
+ $widthratio = $maxwidth / $pic_width;
|
|
|
|
95
|
+ $resizewidth_tag = true;
|
|
|
|
96
|
+ }
|
|
|
|
97
|
+
|
|
|
|
98
|
+ if ($maxheight && $pic_height > $maxheight) {
|
|
|
|
99
|
+ $heightratio = $maxheight / $pic_height;
|
|
|
|
100
|
+ $resizeheight_tag = true;
|
|
|
|
101
|
+ }
|
|
|
|
102
|
+
|
|
|
|
103
|
+ if ($resizewidth_tag && $resizeheight_tag) {
|
|
|
|
104
|
+ if ($widthratio < $heightratio)
|
|
|
|
105
|
+ $ratio = $widthratio;
|
|
|
|
106
|
+ else
|
|
|
|
107
|
+ $ratio = $heightratio;
|
|
|
|
108
|
+ }
|
|
|
|
109
|
+
|
|
|
|
110
|
+
|
|
|
|
111
|
+ if ($resizewidth_tag && !$resizeheight_tag)
|
|
|
|
112
|
+ $ratio = $widthratio;
|
|
|
|
113
|
+ if ($resizeheight_tag && !$resizewidth_tag)
|
|
|
|
114
|
+ $ratio = $heightratio;
|
|
|
|
115
|
+ $newwidth = $pic_width * $ratio;
|
|
|
|
116
|
+ $newheight = $pic_height * $ratio;
|
|
|
|
117
|
+
|
|
|
|
118
|
+ if (function_exists("imagecopyresampled")) {
|
|
|
|
119
|
+ $newim = imagecreatetruecolor($newwidth, $newheight);
|
|
|
|
120
|
+ imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
|
|
|
|
121
|
+ } else {
|
|
|
|
122
|
+ $newim = imagecreate($newwidth, $newheight);
|
|
|
|
123
|
+ imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
|
|
|
|
124
|
+ }
|
|
|
|
125
|
+
|
|
|
|
126
|
+ imagejpeg($newim, $dest);
|
|
|
|
127
|
+ imagedestroy($newim);
|
|
|
|
128
|
+ } else {
|
|
|
|
129
|
+ imagejpeg($im, $dest);
|
|
|
|
130
|
+ }
|
|
|
|
131
|
+ }
|
|
|
|
132
|
+ /**
|
|
|
|
133
|
+ * 解决手机上传图片被旋转问题
|
|
|
|
134
|
+ * @param string $full_filename 文件路径
|
|
|
|
135
|
+ */
|
|
|
|
136
|
+ function adjustPicOrientation($full_filename){
|
|
|
|
137
|
+ $exif = exif_read_data($full_filename);
|
|
|
|
138
|
+ if($exif && isset($exif['Orientation'])) {
|
|
|
|
139
|
+ $orientation = $exif['Orientation'];
|
|
|
|
140
|
+ if($orientation != 1){
|
|
|
|
141
|
+ $img = imagecreatefromjpeg($full_filename);
|
|
|
|
142
|
+
|
|
|
|
143
|
+ $mirror = false;
|
|
|
|
144
|
+ $deg = 0;
|
|
|
|
145
|
+
|
|
|
|
146
|
+ switch ($orientation) {
|
|
|
|
147
|
+ case 2:
|
|
|
|
148
|
+ $mirror = true;
|
|
|
|
149
|
+ break;
|
|
|
|
150
|
+ case 3:
|
|
|
|
151
|
+ $deg = 180;
|
|
|
|
152
|
+ break;
|
|
|
|
153
|
+ case 4:
|
|
|
|
154
|
+ $deg = 180;
|
|
|
|
155
|
+ $mirror = true;
|
|
|
|
156
|
+ break;
|
|
|
|
157
|
+ case 5:
|
|
|
|
158
|
+ $deg = 270;
|
|
|
|
159
|
+ $mirror = true;
|
|
|
|
160
|
+ break;
|
|
|
|
161
|
+ case 6:
|
|
|
|
162
|
+ $deg = 270;
|
|
|
|
163
|
+ break;
|
|
|
|
164
|
+ case 7:
|
|
|
|
165
|
+ $deg = 90;
|
|
|
|
166
|
+ $mirror = true;
|
|
|
|
167
|
+ break;
|
|
|
|
168
|
+ case 8:
|
|
|
|
169
|
+ $deg = 90;
|
|
|
|
170
|
+ break;
|
|
|
|
171
|
+ }
|
|
|
|
172
|
+ if ($deg) $img = imagerotate($img, $deg, 0);
|
|
|
|
173
|
+ if ($mirror) $img = _mirrorImage($img);
|
|
|
|
174
|
+ //$full_filename = str_replace('.jpg', "-O$orientation.jpg", $full_filename);新文件名
|
|
|
|
175
|
+ imagejpeg($img, $full_filename, 95);
|
|
|
|
176
|
+ }
|
|
|
|
177
|
+ }
|
|
|
|
178
|
+ return $full_filename;
|
|
|
|
179
|
+ }
|
|
|
|
180
|
+} |