ホームページ  >  記事  >  バックエンド開発  >  PHP多機能画像処理クラス共有(php画像スケーリングクラス)_PHPチュートリアル

PHP多機能画像処理クラス共有(php画像スケーリングクラス)_PHPチュートリアル

WBOY
WBOYオリジナル
2016-07-13 10:35:56711ブラウズ

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

/**
* 画像のインデントと透かしの追加を完了するために使用される基本的な画像処理
* 透かし画像がターゲット画像サイズを超える場合、透かし画像は自動的にターゲット画像に適応して縮小されます
* 透かし画像は、ターゲット画像とマージするように設定できます背景
*/

/*
使用法:
自動トリミング:
プログラムは、画像のサイズに従って中央から最大の正方形をトリミングし、画像のサイズに従って縮小します。ターゲットサイズ

$t--->setSrcImg("img/test.jpg");
$t->setCutType(1);//この文は OK
$t->setDstImg("tmp/ new_test .jpg");
$t->createImg(60,60);

手動トリミング:
プログラムは、指定された位置に従ってソース画像から画像を取得します

$t->setSrcImg(" img/test .jpg");
$t->setCutType(2);//手動カットを指示する
$t->setSrcCutPosition(100, 100);//ソース画像の開始点の座標
$ t->setRectangleCut( 300, 200); // トリミングサイズ $t->setDstImg("tmp/new_test.jpg");
*/
class ThumbHandler
{
var $dst_img ;// 対象ファイル
var $img_display_quality = 80;// 画像の表示品質、デフォルトは 75
var $img_scale = 0;// 画像のスケーリング率
var $src_w = 0;// オリジナル画像width
var $src_h = 0;// 元の画像の高さ
var $dst_w = 0;// 新しい画像の合計幅
var $dst_h = 0;// 新しい画像の合計高さ
var $fill_w; //塗りつぶされた図形の幅
var $fill_h;//塗りつぶされた図形の高さ
var $copy_w;/ /グラフィックスの幅をコピー
var $copy_h;//グラフィックスの高さをコピー
var $src_x = 0;//元画像描画の開始横座標
var $src_y = 0;// 元画像描画の開始縦座標
var $start_x ;// 新しい画像描画の開始横座標
var $start_y;// 新しい画像描画の開始縦座標画像
var $mask_word;// ウォーターマーク テキスト
var $mask_img;// ウォーターマーク画像
var $mask_pos_x = 0;/ / ウォーターマークの横座標
var $mask_pos_y = 0;// ウォーターマークの縦座標
var $mask_offset_x = 5;//ウォーターマークの水平オフセット
var $mask_offset_y = 5;// ウォーターマークの垂直オフセット
var $font_w;// ウォーターマークのフォント幅
var $font_h;// ウォーターマークのフォントの高さ
var $mask_w;// ウォーターマークの幅
var $mask_h; // ウォーターマークの高さ
var $mask_font_color = "#ffffff";// ウォーターマークのテキストの色
var $mask_font = 2; // ウォーターマークのフォント/ var $ font_size // サイズ
var $mask_position = 0; var $ Mask_img_pct = 50; // 画像の値が大きいほど結合プログラムは低くなります
VAR $mask_txt_pct = 50;// テキストの結合の度合い、値が小さいほど結合プロセスは低くなります
var $img_border_size = 0;//絵枠のサイズ
var $img_border_color;//絵の枠の色
var $_flip_x=0;// 水平反転回数
var $_flip_y=0;// 垂直反転回数

var $cut_type =0;// カットタイプ


var $img_type;// ファイルタイプ

// ファイルタイプの定義、出力画像の関数を指定します
var $ all_type = array (
"jpg" = & gt; array ("output" = & gt; "imagejpeg"),
"gif" = & gt; 配列 ("" 出力"=>"imagegif"),
"png" => 配列("出力"=>"imagepng"),
"wbmp" => 配列("出力"=>) ;"image2wbmp"),
"jpeg" => array("output"=>"imagejpeg"));

/**
* コンストラクター
*/
function ThumbHandler() {
$this-> mask_font_color = "#ffffff";使用 'を使用して' sを使用して使用します。
function setSrcImg($src_img, $img_type =null)
{
if(!file_exists($src_img))

die("画像が存在しません"); If(!empty($img_type))
$img_type; ️ / file_get_contents 関数には PHP バージョン 4.3.0 以上が必要です
_img);   
else
{
$handle = fopen ($src_img, "r");   
while (!feof ($handle))
{
$src .= fgets($fd, 4096);   
}
fclose ($handle);   
}
if(empty($src))
{
die("图片源空");   
}
$this->h_src = @ImageCreateFromString($src);   
$this->src_w = $this->getImgWidth($this->h_src);   
$this->src_h = $this->getImgHeight($this->h_src);   
}

/**
* 画像生成パスを設定
*
* @param string $dst_img 画像生成パス
*/
function setDstImg($dst_img)
{
$arr =explode('/',$dst_img);   
$last = array_pop($arr);   
$path = implode('/',$arr);   
$this->_mkdirs($path);   
$this->dst_img = $dst_img;   
}

/**
* 写真の表示品質を設定します
**/
function setImgDisplayQuality($n)
{
$this->img_display_quality = (int)$n;   
}

/**
* 画像生成の品質を設定します
**/
function setImgCreateQuality($n)
{
$this->img_create_quality = (int)$n;   
}

/**
透かしのフォントの色
*/
function setMaskWord($word)
{
$this->mask_word .= $word;   
}   

/**                                                                                                                                                                                           ​*/
function setMaskFontColor($color="#ffffff")
{
$this->mask_font_color = $color;   
}} /**&#&* /
関数setmaskfont($ font = 2)
{
if(!is_numeric($ font)&&!file_exists($ font))
{ ");   
}
$this->font = $font;   
}

/**文字 * 透かしフォントを設定します
*
* @param String $ FONT fonts |*/
function setMaskFontSize($size = "12")
{
$this->font_size = $size;   
}

