PHP中简单图片处理类

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
/*
* 作者: 雪狐博客
* 类用途: 处理图片,包括加水印等
**/
    final class Image { 
    private $file
    private $image
    private $info
               
    public function __construct( $file ) { 
        if ( file_exists ( $file )) { 
            $this ->file = $file
       
            $info = getimagesize ( $file ); 
       
            $this ->info = array
                'width'  => $info [0], 
                'height' => $info [1], 
                'bits'   => $info [ 'bits' ], 
                'mime'   => $info [ 'mime'
            ); 
                   
            $this ->image = $this ->create( $file ); 
        } else
            exit ( 'Error: Could not load image ' . $file . '!' ); 
       
   
               
    private function create( $image ) { 
        $mime = $this ->info[ 'mime' ]; 
               
        if ( $mime == 'image/gif' ) { 
            return imagecreatefromgif( $image ); 
        } elseif ( $mime == 'image/png' ) { 
            return imagecreatefrompng( $image ); 
        } elseif ( $mime == 'image/jpeg' ) { 
            return imagecreatefromjpeg( $image ); 
       
    }    
           
    public function save( $file , $quality = 100) { 
        $info = pathinfo ( $file ); 
        $extension = $info [ 'extension' ]; 
          
        if ( $extension == ( 'jpeg' || 'jpg' )) { 
            imagejpeg( $this ->image, $file , $quality ); 
        } elseif ( $extension == 'png' ) { 
            imagepng( $this ->image, $file , 0); 
        } elseif ( $extension == 'gif' ) { 
            imagegif( $this ->image, $file ); 
       
                  
        imagedestroy( $this ->image); 
    }        
           
    public function resize( $width = 0, $height = 0) { 
        if (! $this ->info[ 'width' ] || ! $this ->info[ 'height' ]) { 
            return
       
       
        $xpos = 0; 
        $ypos = 0; 
       
        $scale = min( $width / $this ->info[ 'width' ], $height / $this ->info[ 'height' ]); 
               
        if ( $scale == 1) { 
            return
       
               
        $new_width = (int)( $this ->info[ 'width' ] * $scale ); 
        $new_height = (int)( $this ->info[ 'height' ] * $scale );          
        $xpos = (int)(( $width - $new_width ) / 2); 
        $ypos = (int)(( $height - $new_height ) / 2); 
                               
        $image_old = $this ->image; 
        $this ->image = imagecreatetruecolor( $width , $height ); 
                   
        $background = imagecolorallocate( $this ->image, 255, 255, 255); 
        imagefilledrectangle( $this ->image, 0, 0, $width , $height , $background ); 
           
        imagecopyresampled( $this ->image, $image_old , $xpos , $ypos , 0, 0, $new_width , $new_height , $this ->info[ 'width' ], $this ->info[ 'height' ]); 
        imagedestroy( $image_old ); 
                  
        $this ->info[ 'width' ]  = $width
        $this ->info[ 'height' ] = $height
   
           
    public function watermark( $file , $position = 'bottomright' ) { 
        $watermark = $this ->create( $file ); 
               
        $watermark_width = imagesx( $watermark ); 
        $watermark_height = imagesy( $watermark ); 
               
        switch ( $position ) { 
            case 'topleft'
                $watermark_pos_x = 0; 
                $watermark_pos_y = 0; 
                break
            case 'topright'
                $watermark_pos_x = $this ->info[ 'width' ] - $watermark_width
                $watermark_pos_y = 0; 
                break
            case 'bottomleft'
                $watermark_pos_x = 0; 
                $watermark_pos_y = $this ->info[ 'height' ] - $watermark_height
                break
            case 'bottomright'
                $watermark_pos_x = $this ->info[ 'width' ] - $watermark_width
                $watermark_pos_y = $this ->info[ 'height' ] - $watermark_height
                break
       
               
        imagecopy( $this ->image, $watermark , $watermark_pos_x , $watermark_pos_y , 0, 0, 120, 40); 
               
        imagedestroy( $watermark ); 
   
           
    public function crop( $top_x , $top_y , $bottom_x , $bottom_y ) { 
        $image_old = $this ->image; 
        $this ->image = imagecreatetruecolor( $bottom_x - $top_x , $bottom_y - $top_y ); 
               
        imagecopy( $this ->image, $image_old , 0, 0, $top_x , $top_y , $this ->info[ 'width' ], $this ->info[ 'height' ]); 
        imagedestroy( $image_old ); 
               
        $this ->info[ 'width' ] = $bottom_x - $top_x
        $this ->info[ 'height' ] = $bottom_y - $top_y
   
           
    public function rotate( $degree , $color = 'FFFFFF' ) { 
        $rgb = $this ->html2rgb( $color ); 
               
        $this ->image = imagerotate( $this ->image, $degree , imagecolorallocate( $this ->image, $rgb [0], $rgb [1], $rgb [2])); 
               
        $this ->info[ 'width' ] = imagesx( $this ->image); 
        $this ->info[ 'height' ] = imagesy( $this ->image); 
   
               
    private function filter( $filter ) { 
        imagefilter( $this ->image, $filter ); 
   
                   
    private function text( $text , $x = 0, $y = 0, $size = 5, $color = '000000' ) { 
        $rgb = $this ->html2rgb( $color ); 
               
        imagestring( $this ->image, $size , $x , $y , $text , imagecolorallocate( $this ->image, $rgb [0], $rgb [1], $rgb [2])); 
   
           
    private function merge( $file , $x = 0, $y = 0, $opacity = 100) { 
        $merge = $this ->create( $file ); 
       
        $merge_width = imagesx( $image ); 
        $merge_height = imagesy( $image ); 
                       
        imagecopymerge( $this ->image, $merge , $x , $y , 0, 0, $merge_width , $merge_height , $opacity ); 
   
                   
    private function html2rgb( $color ) { 
        if ( $color [0] == '#' ) { 
            $color = substr ( $color , 1); 
       
               
        if ( strlen ( $color ) == 6) { 
            list( $r , $g , $b ) = array ( $color [0] . $color [1], $color [2] . $color [3], $color [4] . $color [5]);    
        } elseif ( strlen ( $color ) == 3) { 
            list( $r , $g , $b ) = array ( $color [0] . $color [0], $color [1] . $color [1], $color [2] . $color [2]);     
        } else
            return FALSE; 
       
               
        $r = hexdec( $r );  
        $g = hexdec( $g );  
        $b = hexdec( $b );     
               
        return array ( $r , $g , $b ); 
    }    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值