php实现图片添加描边字和马赛克的方法
这篇文章主要介绍了php实现图片添加描边字和马赛克的方法,通过自定义函数imagemask与imagetextouter实现图片添加马赛克与文字描边的效果,非常具有实用价值,需要
本文实例讲述了php实现图片添加描边字和马赛克的方法。分享给大家供大家参考。具体实现方法如下:
马赛克:void imagemask ( resource image, int x1, int y1, int x2, int y2, int deep)
imagemask() 把坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)的矩形区域加上马赛克。
deep为模糊程度,数字越大越模糊。
描边:void imagetextouter ( resource image, int size, int x, int y, string color, string fontfile, string text, string outercolor)
imagetextouter() 将字符串 text 画到 image 所代表的图像上,从坐标 x,y(左上角为 0, 0)开始,颜色为 color,边框所使用的颜色为 outercolor,使用 fontfile 所指定的 truetype 字体文件。
如果不指定字体文件,则使用gd的内部字体。根据 php 所使用的 gd 库的不同,如果 fontfile 没有以 ‘/'开头,则 ‘.ttf' 将被加到文件名之后并且会搜索库定义字体路径。
如果指定了字体文件,由 x,y 所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。否则 x,,y 定义了第一个字符的右上角。
fontfile 是想要使用的 truetype 字体的文件名。
text 是文本字符串,可以包含 utf-8 字符序列(形式为:{)来访问字体中超过前 255 个的字符。
color 是十六进制的#rrggbb格式的颜色,如#ff0000为红色。
outercolor 描边颜色,十六进制的#rrggbb格式。
复制代码 代码如下:
/**
* gd image mask
*
* @copyright ugia.cn
*/
function imagemask(&$im, $x1, $y1, $x2, $y2, $deep)
{
for($x = $x1; $x
{
for ($y = $y1; $y
{
$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);
?>
运行效果如下图所示:
复制代码 代码如下:
/**
* 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
{
for ($j = 0; $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的发光效果相同
/*
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);
}

ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

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

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

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

ホットトピック











PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

ファイルのアップロードを行うには、フォーム ヘルパーを使用します。ここではファイルアップロードの例を示します。

CakePHP は、PHP 用のオープンソース フレームワークです。これは、アプリケーションの開発、展開、保守をより簡単にすることを目的としています。 CakePHP は、強力かつ理解しやすい MVC のようなアーキテクチャに基づいています。モデル、ビュー、コントローラー

Visual Studio Code (VS Code とも呼ばれる) は、すべての主要なオペレーティング システムで利用できる無料のソース コード エディター (統合開発環境 (IDE)) です。 多くのプログラミング言語の拡張機能の大規模なコレクションを備えた VS Code は、
