有没有办法用PHP在PHP中使用文本阴影制作图像?

时间:2016-01-26 11:15:04

标签: php image gd imagick drawimage

我已经制作了一个工具,用户可以在其中输入文字&脚本会生成该文本的图像,但是遇到文本阴影的问题,我想用文本阴影制作图像但是GB库self.beaconRegion.peripheralDataWithMeasuredPower(nil) as! [String: AnyObject] 功能只将图像的阴影放在文本上,

这是我现在的图像

enter image description here

但我想要那样

enter image description here

有没有人知道任何方式所以我可以制作这样的图像?

这是我用来创建它的方法,

shadowImage

3 个答案:

答案 0 :(得分:2)

只需使用Imagick :: annotateImage绘制带阴影颜色和阴影偏移的文本,然后在所需位置再次绘制文本。修改后的代码从php Imageick manual

复制而来
<?php
    /* Create some objects */
    $image = new Imagick();
    $draw = new ImagickDraw();
    $pixel = new ImagickPixel( 'white' );

    /* New image */
    $image->newImage(800, 75, $pixel);


    /* Font properties */
    $draw->setFont('Bookman-DemiItalic');
    $draw->setFontSize( 30 );

    $offset_x = 3;
    $offset_y = 3;

    /* Black shadow */
    $draw->setFillColor('black');
    /* Create text */
    $image->annotateImage($draw, 10+$offset_x, 45+$offset_y, 0, 'The quick brown fox jumps over the lazy dog');

    /* Yellow Test */
    $draw->setFillColor('yellow');
    /* Create text */
    $image->annotateImage($draw, 10, 45, 0, 'The quick brown fox jumps over the lazy dog');

    /* Give image a format */
    $image->setImageFormat('png');

    /* Output the image with headers */
    header('Content-type: image/png');
    echo $image;

?>

答案 1 :(得分:2)

您可以从[GitHub Repository]获取参考资料。代码转载如下     

/*
 * imagettftextblur v1.0.0
 *
 * Copyright (c) 2013 Andrew G. Johnson  <andrew@andrewgjohnson.com>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * @author Andrew G. Johnson <andrew@andrewgjohnson.com>
 * @copyright Copyright (c) 2013 Andrew G. Johnson <andrew@andrewgjohnson.com>
 * @link http://github.com/andrewgjohnson/imagettftextblur
 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
 * @version 1.0.0
 * @package imagettftextblur
 *
 */

if (!function_exists('imagettftextblur'))
{
    function imagettftextblur(&$image,$size,$angle,$x,$y,$color,$fontfile,$text,$blur_intensity = null)
    {
        $blur_intensity = !is_null($blur_intensity) && is_numeric($blur_intensity) ? (int)$blur_intensity : 0;
        if ($blur_intensity > 0)
        {
            $text_shadow_image = imagecreatetruecolor(imagesx($image),imagesy($image));
            imagefill($text_shadow_image,0,0,imagecolorallocate($text_shadow_image,0x00,0x00,0x00));
            imagettftext($text_shadow_image,$size,$angle,$x,$y,imagecolorallocate($text_shadow_image,0xFF,0xFF,0xFF),$fontfile,$text);
            for ($blur = 1;$blur <= $blur_intensity;$blur++)
                imagefilter($text_shadow_image,IMG_FILTER_GAUSSIAN_BLUR);
            for ($x_offset = 0;$x_offset < imagesx($text_shadow_image);$x_offset++)
            {
                for ($y_offset = 0;$y_offset < imagesy($text_shadow_image);$y_offset++)
                {
                    $visibility = (imagecolorat($text_shadow_image,$x_offset,$y_offset) & 0xFF) / 255;
                    if ($visibility > 0)
                        imagesetpixel($image,$x_offset,$y_offset,imagecolorallocatealpha($image,($color >> 16) & 0xFF,($color >> 8) & 0xFF,$color & 0xFF,(1 - $visibility) * 127));
                }
            }
            imagedestroy($text_shadow_image);
        }
        else
            return imagettftext($image,$size,$angle,$x,$y,$color,$fontfile,$text);
    }
}

以下是添加阴影的方法:

imagettftextblur($image,$size,0,$x + 3,$y + 3,$shadow_color,$font,$string,1); // 1 can be higher to increase blurriness of the shadow
imagettftextblur($image,$size,0,$x,$y,$text_color,$font,$string);

答案 2 :(得分:2)

两次绘制文字。首先在文本位置的右侧和下方略微绘制阴影文本(没有轮廓),然后按照正常情况绘制文本。