基于PHP封装图片裁剪工具类

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
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
dst_file = $distFile;
        $this->basisUploadPath = storage_path( 'app/uploads/' );
        if( isset( $file ) && $file )
        {
            $this->src_file = $file;
            $this->init( $file );
        }
    }
  
     
    /**
     * 生成唯一的文件名称
     * @author   Administrator
     * @datetime 2019年12月24日 上午11:44:02
     * @comment
     *
     * @param string $salt
     * @return string
     */
    protected function getUniqueDiskName($salt = '')
    {
        if( empty( $salt ) )
        {
            $salt = mt_rand(1,1000000);
        }
        list($usec, $sec) = explode(" ", microtime());
         
        $micros = str_replace('.', '', ((float)$usec + (float)$sec)).$salt;
         
        return md5( $micros );
    }
     
     
    /**
     * 初始化参数
     * @author   Administrator
     * @datetime 2020年7月22日 下午3:00:22
     * @comment
     *
     * @return boolean
     */
    public function init( $url )
    {
        $strExt = $this->getImgExt( $url );
        $filename = $this->getUniqueDiskName( md5( $url ) );
        $path = date( 'y' ) . '/' . date( 'm' ) . '/' . date( 'd' ) . '/' . $filename;
         
        $dowRes = new generalDowmload( $url$path . $strExt );
        if( !empty( $dowRes ) && isset( $dowRes->basename ) )
        {
            if( isset( $dowRes->location ) && strlen( $dowRes->location ) )
            {
                $this->src_file = $dowRes->location;
            }
            else
            {
                $this->src_file = $this->basisUploadPath .  $path . $strExt;
            }
        }
        else
        {
            return false;
        }
        if( !isset( $this->src_file ) || is_null( $this->src_file ) || !file_exists( $this->src_file ) )
        {
            return false;
        }
         
        $arrImageInfos = @getimagesize( $this->src_file );
        if( !isset( $arrImageInfos ) || empty( $arrImageInfos ) )
        {
            return false;
        }
        if( isset( $arrImageInfos[0] ) && $arrImageInfos[0] )
        {
            $this->src_width    = $arrImageInfos[0];
        }
        if( isset( $arrImageInfos[1] ) && $arrImageInfos[1] )
        {
            $this->src_height   = $arrImageInfos[1];
        }
        $this->src_type = 2;
        if( isset( $arrImageInfos[2] ) && $arrImageInfos[2] )
        {
            $this->src_type = $arrImageInfos[2];
        }
        if( isset( $arrImageInfos[ 'mime' ] ) )
        {
            $this->mime     = $arrImageInfos[ 'mime' ];
        }
        switch( $this->src_type )
        {
            case IMAGETYPE_JPEG :
                ini_set( 'gd.jpeg_ignore_warning', true );
                $this->sImage   =  @imagecreatefromjpeg( $this->src_file );
                $this->ext      =  'jpg';
                if( !isset( $this->mime ) || strlen( $this->mime ) mime = 'image/jpeg';
                }
            break;
            case IMAGETYPE_PNG :
                $this->sImage   =  @imagecreatefrompng( $this->src_file );
                $this->ext      =  'png';
                if( !isset( $this->mime ) || strlen( $this->mime ) mime = 'image/' . $this->ext;
                }
            break;
            case IMAGETYPE_GIF :
                $this->sImage   =  imagecreatefromgif( $this->src_file );
                $this->ext      =  'gif';
                if( !isset( $this->mime ) || strlen( $this->mime ) mime = 'image/' . $this->ext;;
                }
            break;
            case 18:
                $this->sImage   = @imagecreatefromwebp( $this->src_file );
                $this->ext      =  'webp';
                if( !isset( $this->mime ) || strlen( $this->mime ) mime = 'image/' . $this->ext;;
                }
            break;
            default:
                return false;
        }
         
        return true;
    }
     
     
     
    /**
     * 裁剪
     * @author   Administrator
     * @datetime 2020年7月22日 下午3:07:36
     * @comment
     *
     * @param unknown $dst_width
     * @param unknown $dst_height
     * @param unknown $dst_x
     * @param unknown $dst_y
     * @param string $dst_file
     * @return boolean
     */
    public function cutImage( $dst_width, $dst_height, $dst_x, $dst_y, $originWidth, $originHeight )
    {
        if( !$dst_width || !$dst_height )
        {
            return false;
        }
  
        # 创建画布时,判断最终需要的画布大小
        if ($originWidth && $originHeight)
        {
            $dst_w = $originWidth;
            $dst_h = $originHeight;
        }
        else
        {
            $dst_w = $dst_width;
            $dst_h = $dst_height;
        }
  
        $this->dImage = imagecreatetruecolor( $dst_w, $dst_h ); //创建了目标文件的大小的画布
        $bg = imagecolorallocatealpha( $this->dImage, 255, 255, 255, 127 ); //给画布分配颜色
        imagefill( $this->dImage, 0, 0, $bg ); //给图像用颜色进行填充
        imagecolortransparent( $this->dImage, $bg ); //背景定义成透明色
         
        $ratio_w = 1.0 * $dst_width / $this->src_width; //横向缩放的比例
        $ratio_h = 1.0 * $dst_height / $this->src_height; //纵向缩放的比例
        //不进行缩放,直接对图像进行裁剪
        $ratio = 1.0;
        $tmp_w = (int)($dst_width / $ratio);
        $tmp_h = (int)($dst_height / $ratio);
         
        $tmp_img = imagecreatetruecolor( $dst_width, $dst_height ); //创建暂时保存的画布
        imagecopy( $tmp_img, $this->sImage, 0, 0, $dst_x,$dst_y, $dst_width, $dst_height ); //拷贝出图像的一部分,进行裁切
        imagecopyresampled( $this->dImage, $tmp_img, 0, 0, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h ); //把暂时缓存的图片,放到目标文件里面
        imagedestroy( $tmp_img );
  
        return true;
    }
     
     
    /**
     * 存储
     * @author   Administrator
     * @datetime 2020年7月22日 下午3:15:52
     * @comment
     *
     * @param unknown $file
     * @return boolean
     */
    public function save( $file = null )
    {
        if( !isset( $file ) || is_null( $file ) )
        {
            $this->dst_file = $this->src_file;
        }
        else
        {
            $this->dst_file = $this->basisUploadPath . '/'. $file;
        }
        try{
            switch( $this->src_type )
            {
                case IMAGETYPE_JPEG :
                    @imagejpeg( $this->dImage, $this->dst_file, 98 );
                break;
                case IMAGETYPE_PNG :
                    imagepng( $this->dImage, $this->dst_file );
                break;
                case IMAGETYPE_GIF :
                    imagegif( $this->dImage, $this->dst_file );
                break;
                case 18:
                    @imagejpeg( $this->dImage, $this->dst_file, 98 );
                break;
                default:
                    return false;
            }
        }catch( Exception $e ){
             
        }
         
        $strExt = $this->getImgExt( $this->dst_file );
         
        $tmpImageInfo = @getimagesize( $this->dst_file );
        $width = 0;
        $height = 0;
        if( isset( $tmpImageInfo[0] ) && $tmpImageInfo[0] > 0 )
        {
            $width = $tmpImageInfo[0];
        }
        if( isset( $tmpImageInfo[1] ) && $tmpImageInfo[1] > 0 )
        {
            $height = $tmpImageInfo[1];
        }
         
        $objRet = new stdClass();
        $objRet->mime       = $this->mime;
        $objRet->filename   = basename( $this->dst_file );
        $objRet->ext        = $strExt;
        $objRet->width      = $width;
        $objRet->height     = $height;
         
        return $objRet;
    }
     
     
    /**
     * 数据销毁
     * @author   Administrator
     * @datetime 2020年7月22日 下午3:31:12
     * @comment
     *
     */
    public function destroy()
    {
        imagedestroy( $this->sImage);
        imagedestroy( $this->dImage );
        @unlink( $this->src_file );
         
        return true;
    }
  
    /**
     * 检索图集是否存在后缀
     *  若不存在-则使用默认(强制转换)
     * @author   Administrator
     * @datetime 2018年11月27日  下午3:30:47
     * @comment
     *
     * @param unknown $url
     * @return string|unknown
     */
    protected function getImgExt( $url )
    {
         
        $iLastSlash = strrpos( $url, '/' );
        $strFileName = mb_substr( $url, intval($iLastSlash+1) );
        $strExt = strrchr( $strFileName, '.' );
        preg_match( "/(.*?)?.*?/", $strExt, $matches );
        if( !empty( $matches ) && isset( $matches[1] ) )
        {
            $strExt = $matches[1];
        }
        if( false == $strExt || is_null( $strExt ) || strlen( $strExt )

知识补充

除了上文的内容,小编还为大家整理了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
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
info=getimagesize($src);
        $this->type=image_type_to_extension($this->info['2'],false);
        $fun="imagecreatefrom{$this->type}";
        $this->image=$fun($src);
    }    /**
     * 文字水印
     * @param  [type]  $font     字体
     * @param  [type]  $content  内容
     * @param  [type]  $size     文字大小
     * @param  [type]  $col      文字颜色(四元数组)
     * @param  array   $location 位置
     * @param  integer $angle    倾斜角度
     * @return [type]          
     */
    public function fontMark($font,$content,$size,$col,$location,$angle=0){
        $col=imagecolorallocatealpha($this->image, $col['0'], $col['1'], $col['2'],$col['3']);
 
        imagettftext($this->image, $size, $angle, $location['0'], $location['1'], $col,$font,$content);
    }   
    /**
     * 图片水印
     * @param  [type] $imageMark 水印图片地址
     * @param  [type] $dst       水印图片在原图片中的位置
     * @param  [type] $pct       透明度
     * @return [type]           
     */
    public function imageMark($imageMark,$dst,$pct){
        $info2=getimagesize($imageMark);
        $type=image_type_to_extension($info2['2'],false);
        $func2="imagecreatefrom".$type;
        $water=$func2($imageMark);
 
        imagecopymerge($this->image, $water, $dst[0], $dst[1], 0, 0, $info2['0'], $info2['1'], $pct);
        imagedestroy($water);
 
    }    /**
     * 压缩图片
     * @param  [type] $thumbSize 压缩图片大小
     * @return [type]            [description]     */
    public function thumb($thumbSize){
        $imageThumb=imagecreatetruecolor($thumbSize[0], $thumbSize[1]);
         
        imagecopyresampled($imageThumb, $this->image, 0, 0, 0, 0, $thumbSize[0], $thumbSize[1], $this->info['0'], $this->info['1']);
        imagedestroy($this->image);
        $this->image=$imageThumb;
    }    /**
    * 裁剪图片
     * @param  [type] $cutSize  裁剪大小
     * @param  [type] $location 裁剪位置
     * @return [type]           [description]     */
     public function cut($cutSize,$location){
         $imageCut=imagecreatetruecolor($cutSize[0],$cutSize[1]);
 
         imagecopyresampled($imageCut, $this->image, 0, 0, $location[0], $location[1],$cutSize[0],$cutSize[1],$cutSize[0],$cutSize[1]);
         imagedestroy($this->image);
         $this->image=$imageCut;
     }    /**
     * 展现图片
     * @return [type] [description]     */
    public function show(){
        header("content-type:".$this->info['mime']);
 
        $funn="image".$this->type;
 
        $funn($this->image);
    }    /**
     * 保存图片
 * @param  [type] $newname 新图片名
 * @return [type]          [description] */
     public function save($newname){
         header("content-type:".$this->info['mime']);
 
         $funn="image".$this->type;
 
         $funn($this->image,$newname.'.'.$this->type);
     }     public function destruct(){
         imagedestroy($this->image);
     }
 
 }

以上就是基于PHP封装图片裁剪工具类的详细内容,更多关于PHP图片裁剪的资料请关注IT俱乐部其它相关文章!

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/code/php/11115.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部