1

我正在给定模板上创建一个文本图像,其中所有参数都是动态的,它工作正常!并创建图像,我的 php 脚本是,

<?php
// To fetch template info from database
$template_query = mysql_query("SELECT * FROM templates WHERE templateID = '".$fetch['templateID']."'");
$temp_data = mysql_fetch_assoc($template_query);
//create and save images
$temp = '../'. $temp_data['blank_templates'];
//check image type
$image_extension = strtolower(substr(strrchr($temp_data['blank_templates'], "."), 1));

    $im = imagecreatefromjpeg($temp);

$black = hexdec($temp_data['font_color']);
// Replacing path by your own font path
$font = '..'.$temp_data['font_file_upload'];

// Break it up into pieces 125 characters long
$no_of_characters_line = $temp_data['no_of_characters_line'];
$lines = explode('|', wordwrap($message, $no_of_characters_line, '|'));
// Starting Y position and X position
$y = $temp_data['position_from_top'];
$x = $temp_data['position_from_left'];
$font_size = $temp_data['font_size'];
$rotation_angle = $temp_data['rotation'];
$line_height = $temp_data['line_height'];

foreach ($lines as $line)
{
    imagettftext($im, $font_size,$rotation_angle, $x, $y, $black, $font, $line);
    // Increment Y so the next line is below the previous line
    $y += $line_height;
}
$id = uniqid();
$save = '../messagesimage/'.$id. '.'.$image_extension;
$path_save = substr($save, 3);
// Using imagepng() results in clearer text compared with imagejpeg()
        imagejpeg($im,$save);

imagedestroy($im);

这正在创造像..的形象!在此处输入图像描述

现在我想添加动态更改字体不透明度和阴影的功能,可以吗?如果是,那么请帮我这样做..

提前致谢

4

2 回答 2

3

哇,你已经等了一段时间了。

为了使您的文本具有一定的透明度,您需要使用 Alpha 通道定义文本颜色。

$black = imageallocatecoloralpha(0,0,0,16);

给你的文字一些阴影

$shadow = imageallocatecoloralpha(0,0,0,64); // more transparent
imagettftext($im, $font_size,$rotation_angle, $x+1, $y+1, $shadow, $font, $line);
imagettftext($im, $font_size,$rotation_angle, $x, $y, $black, $font, $line);
于 2014-01-17T06:04:16.943 回答
3

甚至更晚,但似乎当前的答案是不正确的。

要定义具有透明度的颜色,请使用以下函数imagecolorallocatealpha()

$black = imagecolorallocatealpha($image, 0, 0, 0, 50);

就像迈克尔指出的那样,阴影非常简单(只需使用正确的函数名称,并确保添加图像对象)。但是,如果您想要一个看起来更阴暗的阴影,请将其模糊一下:

$shadow = imagecolorallocatealpha($im, 0, 0, 0, 50);
//Draw shadow text
imagettftext($im, $font_size, 0, $x, $y, $shadow, $fontn, $text);
//Blur
imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
//Draw text
imagettftext($im, $font_size, 0, $x, $y, $white, $fontn, $text);

将上述阴影方法应用于图像后,其呈现类似于以下内容:

示例图像

于 2016-06-18T17:22:40.523 回答