ホームページ  >  記事  >  バックエンド開発  >  PHP 画像透かしコード_PHP チュートリアル

PHP 画像透かしコード_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-21 15:16:30835ブラウズ

テキスト透かしと画像透かしをサポート
透かしのランダムまたは固定位置をサポート (9 正方形グリッド)
透かしの透明度設定 (画像とテキストの透かしの両方がサポートされます)
テキスト透かしのフォント、色、サイズを設定します
画像の背景透かしは透明です

コードをコピーします コードは次のとおりです:

/**
* 透かしクラスを追加し、テキストと画像の透かしの透明度設定、透かし画像の透明な背景をサポートします。
* 日付: 2011-09-27
* 著者: www.jb51.net
* 使用法:
* $obj = new WaterMask($imgFileName) //オブジェクトをインスタンス化します
* $obj->$waterType = 1 ; //タイプ: 0 はテキストの透かし、1 は画像の透かしです
* $obj->$transparent = 45; //透かしの透明度
* $obj->$waterStr = 'www.jb51.net';ウォーターマークのテキスト
* $obj->$fontSize = 16; //テキストのフォントサイズ
* $obj->$fontColor = array(255,0255); //ウォーターマークのテキストの色 (RGB)
* $obj-> ;$fontFile = = 'AHGBold.ttf'; //フォント ファイル
* $obj->output(); //出力ウォーターマーク画像ファイルは入力画像ファイルを上書きします
*/
class WaterMask{
public $waterType = 1; //ウォーターマーク タイプ: 0 はテキスト ウォーターマーク、1 は画像ウォーターマーク
public $pos = 0; Position
public $transparent = 45; //ウォーターマークの透明度

public $waterStr = 'www.jb51.net' //ウォーターマークのテキスト
public $fontSize = 16; public $fontColor = array(255; ,0,255); //ウォーターマークの文字色 (RGB)
public $fontFile = 'AHGBold.ttf'; //フォント ファイル

public $waterImg = 'logo.png' //ウォーターマーク画像

; '; //透かしが必要な画像
private $im = ''; //画像ハンドル
private $srcImg_info = ''; WaterImg_info = ''; //ウォーターマーク画像情報
private $str_w = ''; //ウォーターマークテキスト幅
private $x = ''; private $y = ''; //ウォーターマーク y 座標

function __construct($img) { //Destructor
$this->srcImg = file_exists($img) ? $img : die('"'.$img . '" ソース ファイルが存在しません!');
}
private function imginfo() { //透かしを入れる必要がある画像に関する情報を取得し、画像をロードします。
$this->srcImg_info = getimagesize($this->srcImg);
switch ($this->srcImg_info[2]) {
ケース 3:
$this->im = imagecreatefrompng($this-> ;srcImg);
ブレイク 1;
$this->im = imagecreatefromjpeg($this->srcImg);
ブレイク 1:
$this->im = imagecreatefromgif($this) ->srcImg);
break 1;
default:
die('元の画像 ('.$this->srcImg.') の形式が正しくありません。サポートされているのは PNG、JPEG、および GIF のみです。') ;
}
}
private function Waterimginfo() { //ウォーターマーク画像の情報を取得し、画像を読み込みます。
$this->waterImg_info = getimagesize($this->waterImg);
switch ($this->waterImg_info[2]) {
ケース 3:
$this->water_im = imagecreatefrompng($this-> ;waterImg);
ブレイク 1;
$this->water_im = imagecreatefromjpeg($this->waterImg);
ブレイク 1:
$this->water_im = imagecreatefromgif($this) ->waterImg);
break 1;
default:
die('ウォーターマーク画像 ('.$this->srcImg.') は、PNG、JPEG、GIF のみをサポートしています。');
}
}
private function Waterpos() { //水印位置算法
switch ($this->pos) {
case 0: //随机位置
$this->x = rand(0,$this- >srcImg_info[0]-$this->waterImg_info[0]);
$this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]);
休憩1;
ケース 1: //上左
$this->x = 0;
$this->y = 0;
休憩1;
ケース 2: //上中
$this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
$this->y = 0;
休憩1;
ケース 3: //上右
$this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
$this->y = 0;
休憩1;
ケース 4: //中左
$this->x = 0;
$this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
休憩1;
ケース 5: //中中
$this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
$this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
休憩1;
ケース 6: //中右
$this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
$this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
休憩1;
ケース 7: //下左
$this->x = 0;
$this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
休憩1;
ケース 8: //下中
$this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
$this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
休憩1;
デフォルト: //下右
$this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
$this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
休憩1;
}
}
プライベート関数 Waterimg() {
if ($this->srcImg_info[0] <= $this->waterImg_info[0] || $this->srcImg_info[1] <= $ this->waterImg_info[1]){
die('水印比原图大!');
}
$this->waterpos();
$cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);
imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1] );
$pct = $this->透明;
imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);
imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1] ,$pct);
}
プライベート関数waterstr() {
$rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);
$w = abs($rect[2]-$rect[6]);
$h = abs($rect[3]-$rect[7]);
$fontHeight = $this->fontSize;
$this->water_im = imagecreatetruecolor($w, $h);
imagealphablending($this->water_im,false);
imagesavealpha($this->water_im,true);
$white_alpha = imagecolorallocatealpha($this->water_im,255,255,255,127);
imagefill($this->water_im,0,0,$white_alpha);
$color = imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr);
$this->waterImg_info = array(0=>$w,1=>$h);
$this->waterimg();
}
関数出力() {
$this->imginfo();
if ($this->waterType == 0) {
$this->waterstr();
}else {
$this->waterimginfo();
$this->waterimg();
}
switch ($this->srcImg_info[2]) {
ケース 3:
imagepng($this->im,$this->srcImg);
休憩1;
ケース 2:
imagejpeg($this->im,$this->srcImg);
休憩1;
ケース 1:
imagegif($this->im,$this->srcImg);
休憩1;
デフォルト:
die('添加水印失败!');
休憩;
}
imagedestroy($this->im);
imagedestroy($this->water_im);
}
}
?>

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/325912.html技術記事文字水印、絵片水印をサポート 水印の位置随机または固定(九格) 水印透明度設定(絵片水印と文字水印都サポート) 文字水印の文字、色、...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。