激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - PHP圖片水印類的封裝

PHP圖片水印類的封裝

2021-06-02 17:29東東東蔚 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
<?php
header('Content-type:text/html;charset=utf8');
$img = new Image();
// $img->water('2a.jpg','logo.gif',0);
class Image{
  //路徑
  protected $path;
  //是否啟用隨機名字
  protected $isRandName;
  //要保存的圖像類型
  protected $type;
  
  //通過構造方法隊成員屬性進行初始化
  function __construct($path='./',$isRandName=true,$type='png'){
    $this->path = $path;
    $this->isRandName = $isRandName;
    $this->type = $type;
  }
  //對外公開的水印方法
  
  /**
   * @param char $image  原圖
   * @param char $water  水印圖片
   * @param char $postion 位置
   * @param int $tmp   透明度
   * @param char $prefix 前綴
   */
  function water($image,$water,$postion,$tmp=100,$prefix='water_'){
    //判斷這兩個圖片是否存在
    if(!file_exists($image)||!file_exists($water)){
      die('圖片資源不存在');
    }
    //得到原圖和水印圖片的寬高
    $imageInfo = self::getImageInfo($image);
    $waterInfo = self::getImageInfo($water);
    //判斷水印圖片是否能貼上來
    if (!$this->checkImage($imageInfo,$waterInfo)){
      die('水印圖片太大');
    }
    //打開圖片
    $imageRes = self::openAnyImage($image);
    $waterRes = self::openAnyImage($water);
    //根據水印圖片的位置計算水印圖片的坐標
    $pos = $this->getPosition($postion,$imageInfo,$waterInfo);
    //將水印圖片貼過來
    imagecopymerge($imageRes, $waterRes, $pos['x'], $pos['y'], 0, 0, $waterInfo["width"], $waterInfo["height"], $tmp);
    //得到要保存圖片的文件名
    $newName = $this->createNewName($image,$prefix);
    //得到保存圖片的路徑,也就是文件的全路徑
    $newPath = rtrim($this->path,'/').'/'.$newName;
    //保存圖片
    $this->saveImage($imageRes,$newPath);
    //銷毀資源
    imagedestroy($imageRes);
    imagedestroy($waterRes);
    
    //返回路徑
    return $newPath;
  }
  //保存圖像資源
  protected function saveImage($imageRes,$newPath){
    $func = 'image'.$this->type;
    //通過變量函數進行保存
    $func($imageRes,$newPath);
  }
  //得到文件名函數
  protected function createNewName($imagePath,$prefix){
    if ($this->isRandName){
      $name = $prefix.uniqid().'.'.$this->type;
    }else {
      $name = $prefix.pathinfo($imagePath)['filename'].'.'.$this->type;
    }
    return $name;
  }
  //根據位置計算水印圖片的坐標
  protected function getPosition($postion,$imageInfo,$waterInfo){
    switch ($postion){
      case 1:
        $x = 0;
        $y = 0;
        break;
      case 2:
        $x = ($imageInfo['width']-$waterInfo["width"])/2;
        $y = 0;
        break;
      case 3:
        $x = $imageInfo["width"]- $waterInfo["width"];
        $y = 0;
        break;
      case 4:
        $x = 0;
        $y = ($imageInfo["height"]-$waterInfo["height"])/2;
        break;
      case 5:
        $x = ($imageInfo['width']-$waterInfo["width"])/2;
        $y = ($imageInfo["height"]-$waterInfo["height"])/2;
        break;
      case 6:
        $x = $imageInfo["width"]- $waterInfo["width"];
        $y = ($imageInfo["height"]-$waterInfo["height"])/2;
        break;
      case 7:
        $x = 0;
        $y = $imageInfo['height'] - $waterInfo["height"];
        break;
      case 8:
        $x = ($imageInfo['width']-$waterInfo["width"])/2;
        $y = $imageInfo['height'] - $waterInfo["height"];
        break;
      case 9:
        $x = $imageInfo["width"]- $waterInfo["width"];
        $y = $imageInfo['height'] - $waterInfo["height"];
        break;
      case 0:
        $x = mt_rand(0, $imageInfo["width"]- $waterInfo["width"]);
        $y = mt_rand(0, $imageInfo['height'] - $waterInfo["height"]);
        break;
    }
    return ['x'=>$x , 'y'=>$y];
  }
  protected function checkImage($imageInfo,$waterInfo){
    if (($waterInfo['width'] > $imageInfo['width'])||($waterInfo['height'] > $imageInfo['height'])){
      return false;
    }
    return true;
  }
  //靜態方法。根據圖片的路徑得到圖片的信息,寬度,高度、mime類型
  static function getImageInfo($imagePath){
    $info = getimagesize($imagePath);
    $data['width']=$info[0];
    $data['height']=$info[1];
    $data['mime'] = $info['mime'];
    return $data;
  }
  static function openAnyImage($imagePath){
    //得到圖像的mime類型
    $mime = self::getImageInfo($imagePath)['mime'];
    //根據不同的mime類型打開不同的圖像
    switch ($mime){
      case 'image/png':
          $image = imagecreatefrompng($imagePath);
          break;
      case 'image/gif':
          $image = imagecreatefromgif($imagePath);
          break;
      case 'image/jpeg':
          $image = imagecreatefromjpeg($imagePath);
          break;
      case 'image/wbmp':
          $image = imagecreatefromwbmp($imagePath);
          break;
    }
    return $image;
  }
  
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲综人网 | 99精品视频久久精品视频 | 久久草在线看 | 国产国语毛片 | 亚洲精品v天堂中文字幕 | 成人三级黄色片 | 国产精品高潮视频 | 国产精品视频六区 | 国产精品成人久久久久a级 欧美特黄一级高清免费的香蕉 | 午夜国产小视频 | 黄色网电影 | 国产在线精品一区二区不卡 | 一级做受毛片免费大片 | 91久久线看在观草草青青 | 成人毛片100部免费观看 | 国产一区二区亚洲 | 青草av.久久免费一区 | 天天夜碰日日摸日日澡性色av | 羞羞视频免费观看网站 | 综合国产在线 | 免费一级在线 | 狠狠干b| av免费在线观看国产 | 99精品视频一区二区三区 | 黄wwww| 久久久久久久久久久久久久久伊免 | 精品一区二区久久久久 | av黄色片网站| 欧美人xx| 激情网站在线观看 | 午色影院 | 欧美一级黄色录像片 | 蜜桃一本色道久久综合亚洲精品冫 | 日本在线视 | 91色综合综合热五月激情 | 亚洲男人一区 | a视频在线免费观看 | 青草av.久久免费一区 | 最新日韩中文字幕 | 免费一及片 | 污黄视频在线观看 |