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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?php
/**
 * @author wolf
 *
 */
classImage {
    //类开始
    public$originimage="";//源图片文件地址
    public$imageext="";//源图片格式
    public$thumbimage="";//缩略图文件存放地址
    public$thumb_maxwidth= 1440;//缩略图最大宽度
    public$thumb_maxheight= 900;//缩略图最大高度
    public$watermark_text="";//水印文字内容
    public$watermark_minwidth= 300;//源图片最小宽度:大于此值时加水印
    public$watermark_minheight= 200;//源图片最小高度:大于此值时加水印
    public$watermark_fontfile="";//字体文件
    public$watermark_fontsize= 14;//字体大小
    public$watermark_logo="config/water.png";//水印LOGO地址
    public$watermark_transparent= 20;//水印LOGO不透明度
    private$origin_width= 0;//源图片宽度
    private$origin_height= 0;//源图片高度
    private$imageQuilty= 90;//图片质量
    private$tmp_originimage="";//临时图片(源图片)
    private$tmp_thumbimage="";//临时图片(缩略图)
    private$tmp_waterimage="";//临时图片(水印LOGO)
    private$_waterPosition= 2;//1正中间 2右下角
    //生成缩略图
    publicfunctiongen_thumbimage() {
        if($this->originimage ==""||$this->thumbimage =="") {
            return0;
        }
        $this->get_oriwidthheight ();
        if($this->origin_width <$this->thumb_maxwidth &&$this->origin_height <$this->thumb_maxheight) {
            $this->thumb_maxwidth =$this->origin_width;
            $this->thumb_maxheight =$this->origin_height;
        }else{
            if($this->origin_width <$this->origin_height) {
                $this->thumb_maxwidth = ($this->thumb_maxheight /$this->origin_height) *$this->origin_width;
            }else{
                $this->thumb_maxheight = ($this->thumb_maxwidth /$this->origin_width) *$this->origin_height;
            }
        }
        $this->get_imagetype ();
        $this->gen_tmpimage_origin ();
        $this->gen_tmpimage_thumb ();
        if($this->tmp_originimage ==""||$this->tmp_thumbimage =="") {
            return- 1;
        }
        imagecopyresampled ($this->tmp_thumbimage,$this->tmp_originimage, 0, 0, 0, 0,$this->thumb_maxwidth,$this->thumb_maxheight,$this->origin_width,$this->origin_height );
        switch($this->imageext) {
            case"gif":
                imagegif ($this->tmp_thumbimage,$this->thumbimage );
                return1;
                break;
            case"jpg":
                imagejpeg ($this->tmp_thumbimage,$this->thumbimage,$this->imageQuilty );
                return2;
                break;
            case"png":
                imagepng ($this->tmp_thumbimage,$this->thumbimage );
                return3;
                break;
            default:
                return- 2;
                break;
        }
    }
    //添加文字水印
    publicfunctionadd_watermark1() {
        if($this->originimage ==""||$this->watermark_text ==""||$this->watermark_fontfile =="") {
            return0;
        }
        $this->get_oriwidthheight ();
        if($this->origin_width <$this->watermark_minwidth ||$this->origin_height <$this->watermark_minheight) {
            return0;
        }
        $this->get_imagetype ();
        $this->gen_tmpimage_origin ();
        if($this->tmp_originimage =="") {
            return- 1;
        }
        $textcolor= imagecolorallocate ($this->tmp_originimage, 255, 0, 0 );
        $angle= 0;
        $px=$this->origin_width / 2 - 200;
        $py=$this->origin_height / 2 - 10;
        imagettftext ($this->tmp_originimage,$this->watermark_fontsize,$angle,$px,$py,$textcolor,$this->watermark_fontfile,$this->watermark_text );
        switch($this->imageext) {
            case"gif":
                imagegif ($this->tmp_originimage,$this->originimage );
                return1;
                break;
            case"jpg":
                imagejpeg ($this->tmp_originimage,$this->originimage,$this->imageQuilty );
                return2;
                break;
            case"png":
                imagepng ($this->tmp_originimage,$this->originimage );
                return3;
                break;
            default:
                return- 2;
                break;
        }
    }
    //添加LOGO水印
    publicfunctionadd_watermark2() {
        if($this->originimage ==""||$this->watermark_logo =="") {
            return0;
        }
        $this->get_oriwidthheight ();
        if($this->origin_width <$this->watermark_minwidth ||$this->origin_height <$this->watermark_minheight) {
            return0;
        }
        $this->get_imagetype ();
        $this->gen_tmpimage_origin ();
        $this->gen_tmpimage_waterlogo ();
        if($this->tmp_originimage ==""||$this->tmp_waterimage =="") {
            return- 1;
        }
        list ($logo_width,$logo_height) =getimagesize($this->watermark_logo );
        //正中间
        $waterZb=$this->waterPosition ($logo_width,$logo_height);
        $px=$waterZb['x'];
        $py=$waterZb['y'];
        imagecopymerge ($this->tmp_originimage,$this->tmp_waterimage,$px,$py, 0, 0,$logo_width,$logo_height,$this->watermark_transparent );
        switch($this->imageext) {
            case"gif":
                imagegif ($this->tmp_originimage,$this->originimage );
                return1;
                break;
            case"jpg":
                imagejpeg ($this->tmp_originimage,$this->originimage,$this->imageQuilty );
                return2;
                break;
            case"png":
                imagepng ($this->tmp_originimage,$this->originimage );
                return3;
                break;
            default:
                return- 2;
                break;
        }
    }
      
    /**
     * 上传缩略图
     * 注意上传文件大小限制
     *@param String $files $_FILES['upload'] 类型
     *@param String  $path  存储的目录 默认在/static/attached/
     *@param boolean $isWater 是否添加水印
     * @return string $filePath 网页url图片路径
     */
    publicfunctionupload($files,$path,$isWater) {
        if(is_uploaded_file($files['tmp_name'] )) {
            $upfile=$files;
            $name=$upfile[name];
            $type=$upfile[type];
            $size=$upfile[size];
            $tmp_name=$upfile[tmp_name];
            $error=$upfile[error];
              
            if($size> 1048576) {
                returnarray('status'=> false,'message'=>"$name图片太大超过1MB");
            }
              
            $rs=$this->getImageSize($tmp_name);
            if(!$rs['status']) {
                $rs['message'] =$name.$rs['message'];
                return$rs;
            }
              
            // 创建文件夹
            $save_path=getcwd() ."/static/attached/".$path."/";
            $save_url="./static/attached/".$path."/";
            $ym=date("Ym");
            $save_path.=$ym."/";
            $save_url.=$ym."/";
            if(!file_exists($save_path)) {
                mkdir($save_path);
            }
              
            if($error=='0') {
                $fileType=substr($name,strpos($name,".") + 1 );
                $prefix=$this->getRandPrefix ();
                $newName=date("YmdHi") .$prefix.".".$fileType;
                $filepath=$save_path.$newName;
                move_uploaded_file ($tmp_name,$filepath);
              
            }
              
            if($isWater) {
                $this->water ($filepath);
            }
              
            returnarray('status'=> true,'message'=>$save_url.$newName);
        }
    }
      
    /**
     * 图片增加水印处理
     * @param unknown_type $image
     */
    publicfunctionwater($image) {
        $this->watermark_logo = ROOT .$this->watermark_logo;
        $this->originimage =$image;
        //LOGO水印
        $this->add_watermark2 ();
    }
      
    /**
     *
     * 获取随机前缀
     */
    privatefunctiongetRandPrefix() {
        $string="abcdefghijklmnopqrstuvwxyz0123456789";
        $prefix='';
        for($i= 0;$i< 4;$i++) {
            $rand= rand ( 0, 33 );
            $prefix.=$string{$rand};
        }
        return$prefix;
    }
      
    //检测图片大小
    privatefunctiongetImageSize($image) {
        list ($width,$height,$type,$attr) =getimagesize($image);
          
        if($type!= 2 &&$type!= 3) {
            returnarray('status'=> false,'message'=>"图片格式不正确,请上传JPG或者PNG图片".$type);
        }
        //检测图片大小
        if($width> 1440) {
            returnarray('status'=> false,'message'=>"图片宽度请小于1440px,当前为".$width."px");
        }
          
        if($height> 900) {
            returnarray('status'=> false,'message'=>"图片高度请小于900px,当前为".$height."px");
          
        }
        returnarray('status'=> true );
    }
      
    /**
     * 生成缩略图
     *
     * @param String $imagefile 原始文件
     * @param String $thumbWidth  缩略图宽度
     * @param String $thumbHeight 缩略图高度
     * @return String 缩略图url        
     */
    publicfunctionreduceImage($imagefile,$thumbWidth,$thumbHeight,$path="thumb") {
          
        // 生成缩略图
        $dir=date("Ym", time () );
        $imagefile= ROOT .$imagefile;
        $imagefile_s= ROOT ."static/attached/".$path."/".$dir."/s_".basename($imagefile);
        $imagetrans=newImage ();
        $imagetrans->originimage =$imagefile;
        $imagetrans->thumbimage =$imagefile_s;
        $imagetrans->thumb_maxwidth =$thumbWidth;
        $imagetrans->thumb_maxheight =$thumbHeight;
        $isokid=$imagetrans->gen_thumbimage ();
          
        return"./static/attached/".$path."/".$dir."/s_".basename($imagefile);
      
    }
    /**
     * 水印位置
     * @param int $logo_width
     * @param int $logo_height
     * @return 水印坐标
     */
    privatefunctionwaterPosition($logo_width,$logo_height) {
        switch($this->_waterPosition) {
            case1 :
                $px=$this->origin_width / 2 -$logo_width/ 2;
                $py=$this->origin_height / 2 -$logo_height/ 2;
                break;
            case2 :
                $px=$this->origin_width -$logo_width- 10;
                $py=$this->origin_height -$logo_height- 10;
                break;
            default:
                $px=$this->origin_width / 2 -$logo_width/ 2;
                $py=$this->origin_height / 2 -$logo_height/ 2;
                break;
        }
          
        returnarray('x'=>$px,'y'=>$py);
    }
    //获取图片尺寸
    privatefunctionget_oriwidthheight() {
        list ($this->origin_width,$this->origin_height ) =getimagesize($this->originimage );
        return1;
    }
    /*
     * 检测图片格式
     * 原方法需要开启exif 扩展
     */
    privatefunctionget_imagetype() {
        $ext=$this->getImgext ($this->originimage );
        switch($ext) {
            case1 :
                $this->imageext ="gif";
                break;
            case2 :
                $this->imageext ="jpg";
                break;
            case3 :
                $this->imageext ="png";
                break;
            default:
                $this->imageext ="unknown";
                break;
        }
    }
    //创建临时图片(源图片)
    privatefunctiongen_tmpimage_origin() {
        $ext=$this->getImgext ($this->originimage );
        switch($ext) {
            case1 :
                $this->tmp_originimage = imagecreatefromgif ($this->originimage );
                $bgcolor= imagecolorallocate ($this->tmp_originimage, 0, 0, 0 );
                $bgcolortrans= imagecolortransparent ($this->tmp_originimage,$bgcolor);
                break;
            case2 :
                $this->tmp_originimage = imagecreatefromjpeg ($this->originimage );
                break;
            case3 :
                $this->tmp_originimage = imagecreatefrompng ($this->originimage );
                imagesavealpha ($this->tmp_originimage, true );
                break;
            default:
                $this->tmp_originimage ="";
                break;
        }
    }
    //创建临时图片(缩略图)
    privatefunctiongen_tmpimage_thumb() {
        $ext=$this->getImgext ($this->originimage );
        switch($ext) {
            case1 :
                $this->tmp_thumbimage = imagecreatetruecolor ($this->thumb_maxwidth,$this->thumb_maxheight );
                $bgcolor= imagecolorallocate ($this->tmp_thumbimage, 255, 255, 255 );
                imagefill ($this->tmp_thumbimage, 0, 0,$bgcolor);
                break;
            case2 :
                $this->tmp_thumbimage = imagecreatetruecolor ($this->thumb_maxwidth,$this->thumb_maxheight );
                break;
            case3 :
                $this->tmp_thumbimage = imagecreatetruecolor ($this->thumb_maxwidth,$this->thumb_maxheight );
                $bgcolor= imagecolorallocate ($this->tmp_thumbimage, 255, 255, 255 );
                imagefill ($this->tmp_thumbimage, 0, 0,$bgcolor);
                imagealphablending ($this->tmp_thumbimage, false );
                imagesavealpha ($this->tmp_thumbimage, true );
                break;
            default:
                $this->tmp_thumbimage ="";
                break;
        }
    }
    //创建临时图片(LOGO水印)
    privatefunctiongen_tmpimage_waterlogo() {
        $ext=$this->getImgext ($this->watermark_logo );
        switch($ext) {
            case1 :
                $this->tmp_waterimage = imagecreatefromgif ($this->watermark_logo );
                $bgcolor= imagecolorallocate ($this->tmp_waterimage, 0, 0, 0 );
                $bgcolortrans= imagecolortransparent ($this->tmp_waterimage,$bgcolor);
                break;
            case2 :
                $this->tmp_waterimage = imagecreatefromjpeg ($this->watermark_logo );
                break;
            case3 :
                $this->tmp_waterimage = imagecreatefrompng ($this->watermark_logo );
                imagesavealpha ($this->tmp_waterimage, true );
                break;
            default:
                $this->tmp_waterimage ="";
                break;
        }
    }
    /*
     * 获取后缀名
     */
    publicfunctiongetImgext($filename) {
        returnexif_imagetype ($filename);
    }
    //释放资源
    publicfunction__destruct() {
        if(is_object($this->tmp_originimage ) == true) {
            imagedestroy ($this->tmp_originimage );
        }
        if(is_object($this->tmp_thumbimage ) == true) {
            imagedestroy ($this->tmp_thumbimage );
        }
        if(is_object($this->tmp_waterimage ) == true) {
            imagedestroy ($this->tmp_waterimage );
        }
    }
}
转载请注明来源:php 图片操作类,支持生成缩略图,添加水印,上传缩略图

  哈尔滨品用软件有限公司致力于为哈尔滨的中小企业制作大气、美观的优秀网站,并且能够搭建符合百度排名规范的网站基底,使您的网站无需额外费用,即可稳步提升排名至首页。欢迎体验最佳的哈尔滨网站建设