PHP给图片增多水印得类

Jun 13, 2016 am 10:39 AM
image text this

PHP给图片增加水印得类

<?php/*+--------------------------------------| 生成加水印的图片类 (支持水印为图片或者文字)| ============================| by JackMing (感谢Dash和其他不知道姓名的朋友支持,本类在这些朋友作品的基础上创建)+--------------------------------------*/Class Gimage{var $src_image_name = ""; //输入图片的文件名(必须包含路径名)var $jpeg_quality = 90; //jpeg图片质量var $save_image_file = ''; //输出文件名var $wm_image_name = ""; //水印图片的文件名(必须包含路径名)var $wm_image_pos = 1; //水印图片放置的位置// 0 = middle// 1 = top left// 2 = top right// 3 = bottom right// 4 = bottom left// 5 = top middle// 6 = middle right// 7 = bottom middle// 8 = middle left//other = 3var $wm_image_transition = 20; //水印图片与原图片的融合度 (1=100)var $wm_text = ""; //水印文字(支持中英文以及带有\r\n的跨行文字)var $wm_text_size = 20; //水印文字大小var $wm_text_angle = 4; //水印文字角度,这个值尽量不要更改var $wm_text_pos = 3; //水印文字放置位置var $wm_text_font = ""; //水印文字的字体var $wm_text_color = "#cccccc"; //水印字体的颜色值function create($filename=""){if ($filename) $this->src_image_name = strtolower(trim($filename));$src_image_type = $this->get_type($this->src_image_name);$src_image = $this->createImage($src_image_type,$this->src_image_name);if (!$src_image) return;$src_image_w=ImageSX($src_image);$src_image_h=ImageSY($src_image);if ($this->wm_image_name){$this->wm_image_name = strtolower(trim($this->wm_image_name));$wm_image_type = $this->get_type($this->wm_image_name);$wm_image = $this->createImage($wm_image_type,$this->wm_image_name);$wm_image_w=ImageSX($wm_image);$wm_image_h=ImageSY($wm_image);$temp_wm_image = $this->getPos($src_image_w,$src_image_h,$this->wm_image_pos,$wm_image);$wm_image_x = $temp_wm_image["dest_x"];$wm_image_y = $temp_wm_image["dest_y"];imageCopyMerge($src_image, $wm_image,$wm_image_x,$wm_image_y,0,0,$wm_image_w,$wm_image_h,$this->wm_image_transition);}if ($this->wm_text){$this->wm_text = $this->gb2utf8($this->wm_text);$temp_wm_text = $this->getPos($src_image_w,$src_image_h,$this->wm_text_pos);$wm_text_x = $temp_wm_text["dest_x"];$wm_text_y = $temp_wm_text["dest_y"];if(preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_text_color, $color)){$red = hexdec($color[1]);$green = hexdec($color[2]);$blue = hexdec($color[3]);$wm_text_color = imagecolorallocate($src_image, $red,$green,$blue);}else{$wm_text_color = imagecolorallocate($src_image, 255,255,255);}imagettftext($src_image, $this->wm_text_size, $this->wm_angle, $wm_text_x, $wm_text_y, $wm_text_color,$this->wm_text_font,$this->wm_text);}if ($this->save_file){switch ($this->output_type){case 'gif':$src_img=ImagePNG($src_image, $this->save_file); break;case 'jpeg':$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;case 'png':$src_img=ImagePNG($src_image, $this->save_file); break;default:$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;}}else{if ($src_image_type = "jpg") $src_image_type="jpeg";header("Content-type: image/{$src_image_type}");switch ($src_image_type){case 'gif':$src_img=ImagePNG($src_image); break;case 'jpg':$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;case 'png':$src_img=ImagePNG($src_image);break;default:$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;}}imagedestroy($src_image);}/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/*createImage 根据文件名和类型创建图片内部函数$type: 图片的类型,包括gif,jpg,png$img_name:图片文件名,包括路径名,例如 " ./mouse.jpg"*/function createImage($type,$img_name){if (!$type){$type = $this->get_type($img_name);}switch ($type){case 'gif':if (function_exists('imagecreatefromgif'))[email&#160;protected]($img_name);break;case 'jpg':$tmp_img=ImageCreateFromJPEG($img_name);break;case 'png':$tmp_img=ImageCreateFromPNG($img_name);break;default:$tmp_img=ImageCreateFromString($img_name);break;}return $tmp_img;}/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++getPos 根据源图像的长、宽,位置代码,水印图片id来生成把水印放置到源图像中的位置内部函数$sourcefile_width: 源图像的宽$sourcefile_height: 原图像的高$pos: 位置代码// 0 = middle// 1 = top left// 2 = top right// 3 = bottom right// 4 = bottom left// 5 = top middle// 6 = middle right// 7 = bottom middle// 8 = middle left$wm_image: 水印图片ID*/function getPos($sourcefile_width,$sourcefile_height,$pos,$wm_image=""){if($wm_image){$insertfile_width = ImageSx($wm_image);$insertfile_height = ImageSy($wm_image);}else {$lineCount = explode("\r\n",$this->wm_text);$fontSize = imagettfbbox($this->wm_text_size,$this->wm_text_angle,$this->wm_text_font,$this->wm_text);$insertfile_width = $fontSize[2] - $fontSize[0];$insertfile_height = count($lineCount)*($fontSize[1] - $fontSize[3]);}switch ($pos){case 0:$dest_x = ( $sourcefile_width / 2 ) - ( $insertfile_width / 2 );$dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );break;case 1:$dest_x = 0;if ($this->wm_text){$dest_y = $insertfile_height;}else{$dest_y = 0;}break;case 2:$dest_x = $sourcefile_width - $insertfile_width;if ($this->wm_text){$dest_y = $insertfile_height;}else{$dest_y = 0;}break;case 3:$dest_x = $sourcefile_width - $insertfile_width;$dest_y = $sourcefile_height - $insertfile_height;break;case 4:$dest_x = 0;$dest_y = $sourcefile_height - $insertfile_height;break;case 5:$dest_x = ( ( $sourcefile_width - $insertfile_width ) / 2 );if ($this->wm_text){$dest_y = $insertfile_height;}else{$dest_y = 0;}break;case 6:$dest_x = $sourcefile_width - $insertfile_width;$dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );break;case 7:$dest_x = ( ( $sourcefile_width - $insertfile_width ) / 2 );$dest_y = $sourcefile_height - $insertfile_height;break;case 8:$dest_x = 0;$dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );break;default:$dest_x = $sourcefile_width - $insertfile_width;$dest_y = $sourcefile_height - $insertfile_height;break;}return array("dest_x"=>$dest_x,"dest_y"=>$dest_y);}/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++gb2utf8 指定的文字转换为UTF-8格式,包括中英文混合内部函数*/function gb2utf8($gb){if(!trim($gb))return $gb;$filename="./gb2312.txt";$tmp=file($filename);$codetable=array();while(list($key,$value)=each($tmp))$codetable[hexdec(substr($value,0,6))]=substr($value,7,6);$utf8="";while($gb){if (ord(substr($gb,0,1))>127){$tthis=substr($gb,0,2);$gb=substr($gb,2,strlen($gb)-2);$utf8.=$this->u2utf8(hexdec($codetable[hexdec(bin2hex($tthis))-0x8080]));}else{$tthis=substr($gb,0,1);$gb=substr($gb,1,strlen($gb)-1);$utf8.=$this->u2utf8($tthis);}}return $utf8;}function u2utf8($c){$str="";if ($c < 0x80){$str.=$c;}else if ($c < 0x800){$str.=chr(0xC0 | $c>>6);$str.=chr(0x80 | $c & 0x3F);}else if ($c < 0x10000){$str.=chr(0xE0 | $c>>12);$str.=chr(0x80 | $c>>6 & 0x3F);$str.=chr(0x80 | $c & 0x3F);}else if ($c < 0x200000){$str.=chr(0xF0 | $c>>18);$str.=chr(0x80 | $c>>12 & 0x3F);$str.=chr(0x80 | $c>>6 & 0x3F);$str.=chr(0x80 | $c & 0x3F);}return $str;}/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++get_type 获得图片的格式,包括jpg,png,gif内部函数$img_name: 图片文件名,可以包括路径名*/function get_type($img_name)//获取图像文件类型{$name_array = explode(".",$img_name);if (preg_match("/\.(jpg|jpeg|gif|png)$/", $img_name, $matches)){$type = strtolower($matches[1]);}else{$type = "string";}return $type;}}?>
ログイン後にコピー


使用方法:
$img = new Gimage();
$img->wm_text = "www.discuz.com";
$img->wm_text_font = "./STXINWEI.TTF";
$img->create("./mouse.jpg");

就可以了,其中
mouse.jpg是你要在其上添加水印的图片名称,注意包含路径名
STXINWEI.TTF是字体文件的路径名+文件名
这就是一个简单的测试。如果要调整更复杂的显示效果,只要修改一下类中的属性就可以了,例如把字体放大就可以
$img->wm_text_size = 20;
增加水印图片就可以
$img->wm_image_name="文件名";

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Bing Image Creatorを無料で使用する方法 Bing Image Creatorを無料で使用する方法 Feb 27, 2024 am 11:04 AM

この記事では、無料の BingImageCreator を使用して高品質の出力を取得する 7 つの方法を紹介します。 BingImageCreator (現在は ImageCreator for Microsoft Designer として知られています) は、優れたオンライン人工知能アート ジェネレーターの 1 つです。ユーザーのプロンプトに基づいて、非常にリアルな視覚効果を生成します。プロンプトがより具体的、明確、創造的であればあるほど、より良い結果が得られます。 BingImageCreator は、高品質の画像の作成において大きな進歩を遂げました。 Dall-E3 トレーニング モードを使用するようになり、より高いレベルの詳細とリアリズムが表示されます。ただし、一貫して HD 結果を生成できるかどうかは、高速な処理などのいくつかの要因によって決まります。

Xiaomi 携帯電話から画像を削除する方法 Xiaomi 携帯電話から画像を削除する方法 Mar 02, 2024 pm 05:34 PM

Xiaomi 携帯電話で画像を削除する方法? Xiaomi 携帯電話で画像を削除できますが、ほとんどのユーザーは画像の削除方法を知りません. 次に、編集者が提供する Xiaomi 携帯電話での画像の削除方法に関するチュートリアルです。ぜひご参加ください。 Xiaomi 携帯電話で画像を削除する方法 1. まず Xiaomi 携帯電話の [アルバム] 機能を開きます; 2. 次に、不要な写真を確認して右下の [削除] ボタンをクリックします; 3. 次に、[アルバム] をクリックします。上部にある特別な領域に入り、[ごみ箱]を選択します; 4. 次に、下図に示すように[ごみ箱を空にする]を直接クリックします; 5. 最後に[完全に削除]を直接クリックして完了します。

Imagemagic のインストール Centos と Image のインストール チュートリアル Imagemagic のインストール Centos と Image のインストール チュートリアル Feb 12, 2024 pm 05:27 PM

LINUX はオープン ソース オペレーティング システムです。その柔軟性とカスタマイズ性により、多くの開発者やシステム管理者が最初に選択します。LINUX システムでは、画像処理は非常に重要なタスクであり、Imagemagick と Image は 2 つの非常に人気のある画像処理ツールです。この記事では、Centos システムに Imagemagick と Image をインストールする方法を紹介し、詳細なインストール チュートリアルを提供します。 Imagemagic のインストール Centos チュートリアル Imagemagick は、コマンド ラインでさまざまな画像操作を実行できる強力な画像処理ツールセットです。Centos システムに Imagemagick をインストールする手順は次のとおりです: 1

この点を理解してフロントエンド担当者の7割をキャッチアップした記事 この点を理解してフロントエンド担当者の7割をキャッチアップした記事 Sep 06, 2022 pm 05:03 PM

同僚は、これによって指摘されたバグのために立ち往生しました。Vue2 のこの指摘の問題により、アロー関数が使用され、その結果、対応する props を取得できなくなりました。私がそれを彼に紹介したとき、彼はそれを知りませんでした。その後、私はわざとフロントエンド コミュニケーション グループに目を向けました。これまでのところ、フロントエンド プログラマーの少なくとも 70% はまだそれを理解していません。今日私はそれを共有しますyou this link. もしすべてが間違っている場合 まだ学習していない場合は、大きな口を与えてください。

Java でこのメソッドを使用する方法 Java でこのメソッドを使用する方法 Apr 18, 2023 pm 01:58 PM

1. this キーワード 1. this の型: どのオブジェクトが呼び出されるかがそのオブジェクトの参照型 2. 使用法の概要 1. this.data;//アクセス属性 2. this.func();// アクセスメソッド 3.this ( );//このクラス内の他のコンストラクタを呼び出す 3. 使い方の説明 1.this.dataはメンバメソッドで使用しているので、これを追加しないとどうなるかを見てみましょう classMyDate{publicintyear;publicintmonth;publicintday; publicvoidsetDate(intyear, intmonth,intday){はい

jQueryでのthisの利用スキルの分析 jQueryでのthisの利用スキルの分析 Feb 22, 2024 pm 08:54 PM

jQuery は、Web 開発における DOM 操作やイベント処理に広く使用されている人気の JavaScript ライブラリです。重要な概念の 1 つは、このキーワードの使用です。 jQuery では、これは現在操作されている DOM 要素を表しますが、コンテキストが異なると、このポインタが異なる場合があります。この記事では、具体的なコード例を通じて、jQuery での this の使用スキルを分析します。まず、簡単な例を見てみましょう。

JavaScriptのアロー関数で詳しく解説 JavaScriptのアロー関数で詳しく解説 Jan 25, 2024 pm 01:41 PM

JavaScript のアロー関数は比較的新しい構文です。独自の this キーワードはありません。逆に、アロー関数の this は、それを含むスコープ オブジェクトを指します。影響は次のとおりです: 1. アロー関数の this は次のとおりです。 static; 2. アロー関数はコンストラクタとして使用できません; 3. アロー関数はメソッドとして使用できません。

これは何ですか? JavaScript でのこれの詳細な分析 これは何ですか? JavaScript でのこれの詳細な分析 Aug 04, 2022 pm 05:02 PM

これは何ですか?次の記事では、JavaScript でのこれについて説明し、関数の呼び出し方法の違いによる違いについて説明します。

See all articles