<?php /** * 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); ?>
运行效果如下图所示:
<?php /** * 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的发光效果相同 /* 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); ?>
希望本文所述对大家的PHP程序设计有所帮助。