[PHP GD庫]①②--文字水印封裝

/**文字水印
 * @param $filename
 * @param string $fontfile
 * @param string $text
 * @param string $dest
 * @param string $pre
 * @param bool $delSource
 * @param int $r
 * @param int $g
 * @param int $b
 * @param int $alpha
 * @param int $angle
 * @param int $size
 * @param int $x
 * @param int $y
 * @return string
 */
function water_text($filename, $fontfile = 'fonts/couri.ttf', $text = 'Imooc', $dest = 'waterText', $pre = 'waterText', $delSource = false, $r = 255,
                    $g = 0, $b = 0, $alpha = 60, $angle = 0, $size = 30, $x = 0, $y = 30)
{
    $fileInfo = getImageInfo($filename);
    $image = $fileInfo['createFun']($filename);
    $color = imagecolorallocatealpha($image, $r, $g, $b, $alpha);
    imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);
    if ($dest && !file_exists($dest)) {
        mkdir($dest, 0777, true);
    }
    $randNum = mt_rand(100000, 999999);
    $dstName = "{$pre}{$randNum}" . $fileInfo['ext'];
    $destination = $dest ? $dest . '/' . $dstName : $dstName;
    $fileInfo['outFun']($image, $destination);
    imagedestroy($image);
    if ($delSource) {
        @unlink($filename);
    }
    return $destination;
}

test.php

<?php
require_once 'image.func.php';
$filename = 'images/1.jpg';
water_text($filename);
?>

image.func.php

<?php

/** 返回圖片信息
 * @param $filename
 */
function getImageInfo($filename)
{
    if (@!$info = getimagesize($filename)) {
        exit("文件不是真實圖片");
    }
    /**
     * array
     * 0 => int 756
     * 1 => int 960
     * 2 => int 2
     * 3 => string 'width="756" height="960"' (length=24)
     * 'bits' => int 8
     * 'channels' => int 3
     * 'mime' => string 'image/jpeg' (length=10)
     */
    $fileInfo['width'] = $info[0];//756
    $fileInfo['height'] = $info[1];//960
    $mime = image_type_to_mime_type($info[2]);//image/jpeg
    $createFun = str_replace('/', 'createfrom', $mime);
    $outFun = str_replace('/', '', $mime);
    $fileInfo['createFun'] = $createFun;
    $fileInfo['outFun'] = $outFun;
    $fileInfo['ext'] = strtolower(image_type_to_extension($info[2]));
    return $fileInfo;
}

/**
 * 形成縮略圖
 * @param $filename 文件名
 * @param string $dest 縮略圖保存路徑,默認(rèn)'thumb'
 * @param string $pre 默認(rèn)前綴thumb_
 * @param null $dst_w 最大寬度
 * @param null $dst_h 最大高度
 * @param float $scale 默認(rèn)縮放比例
 * @param boolean $delSource 是否刪除源文件標(biāo)志
 * @return string 最終保存路徑及文件名
 *
 */
function thumb($filename, $dest = 'thumb', $pre = 'thumb_',
               $dst_w = null, $dst_h = null, $scale = 0.5, $delSource = false)
{
    $fileInfo = getImageInfo($filename);
    $src_w = $fileInfo['width'];
    $src_h = $fileInfo['height'];
//如果指定最大寬度和高度飞崖,按照等比例縮放進(jìn)行處理
    if (is_numeric($dst_w) && is_numeric($dst_h)) {
        $ratio_orig = $src_w / $src_h;
        if ($dst_w / $dst_h > $ratio_orig) {
            $dst_w = $dst_h * $ratio_orig;
        } else {
            $dst_h = $dst_w / $ratio_orig;
        }
    } else {
        $dst_w = ceil($src_w * $scale);
        $dst_h = ceil($src_h * $scale);
    }
    $dst_image = imagecreatetruecolor($dst_w, $dst_h);
    $src_image = $fileInfo['createFun']($filename);
    imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    if ($dest && !file_exists($dest)) {
        mkdir($dest, 07777, true);
    }
    $randNum = mt_rand(100000, 999999);
    $dstName = "{$pre}{$randNum}" . $fileInfo['ext'];
    $destination = $dest ? $dest . '/' . $dstName : $dstName;
    $fileInfo['outFun']($dst_image, $destination);
    imagedestroy($src_image);
    imagedestroy($dst_image);
    if ($delSource) {
        @unlink($filename);
    }
    return $destination;
}

/**文字水印
 * @param $filename
 * @param string $fontfile
 * @param string $text
 * @param string $dest
 * @param string $pre
 * @param bool $delSource
 * @param int $r
 * @param int $g
 * @param int $b
 * @param int $alpha
 * @param int $angle
 * @param int $size
 * @param int $x
 * @param int $y
 * @return string
 */
function water_text($filename, $fontfile = 'fonts/couri.ttf', $text = 'Imooc', $dest = 'waterText', $pre = 'waterText', $delSource = false, $r = 255,
                    $g = 0, $b = 0, $alpha = 60, $angle = 0, $size = 30, $x = 0, $y = 30)
{
    $fileInfo = getImageInfo($filename);
    $image = $fileInfo['createFun']($filename);
    $color = imagecolorallocatealpha($image, $r, $g, $b, $alpha);
    imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);
    if ($dest && !file_exists($dest)) {
        mkdir($dest, 0777, true);
    }
    $randNum = mt_rand(100000, 999999);
    $dstName = "{$pre}{$randNum}" . $fileInfo['ext'];
    $destination = $dest ? $dest . '/' . $dstName : $dstName;
    $fileInfo['outFun']($image, $destination);
    imagedestroy($image);
    if ($delSource) {
        @unlink($filename);
    }
    return $destination;
}

?>
Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末孤个,一起剝皮案震驚了整個濱河市栏饮,隨后出現(xiàn)的幾起案子志电,更是在濱河造成了極大的恐慌摔寨,老刑警劉巖闲询,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件袍祖,死亡現(xiàn)場離奇詭異底瓣,居然都是意外死亡,警方通過查閱死者的電腦和手機蕉陋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進(jìn)店門捐凭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人凳鬓,你說我怎么就攤上這事茁肠。” “怎么了缩举?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵垦梆,是天一觀的道長。 經(jīng)常有香客問我仅孩,道長托猩,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任杠氢,我火速辦了婚禮站刑,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鼻百。我一直安慰自己绞旅,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布温艇。 她就那樣靜靜地躺著因悲,像睡著了一般。 火紅的嫁衣襯著肌膚如雪勺爱。 梳的紋絲不亂的頭發(fā)上晃琳,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天,我揣著相機與錄音琐鲁,去河邊找鬼卫旱。 笑死,一個胖子當(dāng)著我的面吹牛围段,可吹牛的內(nèi)容都是我干的顾翼。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼奈泪,長吁一口氣:“原來是場噩夢啊……” “哼适贸!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起涝桅,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤拜姿,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后冯遂,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蕊肥,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年蛤肌,在試婚紗的時候發(fā)現(xiàn)自己被綠了壁却。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡寻定,死狀恐怖儒洛,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情狼速,我是刑警寧澤琅锻,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站向胡,受9級特大地震影響恼蓬,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜僵芹,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一处硬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧拇派,春花似錦荷辕、人聲如沸凿跳。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽控嗜。三九已至,卻和暖如春骡显,著一層夾襖步出監(jiān)牢的瞬間疆栏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工惫谤, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留壁顶,地道東北人。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓溜歪,卻偏偏與公主長得像若专,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子痹愚,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容