首页 电脑 电脑学堂 查看内容

PHP给图片添加水印 压缩 剪切的封装类

2015-8-18 12:07 1479 0

摘要: 给图片添加水印,其实就是把原来的图片和水印添加在一起,下面小编把最近整理的资料分享给大家。 php对图片文件的操作主要是利用GD库扩展。当我们频繁利用php对图片进行操作时,会自然封装很多函数,否则会写太多重 ...
关键词: nbsp Image 图片 type 水印 upload break param case location

给图片添加水印,其实就是把原来的图片和水印添加在一起,下面小编把最近整理的资料分享给大家。

php对图片文件的操作主要是利用GD库扩展。当我们频繁利用php对图片进行操作时,会自然封装很多函数,否则会写太多重复的代码。当有很多对图片的相关函数的时候,我们可以考虑将这些函数也整理一下,因而就有了封装成类的想法。

  操作图片主要历经四个步骤:

        第一步:打开图片

        第二步:操作图片

        第三步:输出图片

        第四步:销毁图片

  1,3,4三个步骤每次都要写,每次又都差不多。真正需要变通的只有操作图片的这一步骤了。操作图片又往往通过1或多个主要的GD函数来完成。

  本文封装类里面的四种方法,文字水印(imagettftext()),图片水印(imagecopymerge()),图片压缩,图片剪切(imagecopyresampled()),其余的常用GD函数便不赘述。

直接上代码:

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
<?php
class Image
  private $info;
  private $image;
  public $type;
  public function __construct($src)
  {
    $this->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);
   }
 }
 ?>

如果还需要其他操作,只需要再往这个类里面添加就好啦~~

给图片添加水印代码:

先看文件check_image_addwatermark.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
<?php
//修改图片效果
$db = mysql_connect('localhost','root','Ctrip07185419') or die('can not connect to database');
mysql_select_db('moviesite',$db) or die(mysql_error($db));
//上传文件的路径
$dir = 'D:\Serious\phpdev\test\images';
//设置环境变量
putenv('GDFONTPATH='.'C:\Windows\Fonts');
$font = "arial";
//upload_image.php页面传递过来的参数,如果是上传图片
if($_POST['submit'] == 'Upload')
{
  if($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK)
  {
    switch($_FILES['uploadfile']['error'])
    {
      case UPLOAD_ERR_INI_SIZE:
        die('The uploaded file exceeds the upload_max_filesize directive');
      break;
      case UPLOAD_ERR_FORM_SIZE:
        die('The upload file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form');
      break;
      case UPLOAD_ERR_PARTIAL:
        die('The uploaded file was only partially uploaded');
      break;
      case UPLOAD_ERR_NO_FILE:
        die('No file was uploaded');
      break;
      case UPLOAD_ERR_NO_TMP_DIR:
        die('The server is missing a temporary folder');
      break
      case UPLOAD_ERR_CANT_WRITE:
        die('The server fail to write the uploaded file to the disk');
      break;   
      case UPLOAD_ERR_EXTENSION:
        die('The upload stopped by extension');
      break;       
    }
  }
  $image_caption = $_POST['caption'];
  $image_username = $_POST['username'];
  $image_date = date('Y-m-d');
  list($width,$height,$type,$attr) = getimagesize($_FILES['uploadfile']['tmp_name']);
  $error = 'The file you upload is not a supported filetype';
  switch($type)
  {
    case IMAGETYPE_GIF:
      $image = imagecreatefromgif($_FILES['uploadfile']['tmp_name']) or die($error);
    break;
    case IMAGETYPE_JPEG:
      $image = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']) or die($error);
    break;
    case IMAGETYPE_PNG:
      $image = imagecreatefrompng($_FILES['uploadfile']['tmp_name']) or die($error);
    break;
    default:
    break;
  }
  $query = 'insert into images(image_caption,image_username,image_date) values("'.$image_caption.'" , "'.$image_username.'","'.$image_date.'")';
  $result = mysql_query($query,$db) or die(mysql_error($db));
  $last_id = mysql_insert_id();
  // $imagename = $last_id.'.jpg';
  // imagejpeg($image,$dir.'/'.$imagename);
  // imagedestroy($image);
  $image_id = $last_id;
  imagejpeg($image , $dir.'/'.$image_id.'.jpg');
  imagedestroy($image);
}
else //如果图片已经上传,则从数据库中取图片名字
{
  $query = 'select image_id,image_caption,image_username,image_date from images where image_id='.$_POST['id'];
  $result = mysql_query($query,$db) or die(mysql_error($db));
  extract(mysql_fetch_assoc($result));
  list($width,$height,$type,$attr) = getimagesize($dir.'/'.$image_id.'.jpg');
}
//如果是保存图片
if($_POST['submit'] == 'Save')
{
  if(isset($_POST['id']) && ctype_digit($_POST['id']) && file_exists($dir.'/'.$_POST['id'].'.jpg'))
  {
    $image = imagecreatefromjpeg($dir.'/'.$_POST['id'].'.jpg');
  }
  else
  {
    die('invalid image specified');
  }
  $effect = (isset($_POST['effect'])) ? $_POST['effect'] : -1;
  switch($effect)
  {
    case IMG_FILTER_NEGATE:
      imagefilter($image , IMG_FILTER_NEGATE);   //将图像中所有颜色反转
    break;
    case IMG_FILTER_GRAYSCALE:
      imagefilter($image , IMG_FILTER_GRAYSCALE); //将图像转换为灰度的
    break;
    case IMG_FILTER_EMBOSS:
      imagefilter($image , IMG_FILTER_EMBOSS);   //使图像浮雕化
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

最新评论