/**
* テキストのフォント サイズを設定します。TrueType フォントにのみ有効です
*/
関数 setMaskImg($img)
{
$this->mask_img = $img;   
}

/**印 * 画像透かしを設定します 设 * @param String $ img 透かし画像ソース
*/
function setMaskOffsetX($x)
{
$this->mask_offset_x = (int)$x;   
}

/**
* 透かしの水平オフセットを設定します
*
*/
function setMaskOffsetY($y)
{
$this->mask_offset_y = (int)$y;   
}

/**
* ウォーターマークの垂直オフセットを設定します
*
* @param integer $y 垂直オフセット
*/
function setMaskPosition($position=0)
{
$this->mask_position = (int)$position;   
}   

/**
* 画像の結合度を設定します
*
*/
function setMaskImgPct($n)
{
$this->mask_img_pct = (int)$n;   
}

/**
* テキストの結合の度合いを設定します
*
*/
function setMaskTxtPct($n)
{
$this->mask_txt_pct = (int)$n;   
}

/**                                                                                                   */
function setDstImgBorder($size=1, $color="#000000")
{
$this->img_border_size = (int)$size;   
$this->img_border_color = $color;   
}

/**
*水平反転
*/
関数flipH()
{
$this->_flip_x++;   
}

/**
*垂直反転
*/
関数flipV()
{
$this->_flip_y++;   
}

/**                                                                                                      */
function setCutType($type)
{
$this->cut_type = (int)$type;   
}

