php實(shí)現(xiàn)圖片添加描邊字和馬賽克的方法
本文實(shí)例講述了php實(shí)現(xiàn)圖片添加描邊字和馬賽克的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
馬賽克:void imagemask ( resource image, int x1, int y1, int x2, int y2, int deep)
imagemask() 把坐標(biāo) x1,y1 到 x2,y2(圖像左上角為 0, 0)的矩形區(qū)域加上馬賽克。
deep為模糊程度,數(shù)字越大越模糊。
描邊:void imagetextouter ( resource image, int size, int x, int y, string color, string fontfile, string text, string outercolor)
imagetextouter() 將字符串 text 畫(huà)到 image 所代表的圖像上,從坐標(biāo) x,y(左上角為 0, 0)開(kāi)始,顏色為 color,邊框所使用的顏色為 outercolor,使用 fontfile 所指定的 truetype 字體文件。
如果不指定字體文件,則使用gd的內(nèi)部字體。根據(jù) php 所使用的 gd 庫(kù)的不同,如果 fontfile 沒(méi)有以 ‘/'開(kāi)頭,則 ‘.ttf' 將被加到文件名之后并且會(huì)搜索庫(kù)定義字體路徑。
如果指定了字體文件,由 x,y 所表示的坐標(biāo)定義了第一個(gè)字符的基本點(diǎn)(大概是字符的左下角)。否則 x,y 定義了第一個(gè)字符的右上角。
fontfile 是想要使用的 truetype 字體的文件名。
text 是文本字符串,可以包含 utf-8 字符序列(形式為:{)來(lái)訪問(wèn)字體中超過(guò)前 255 個(gè)的字符。
color 是十六進(jìn)制的#rrggbb格式的顏色,如#ff0000為紅色。
outercolor 描邊顏色,十六進(jìn)制的#rrggbb格式。
/**
* gd image mask
*
* @copyright ugia.cn
*/
function imagemask(&$im, $x1, $y1, $x2, $y2, $deep)
{
for($x = $x1; $x < $x2; $x += $deep)
{
for ($y = $y1; $y < $y2; $y += $deep)
{
$color = imagecolorat ($im, $x + round($deep / 2), $y + round($deep / 2));
imagefilledrectangle ($im, $x, $y, $x + $deep, $y + $deep, $color);
}
}
}
//馬賽克用法示例:
header("content-type: image/png");
$im = imagecreatefromjpeg("test.jpg");
imagemask($im, 57, 22, 103, 40, 8);
imagepng($im);
imagedestroy($im);
?>
運(yùn)行效果如下圖所示:

/**
* gd image text outer
*
* @copyright ugia.cn
*/
function imagetextouter(&$im, $size, $x, $y, $color, $fontfile, $text, $outer)
{
if (!function_exists('imagecolorallocatehex'))
{
function imagecolorallocatehex($im, $s)
{
if($s{0} == "#") $s = substr($s,1);
$bg_dec = hexdec($s);
return imagecolorallocate($im,
($bg_dec & 0xff0000) >> 16,
($bg_dec & 0x00ff00) >> 8,
($bg_dec & 0x0000ff)
);
}
}
$ttf = false;
if (is_file($fontfile))
{
$ttf = true;
$area = imagettfbbox($size, $angle, $fontfile, $text);
$width = $area[2] - $area[0] + 2;
$height = $area[1] - $area[5] + 2;
}
else
{
$width = strlen($text) * 10;
$height = 16;
}
$im_tmp = imagecreate($width, $height);
$white = imagecolorallocate($im_tmp, 255, 255, 255);
$black = imagecolorallocate($im_tmp, 0, 0, 0);
$color = imagecolorallocatehex($im, $color);
$outer = imagecolorallocatehex($im, $outer);
if ($ttf)
{
imagettftext($im_tmp, $size, 0, 0, $height - 2, $black, $fontfile, $text);
imagettftext($im, $size, 0, $x, $y, $color, $fontfile, $text);
$y = $y - $height + 2;
}
else
{
imagestring($im_tmp, $size, 0, 0, $text, $black);
imagestring($im, $size, $x, $y, $text, $color);
}
for ($i = 0; $i < $width; $i ++)
{
for ($j = 0; $j < $height; $j ++)
{
$c = imagecolorat($im_tmp, $i, $j);
if ($c !== $white)
{
imagecolorat ($im_tmp, $i, $j - 1) != $white || imagesetpixel($im, $x + $i, $y + $j - 1, $outer);
imagecolorat ($im_tmp, $i, $j + 1) != $white || imagesetpixel($im, $x + $i, $y + $j + 1, $outer);
imagecolorat ($im_tmp, $i - 1, $j) != $white || imagesetpixel($im, $x + $i - 1, $y + $j, $outer);
imagecolorat ($im_tmp, $i + 1, $j) != $white || imagesetpixel($im, $x + $i + 1, $y + $j, $outer);
// 取消注釋,與fireworks的發(fā)光效果相同
/*
imagecolorat ($im_tmp, $i - 1, $j - 1) != $white || imagesetpixel($im, $x + $i - 1, $y + $j - 1, $outer);
imagecolorat ($im_tmp, $i + 1, $j - 1) != $white || imagesetpixel($im, $x + $i + 1, $y + $j - 1, $outer);
imagecolorat ($im_tmp, $i - 1, $j + 1) != $white || imagesetpixel($im, $x + $i - 1, $y + $j + 1, $outer);
imagecolorat ($im_tmp, $i + 1, $j + 1) != $white || imagesetpixel($im, $x + $i + 1, $y + $j + 1, $outer);
*/
}
}
}
imagedestroy($im_tmp);
}
//用法示例:
header("content-type: image/png");
$im = imagecreatefromjpeg("bluesky.jpg");
$white = imagecolorallocate($im, 255,255,255);
imagetextouter($im, 9, 10, 20, '#000000', "simsun.ttc", '新年快樂(lè)', '#ffffff');
imagetextouter($im, 2, 10, 30, '#ffff00', "", 'hello, world!' , '#103993');
imagepng($im);
imagedestroy($im);
?>
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。
- JS實(shí)現(xiàn)馬賽克圖片效果完整示例
- C#對(duì)圖片進(jìn)行馬賽克處理可控制模糊程度的實(shí)現(xiàn)代碼
- java實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)和馬賽克化
- Java實(shí)現(xiàn)給圖片添加圖片水印,文字水印及馬賽克的方法示例
- php實(shí)現(xiàn)圖片局部打馬賽克的方法
- 自己用jQuery寫(xiě)了一個(gè)圖片的馬賽克消失效果
- 用javascript實(shí)現(xiàn)的圖片馬賽克后顯示并切換加文字功能
- 用javascript實(shí)現(xiàn)圖片馬賽克后顯示并切換
- three.js 將圖片馬賽克化的示例代碼
相關(guān)文章
php實(shí)現(xiàn)過(guò)濾UBB代碼的類
這篇文章主要介紹了php實(shí)現(xiàn)過(guò)濾UBB代碼的類,涉及php正則表達(dá)式的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
php編程實(shí)現(xiàn)追加內(nèi)容到txt文件中的方法
這篇文章主要介紹了php編程實(shí)現(xiàn)追加內(nèi)容到txt文件中的方法,涉及php覆蓋式添加內(nèi)容到txt文件、追加內(nèi)容以及逐行讀取txt文件的相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
PHP 文件寫(xiě)入和讀取操作實(shí)例詳解【必看篇】
這篇文章主要介紹了PHP 文件寫(xiě)入和讀取操作,結(jié)合實(shí)例形式詳細(xì)分析了PHP針對(duì)文件的打開(kāi)、讀取、寫(xiě)入及指針操作相關(guān)使用技巧,需要的朋友可以參考下2019-11-11
php中filter函數(shù)驗(yàn)證、過(guò)濾用戶輸入的數(shù)據(jù)
PHP 過(guò)濾器用于對(duì)來(lái)自非安全來(lái)源的數(shù)據(jù)(比如用戶輸入)進(jìn)行驗(yàn)證和過(guò)濾,下面為大家整理了一些,需要的朋友可以了解下2014-01-01
PHP實(shí)現(xiàn)限制IP訪問(wèn)及提交次數(shù)的方法詳解
這篇文章主要介紹了PHP實(shí)現(xiàn)限制IP訪問(wèn)及提交次數(shù)的方法,涉及php針對(duì)客戶端來(lái)訪IP的獲取、判斷以及結(jié)合session記錄IP訪問(wèn)次數(shù)等相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Win2003下APACHE+PHP5+MYSQL4+PHPMYADMIN 的簡(jiǎn)易安裝配置
Win2003下APACHE+PHP5+MYSQL4+PHPMYADMIN 的簡(jiǎn)易安裝配置...2006-11-11

