php - 如何在使用 php 创建文本图像时添加背景图像?

标签 php image-processing

我目前正在使用以下脚本--

<?php
// Set the content-type
header('Content-type: image/png');

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 115, 150, 195);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'My Name';
// Replace path by your own font path
$font = 'AGENCYB.TTF';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

但我也想添加背景图片。请帮忙,我是这个功能的新手。

最佳答案

像下面这样的东西对你有用吗?您想打开要用作背景的图像,然后在顶部写上您的文字。

    <?php
    // Set the content-type
    header('Content-type: image/png');

    /* Attempt to open */
        $im = @imagecreatefrompng('backgroundimage.png');

        /* See if it failed */
        if(!$im)
        {
            // Create some colors
        $white = imagecolorallocate($im, 255, 255, 255);
        $grey = imagecolorallocate($im, 128, 128, 128);
        $black = imagecolorallocate($im, 115, 150, 195);
        imagefilledrectangle($im, 0, 0, 399, 29, $white);

        // The text to draw
        $text = 'My Name';
        // Replace path by your own font path
        $font = 'AGENCYB.TTF';

        // Add some shadow to the text
        imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

        // Add the text
        imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

        // Using imagepng() results in clearer text compared with imagejpeg()
        imagepng($im);
        imagedestroy($im);


        }else
    {
         //you want to do something here if your image didn't open like maybe fpassthru an alternative image
    }
?>

关于php - 如何在使用 php 创建文本图像时添加背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5355622/

相关文章:

php - 并非所有字段都保存在我的 MySQL 数据库中

php - JavaScript 中的 JSON 评估

php - 查询生成器 laravel

php - 为 WooCommerce 中的特定结账字段启用更新结账

linux - 使用 Linux 进行 pdf 嵌入的高效图像压缩

java - 线路检测 |用Java进行角度检测

image - 为什么图像不能完全从逻辑图像转换回来

php - @package 是否需要 PHP 5.3 命名空间?

image - 如何裁剪图像中的上、左、下、右边界?

python - 如何使用开放式简历在脑肿瘤上绘制矩形?