圖片上添加描邊字和馬賽克php代碼
Jun 13, 2016 am 11:24 AM
/**
?* 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的發(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", '新年快樂', '#ffffff');
imagetextouter($im, 2, 10, 30, '#ffff00', "", 'hello, world!' , '#103993');
imagepng($im);
imagedestroy($im);
?>
馬賽克:void imagemask ( resource image, int x1, int y1, int x2, int y2, int deep)
imagemask() 把坐標 x1,y1 到 x2,y2(圖像左上角為 0, 0)的矩形區(qū)域加上馬賽克。
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格式。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

In PHP, to pass a session variable to another page, the key is to start the session correctly and use the same $_SESSION key name. 1. Before using session variables for each page, it must be called session_start() and placed in the front of the script; 2. Set session variables such as $_SESSION['username']='JohnDoe' on the first page; 3. After calling session_start() on another page, access the variables through the same key name; 4. Make sure that session_start() is called on each page, avoid outputting content in advance, and check that the session storage path on the server is writable; 5. Use ses

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach
