検索
ホームページphp教程php手册便利な透かしとサムネイル用の PHP グラフィックス クラス

便利な透かしとサムネイル用の PHP グラフィックス クラス

Jun 13, 2016 pm 12:24 PM
phpコードそしてグラフィックスコピー透かし親切

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



/*
*@author 27262681@qq.com
*著作権 http://www.gowake.cn
*/

class img {
function __construct($arg = null) {
$args = func_get_args();
if($arg == null) {
return
🎜> if(function_exists('image'.$func)) {
$funcstr = 'image'.$func;
}elseif (function_exists($func)){
$funcstr = $func;
} else {
error("そのようなクラスメソッドまたは関数はありません"); /*
*Create image
*@param string/int 画像ファイルのパスまたは幅
*@param整数高さ、省略可能
*@param string 6桁の16進整数
*/
function create($arg = null) {
$args = func_get_args()
if(is_file) ($args[0])) {
$this->file = $args[0 ];
$size = getimagesize($this->file) または error('画像タイプ エラー'); : $size;
$type = image_type_to_extension($size[2],false);
$this->type = $this->type ? > args; [1]>0) {
$im = imagecreatetruecolor((int)$args[0],(int)$args[1]) または error(「申し訳ありません、パラメータが間違っています! ");
else {
$color = hexdec(str_replace('#','',$args[2]));
$this ->サイズ = $this ->サイズ? $this->サイズ: array((int)$args[0] ,(int)$args[1]);
、パラメータエラーです!");
}
//imagealphablending($im,false);//これらの 2 行は、透明チャネルを記録するために使用されます
imageavealpha($im,true); );//インターレースを有効にする
return $im;
}

/*
*サムネイルを生成
*@param int $w 新しい画像の幅
* @param int $h new picture
*@param string/bool $color オプション、新しい画像の背景色、透明にする場合は false または空
*@param bool $lashen オプション、ストレッチするかどうか、デフォルトはストレッチしない
*/
function suolue($w = null,$h = null,$color = false,$lashen = false) {
$w_o = imagex($this->im ); 🎜> } elseif ($w != null および $h == null){ ;
$im = $this->create($w,$h,$color);
$w_n = $w ;
$h_n = $h;
if($w_o/ $h_o > $w/$h) {
$h_n = $w*$h_o/$w_o; ($ h- $ h_n)/2; this->im,$x,$y,0,0,$w_n,$h_n,$w_o,$h_o);
//imagedestroy($this->im); >im = $im;
$im を返す
}

/*
*画像に書き込みます
*@param string $str 書き込む文字列
*@param array $arg 左の連想配列である文字列関連パラメータ。は左からの距離、右は右からの距離、左が優先、上は上からの距離、下は下からの距離、上は角度、色は6桁です。 16 進数の色、touming はテキストの透明度、フォント フォント ファイルの場合
*/
function write($str = '' , $arg = array()) {
$size = $arg['size '] $arg['size'] : 20;
$angle = $arg['angle'] : 0 ; ? $arg['color'] : '000000 ';
$touming = $arg['touming'] : 100;
$touming = dechex((100-$touming) )*127/100);
$ color = hexdec($touming.str_replace("#","",$color));
$font = $arg['font'] $arg[' font'] : 'arial.ttf';
$boxarr = imagettfbbox($size,$angle,$font,$str)
$w = imagex($this->im); $h = imagey($this->im );

$x_l = $x_r = $boxarr[0]
$y_t = $y_b = $boxarr[1]; i 2) {
$x_l = $boxarr[$i] $x_r = $boxarr ? [$i] : $x_r; $y_t = $boxarr[$i 1] > $boxarr[$i 1] > i 1] : $y_b;
}
$width = $x_r - $x_l;

/*正確なオフセットを取得します*/
$im = $this->create($width*4,$height*4);
$tm = hexdec('7fffffff');
4;$i ) {
for( $ii=0;$ii if(imagecolorat($im,$i,$ii) != $tm) {
$x_l 🎜> for($i =0;$i for($ii=$x_l;$ii if(imagecolorat($im,$ii) ,$i) != $tm) {
y_t = $i; _
for($i=$width*4-1;$i>0;$i--) {
for($ii=$y_t;$ii if(imagecolorat($im,$i,$ii) != $tm) {
$x_r = $i;
休憩(2);

}
}
for($i=$height*4-1;$i>0;$i--) {
for($ii=$x_) l;$ ii if(imagecolorat($im,$ii,$i) != $tm) {
$y_b = $i;
休憩(2);

}
}
$x_off = $x_l - $width*2;
$y_off = $y_b - $height*2;
$width = $x_r - $x_l; //精确宽度
$height = $y_b - $y_t; //精确高
imagedestroy($im);

if(isset($arg['left'])) {
$x = (int)$arg['left'] - $x_off;
}elseif (isset($arg['right'])){
$x = $w - (int)$arg['right'] - $width - $x_off;
}else {
$x = ($w - $width)/2 - $x_off;
}
if(isset($arg['top'])) {
$y = (int)$arg['top'] - $y_off $height;
}elseif (isset($arg['bottom'])){
$y = $h - (int)$arg['bottom'] - $y_off;
}else {
$y = ($h $height)/2 - $y_off;
}

imagettftext($this->im,$size,$angle,$x,$y,$color,$font,$str);
$this->im; を返します。
}

/*
*結合并图片(图片水影)
*@param string/resource $file 図片文件路径またはこの图片标识符
*@param array $arg 文字列に関連するパラメータ、1 つの関連数グループ、左は左边距離、右は右边距離、左は上、上は距離上、下は下位、上は文字透明度
*/
function merge($file,$arg = array()) {
if(is_file($file)) {
$imc = $this->create($file);
}elseif(gettype($file)=='resource') {
$imc = $file;
}else {
error("没有图片");
}
$touming = $arg['touming'] ? (int)$arg['touming'] : 100 ;
$w = 画像x($this->im);
$h = imagey($this->im);
$width = imagex($imc);
$height = imagey($imc);
if(isset($arg['left'])) {
$x = (int)$arg['left'];
}elseif (isset($arg['right'])){
$x = $w - (int)$arg['right'] - $width;
}else {
$x = ($w - $width)/2;
}
if(isset($arg['top'])) {
$y = (int)$arg['top'];
}elseif (isset($arg['bottom'])){
$y = $h - $height - $arg['bottom'];
}else {
$y = ($h - $height)/2;
}
imagecopymergegray($this->im,$imc,$x,$y,0,0,$width,$height,$touming);
}

/*
*出力図
*@param string $type
*@param string $filename 要转存文件路径
*@param int $ zhiliang jpeg画像特有の、画像清晰度
*/
function display($type = null,$filename = null,$zhiliang = null) {
if($type == null) {
$type = $this->type ? $this->type : 'jpg';
}
if(($type == 'jpeg' または $type == 'jpg') および $zhiliang == null) {
$type = 'jpeg';
$zhiliang = 100;
}
if($filename == null) {
header('Content-type: image/'.$type);
}
$displayfunc = 'image'.$type;
$displayfunc($this->im,$filename,$zhiliang);
imagedestroy($this->im);
}

function randcolor($a,$b) {
$a = $a>255 ? 255 : (int)$a;
$a = $a $b = $b>255 ? 255 : (int)$b;
$b = $b for($i=0;$i $color .= str_pad(dechex(mt_rand($a,$b)), 2, "0", STR_PAD_LEFT);
}
$color を返します。
}
}

/*
関数 error($msg,$debug = false) {
$err = new Exception($msg);
$str = "

n<span style="color:red">错误:</span>n".print_r($err->getTrace() ,1)."n
";
if($debug == true) {
file_put_contents(date('Y-m-d').".log",$str);
$str; を返します。
}else{
die($str);
}
}
*/
?>

これは简单的用法实例

复制代代码如下:


$img = new img('a.png');
$m = $img->im;
$im = $img->suolue(100);
$img->im = $m;
$img->suolue(300);
$img->merge($m,array('left'=>0,'top'=>0,'touming'=>60));
$img->merge($im,array('right'=>0,'top'=>0,'touming'=>60));
$img->merge($im,array('left'=>0,'bottom'=>0,'touming'=>60));
$img->merge($im,array('right'=>0,'bottom'=>0,'touming'=>60));

$img->write("春天来了",array('left'=>0,'top'=>0,'size'=>30,'color'=> ;$img->randcolor(0,180),'angle'=>-45,'font'=>'simfang.ttf','touming'=>80));
$img->write("春天来了",array('left'=>0,'bottom'=>0,'size'=>30,'color'=>$img ->randcolor(0,180),'angle'=>45,'font'=>'simfang.ttf','touming'=>80));
$img->write("春天来了",array('right'=>0,'bottom'=>0,'size'=>30,'color'=>$img) ->randcolor(0,180),'angle'=>-45,'font'=>'simfang.ttf','touming'=>80));
$img->write("春天来了",array('right'=>0,'top'=>0,'size'=>30,'color'=>$img) ->randcolor(0,180),'angle'=>45,'font'=>'simfang.ttf','touming'=>80));
$img->display("gif");

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、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ヘンタイを無料で生成します。

ホットツール

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン