如何使用PHP在图像文本内容上添加背景色?
使用下面的PHP代码,我可以向图像添加文本,但现在我想向图像文本添加背景色。你能告诉我怎么做吗 注意:文本背景需要位于图像下方 现在是什么:如何使用PHP在图像文本内容上添加背景色?,php,Php,使用下面的PHP代码,我可以向图像添加文本,但现在我想向图像文本添加背景色。你能告诉我怎么做吗 注意:文本背景需要位于图像下方 现在是什么: //Set the Content Type header('Content-type: image/jpg'); // Create Image From Existing File $jpg_image = imagecreatefromjpeg('test-02.jpg'); // Allocate A Color For Th
//Set the Content Type
header('Content-type: image/jpg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('test-02.jpg');
// Allocate A Color For The Text
$color = imagecolorallocate($jpg_image, 0, 0, 0);
// Set Path to Font File
$font_path = 'arial.ttf';
// Set Text to Be Printed On Image
$text = "My Content Goes to here";
// Print Text On Image
//$image = imagecreatefromjpg($jpg_image);
$orig_width = imagesx($jpg_image);
$orig_height = imagesy($jpg_image);
imagettftext($jpg_image, 20, 0, 10, $orig_height-10, $color, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
我想要什么:
//Set the Content Type
header('Content-type: image/jpg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('test-02.jpg');
// Allocate A Color For The Text
$color = imagecolorallocate($jpg_image, 0, 0, 0);
// Set Path to Font File
$font_path = 'arial.ttf';
// Set Text to Be Printed On Image
$text = "My Content Goes to here";
// Print Text On Image
//$image = imagecreatefromjpg($jpg_image);
$orig_width = imagesx($jpg_image);
$orig_height = imagesy($jpg_image);
imagettftext($jpg_image, 20, 0, 10, $orig_height-10, $color, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
PHP代码:
//Set the Content Type
header('Content-type: image/jpg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('test-02.jpg');
// Allocate A Color For The Text
$color = imagecolorallocate($jpg_image, 0, 0, 0);
// Set Path to Font File
$font_path = 'arial.ttf';
// Set Text to Be Printed On Image
$text = "My Content Goes to here";
// Print Text On Image
//$image = imagecreatefromjpg($jpg_image);
$orig_width = imagesx($jpg_image);
$orig_height = imagesy($jpg_image);
imagettftext($jpg_image, 20, 0, 10, $orig_height-10, $color, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
如果您不将其视为背景色,而是在编写文本之前绘制一个矩形,那么问题就变得很容易:
$bcolor=imagecolorallocate($jpg_image, 0x00, 0xC0, 0xFF);
imagerectangle($jpg_image, 0, $orig_height*0.8, 0, $orig_height, $bcolor);
在imagettftext
之前
编辑
它当然是imagefilledrectangle
,而不是imagerectangle
。这是完整的脚本
<?php
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('test-02.jpg');
$orig_width = imagesx($jpg_image);
$orig_height = imagesy($jpg_image);
// Allocate A Color For The background
$bcolor=imagecolorallocate($jpg_image, 0x00, 0xC0, 0xFF);
//Create background
imagefilledrectangle($jpg_image, 0, $orig_height*0.8, $orig_width, $orig_height, $bcolor);
// Set Path to Font File
$font_path = realpath(dirname(__FILE__)).'/arial.ttf';
// Set Text to Be Printed On Image
$text = "New Content Goes to here";
// Allocate A Color For The Text
$color = imagecolorallocate($jpg_image, 0, 0, 0);
// Print Text On Image
imagettftext($jpg_image, 20, 0, 10, $orig_height-10, $color, $font_path, $text);
//Set the Content Type
header('Content-type: image/jpg');
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
这不是背景颜色。这只是一个带有bg颜色的矩形区域。找到一个函数来编写一个矩形区域并使用它。@u_mulder感谢您的更正。我在imagettftest
之前添加了这两行,但输出与我的第一个图像相同。日志文件中有什么内容吗?