/**
* 画像のクリッピングを設定します
*
* @param integer $width Rectangular clipping*/
function setRectangleCut($width, $height)
{
{
$this->fill_w = (int)$width;   
$this->fill_h = (int)$height;   
}

/**
* ソース画像のせん断開始座標点を設定
*
*/
function setSrcCutPosition($x, $y)
{
$this->src_x = (int)$x;   
$this->src_y = (int)$y;   
}

/**整数$b スケーリング後の画像の高さ
*/
function createImg($a, $b=null)
{
$num = func_num_args();
if(1 == $num)
{ $a; $this-> ;img_scale = $r;
$this->_setNewImgSize($r ); } }

if(2 == $num)

{ $w = (int)$a; $ b;   
else
{
falseを返します。   
}
}} /**&#&* /
関数_createmask()
{
if($ this-> mask_word)   

if($this->_isFull())
{
die("水印文字过大");   
}
else
{
$this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h );   
$white = ImageColorAllocate($this->h_dst,255,255,255);   
imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 塗りつぶされた背景色
$this->_drawBorder();   
imagecopyresampled( $this->h_dst, $this->h_src,
$this->start_x, $this->start_y,
$this->src_x、$this->src_y、
$this ->fill_w, $this->fill_h,
$this->copy_w, $this->copy_h);   
$this->_createMaskWord($this->h_dst);   
}
}

isfull())は、元の画像で生成され、_h = emageColorAllocate($> h_dst、255,255,255); ,$this->dst_w,$this->dst_h,$white);// 塗りつぶされた背景色 $this->start_x, $this ->start_y, , $this->src_y 、
this-> copy_w、$ this-&gt_h);   
else
{
// 创建新图并贝
$this->h_dst = imagecreatetruecolor($this- >dst_w, $this->dst_h);   
$white = ImageColorAllocate($this->h_dst,255,255,255);   
imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 塗りつぶされた背景色
$this->_drawBorder();   
imagecopyresampled( $this->h_dst, $this->h_src,
$this->start_x, $this->start_y,
$this->src_x、$this->src_y、
$this ->fill_w, $this->fill_h,
$this->copy_w, $this->copy_h);   
$this->_createMaskImg($this->h_dst);   
}
}

if(empty($this->mask_word) && empty($this->mask_img))
{
$this->h_dst = imagecreatetruecolor($this->dst_w, $this- >dst_h);   
$white = ImageColorAllocate($this->h_dst,255,255,255);   
imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 塗りつぶされた背景色
$this->_drawBorder();   

imagecopyresampled( $this->h_dst, $this->h_src,
$this->start_x, $this->start_y,
$this->src_x、$this->src_y、
$ this->fill_w, $this->fill_h,
$this->copy_w, $this->copy_h);   
}
}   

/**
*枠線を描きます
*/
function _drawBorder()
{
if(!empty($this->img_border_size))
{
$c = $this->_parseColor($this->img_border_color) );   
$color = ImageColorAllocate($this->h_src,$c[0], $c[1], $c[2]);   
imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$color);// 塗りつぶされた背景色
}
}

/**
* 透かしテキストを生成
* /
function _createMaskWord($src)
{
$this->_countMaskPos();   
$this->_checkMaskValid();   

$c = $this->_parseColor($this->mask_font_color);   
$color = imagecolorallocatealpha($src, $c[0], $c[1], $c[2], $this->mask_txt_pct);   

if(is_numeric($this->font))
{
imagestring($src,
$this->font,
$this->mask_pos_x、$this->mask_pos_y、
$this->gt ;mask_word,
$color);   
}
else
{
imagettftext($src,
$this->font_size, 0,
$this->mask_pos_x, $this->mask_pos_y,
$color,
$this- >フォント、
$this->mask_word);   
}
}   

/**
* 透かし画像を生成
*/
function _createMaskImg($src)
{
$this->_countMaskPos();   
$this->_checkMaskValid();   
imagecopymerge($src,
$this->h_mask,
$this->mask_pos_x ,$this->mask_pos_y,
0, 0,
$this->mask_w、$this->mask_h、
$this->mask_img_pct);   

imagedestroy($this->h_mask);   
}

