【问题标题】:How to create gif image with different text formats in php with background image?如何在带有背景图像的php中创建具有不同文本格式的gif图像?
【发布时间】:2012-12-03 17:02:36
【问题描述】:

这是否可以在 php 中创建具有各种文本字体、样式、格式和对齐方式的背景图像的 gif 图像?

例如,我有一个背景图片,例如

我想在背景图片上插入的图片是

输出的 gif 图像应该是这样的

我试过这个代码

<?php
// Create a new image instance
$im = imagecreatetruecolor(300, 250);
$img = imagecreatefrompng('back.png');
// Make the background white
imagefilledrectangle($im, 0, 0, 300, 250, 0xEFEFEF);

// Draw a text string on the image

imagestring($im, 20,20, 40, 'Heading1', 0x000000);
imagecopymerge($img, $im, 0, 0, 0, 0, 300, 250, 70);
// Output the image to browser
header('Content-Type: image/gif');

imagegif($img,'test.gif');
imagedestroy($im);
?>

但不幸的是,它没有提供所需的输出,我想向背景图像添加更多内容,如输出图像中所示。

【问题讨论】:

    标签: php


    【解决方案1】:

    对于这个设计:

    您可以使用此代码:

    <?php
    // Create a new image instance
    $img = imagecreatefrompng('back.png');
    
    // Draw a text string on the image
    //  imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color )
    imagestring($img, 5, 20, 30, 'Heading1', 0x000000);
    imagestring($img, 3, 180, 30, 'Heading 2 test desc', 0x000000);
    
    imagestring($img, 3, 20, 80, 'Text info 3', 0xFF0000);   //red
    imagestring($img, 1, 180, 80, 'Demonstration text', 0x000000);
    
    imagestring($img, 5, 400, 170, 'Price here', 0xFF0000);   //red
    
    $im = imagecreatefrompng('plane.png'); //load pic 2
    imagecopymerge($img, $im, 0, 130, 0, 0, 165, 71, 70);
    imagedestroy($im);
    
    // Output the image to browser
    header('Content-Type: image/gif');
    imagegif($img);
    imagedestroy($img);
    ?>
    

    imagestring() 函数仅限于内置的、相当难看的字体,并且大小选项只有 1 到 5。您可以使用另一个函数来代替 TTF 字体,如下所述。

    要更改字体,您需要提供自己的 TTF 格式字体,请将它们与脚本放在同一文件夹中,并将 imagestring() 函数替换为该函数。参数非常相似,您自己替换该代码应该很简单。这是解释如何使用imagettftext()-function 的链接。

    http://php.net/manual/en/function.imagettftext.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-15
      • 2019-07-03
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多