/**
*透かし画像をロードします
*/
function _loadMaskImg()
{
$mask_type = $this->_getImgType($this->mask_img);   
$this->_checkValid($mask_type);   

// file_get_contents関数数要求phpバージョン本>4.3.0
$src = '';   
if(function_exists("file_get_contents"))
{
$src = file_get_contents($this->mask_img);   
}
else
{
$handle = fopen ($this->mask_img, "r");   
while (!feof ($handle))
{
$src .= fgets($fd, 4096);   
}
fclose ($handle);   
}
if(empty($this->mask_img))
{
die("水印图片は空");   
}
$this->h_mask = ImageCreateFromString($src);   
$this->mask_w = $this->getImgWidth($this->h_mask);   
$this->mask_h = $this->getImgHeight($this->h_mask);   
}   

/**
*画像出力
*/
function _output()
{
$img_type = $this->img_type;   
$func_name = $this->all_type[$img_type]['output'];   
if(function_exists($func_name))
{
// 判断浏览器、若是IE不送头
if(isset($_SERVER['HTTP_USER_AGENT'] ))
{
$ua = strtoupper($_SERVER[' HTTP_USER_AGENT']);   
if(!preg_match('/^.*MSIE.*)$/i',$ua))
{
header("Content-type:$img_type");   
}
}
$func_name($this->h_dst, $this->dst_img, $this->img_display_quality);   
}
else
{
falseを返します。   
}
}   

    /**  
     * 分析颜色  
     *  
     * @param    string     $color    十六进制颜色  
     */   
    function _parseColor($color)   
    {   
        $arr = array();   
        for($ii=1; $ii_isFull())   
        {   
            switch($this->mask_position)   
            {   
                case 1:   
                    // 左上   
                    $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;   
                    $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;   
                    break;   

                case 2:   
                    // 左下   
                    $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;   
                    $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;   
                    break;   

                case 3:   
                    // 右上   
                    $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;   
                    $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;   
                    break;   

                case 4:   
                    // 右下   
                    $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;   
                    $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;   
                    break;   

                default:   
                    // 默认将水印放到右下,偏移指定像素   
                    $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;   
$this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;   
休憩。   
}
}   
        else   
        {   
            switch($this->mask_position)   
            {   
                case 1:   
                    // 左上   
                    $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;   
                    $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;   
                    break;   

                case 2:   
                    // 左下   
                    $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;   
                    $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;   
                    break;   

                case 3:   
                    // 右上   
                    $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;   
                    $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;   
                    break;   

                case 4:   
                    // 右下   
                    $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;   
                    $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;   
                    break;   

                default:   
                    // 默认将水印放到右下,偏移指定像素   
                    $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;   
                    $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;   
                    壊す;   
}}}}}

/**&#&* /
関数_setFontInfo(){
{
if(is_numeric($ this-> font))
{
$ this-> font_w = imagefontwidth($ this $ this $ ->フォント);   
$this->font_h = imagefontheight($this->font);   

// 计算水音声体所占宽高
$word_length = strlen($this->mask_word);   
$this->mask_w = $this->font_w*$word_length;   
$this->mask_h = $this->font_h;   
}
else
{
$arr = imagettfbbox ($this->font_size,0, $this->font,$this->mask_word);   
$this->mask_w = abs($arr[0] - $arr[2]);   
$this->mask_h = abs($arr[7] - $arr[1]);   
}
}

/**
*フォント情報を設定します
*/
function _setNewImgSize($img_w, $img_h=null)
{
func_num_args( );   
if(1 == $num)
{
$this->img_scale = $img_w;// 宽度作値比
$this->fill_w =round($this->s rc_w * $this->img_scale / 100) - $this->img_border_size*2;   
$this->fill_h =round($this->src_h * $this->img_scale / 100) - $this->img_border_size*2;   

// ソース文件起始坐标
$this->src_x = 0;   
$this->src_y = 0;   
$this->copy_w = $this->src_w;   
$this->copy_h = $this->src_h;   

// 目标尺寸
$this->dst_w = $this->fill_w + $this->img_border_size*2;   
$this->dst_h = $this->fill_h + $this->img_border_size*2;   
}

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