如何将多个图像和文本合并为单个图像?

2023-12-10

我在 div 中有多个 PNG 图像,这些图像是 PNG 并根据用户选择的自定义选项作为单个图像呈现给用户。此外,添加文本也可以作为其他功能启用,它允许带有文本的 div 添加到这些图像的上方。

现在我想生成一个将多个图像和文本组合在一起的图像,并保持文本的字体系列和大小。

For eg. the image that appears on interface with combination of images and text. (It's managed to appear with css positioning) enter image description here Where as this is made of two images below and text

![enter image description here enter image description here

这就是我尝试拍摄图像的方法:create-image.php 文件

<?php
createimageinstantly();
function createimageinstantly($img1='',$img2='',$img3=''){
    $x=$y=1000;
    header('Content-Type: image/png');
    $targetFolder = '/gw/media/uploads/processed/';
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;

    $img1 = $targetPath.'img1.png';
    $img2 = $targetPath.'img2.png';
    $img3 = $targetPath.'img3.png';

    $outputImage = imagecreatetruecolor(1000, 1000);

    $first = imagecreatefrompng($img1);
    $second = imagecreatefrompng($img2);
    $third = imagecreatefrompng($img3);

    imagecopy($outputImage,$first,0,0,0,0, $x, $y);
    imagecopy($outputImage,$second,0,0,0,0, $x, $y);
    imagecopy($outputImage,$third,0,200,-200,0, $x, $y);

    imagepng($outputImage, $targetPath .round(microtime(true) * 1000).'.png');

    imagedestroy($outputImage);
 }
?>

But this gives me whole black colored image ![enter image description here

另外,我需要与最终生成的图像上的文本混合

edited :

  • jpg 图片更改为 png

  • imagecopymege变成imagecopy

最新结果:

       <?php
            createimageinstantly();
            //$targetFolder = '/gw/media/uploads/processed/';
            //$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
            //$img3 = $targetPath.'img3.png';
            //print_r(getimagesize('http://www.vapor-rage.com/wp-content/uploads/2014/05/sample.jpg'));
            function createimageinstantly($img1='',$img2='',$img3=''){
                $x=$y=600;
                header('Content-Type: image/png');
                $targetFolder = '/gw/media/uploads/processed/';
                $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;

                $img1 = $targetPath.'img1.png';
                $img2 = $targetPath.'img2.png';
                $img3 = $targetPath.'img3.png';

                $outputImage = imagecreatetruecolor(600, 600);

                // set background to white
                $white = imagecolorallocate($outputImage, 255, 255, 255);
                imagefill($outputImage, 0, 0, $white);

                $first = imagecreatefrompng($img1);
                $second = imagecreatefrompng($img2);
                $third = imagecreatefrompng($img3);

                //imagecopyresized ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
                imagecopyresized($outputImage,$first,0,0,0,0, $x, $y,$x,$y);
                imagecopyresized($outputImage,$second,0,0,0,0, $x, $y,$x,$y);
                imagecopyresized($outputImage,$third,200,200,0,0, 100, 100, 204, 148);

                imagepng($outputImage, $targetPath .round(microtime(true)).'.png');

                imagedestroy($outputImage);
            }
        ?>

And the output image enter image description here


我是这样实现的,

使用了这 3 个图像,img1.png,img2.png,img3.png

enter image description hereenter image description hereenter image description here

And create-image.php file

 <?php
        createimageinstantly();
        //$targetFolder = '/gw/media/uploads/processed/';
        //$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
        //$img3 = $targetPath.'img3.png';
        //print_r(getimagesize('http://www.vapor-rage.com/wp-content/uploads/2014/05/sample.jpg'));
        function createimageinstantly($img1='',$img2='',$img3=''){
            $x=$y=600;
            header('Content-Type: image/png');
            $targetFolder = '/gw/media/uploads/processed/';
            $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;

            $img1 = $targetPath.'img1.png';
            $img2 = $targetPath.'img2.png';
            $img3 = $targetPath.'img3.png';

            $outputImage = imagecreatetruecolor(600, 600);

            // set background to white
            $white = imagecolorallocate($outputImage, 255, 255, 255);
            imagefill($outputImage, 0, 0, $white);

            $first = imagecreatefrompng($img1);
            $second = imagecreatefrompng($img2);
            $third = imagecreatefrompng($img3);

            //imagecopyresized ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
            imagecopyresized($outputImage,$first,0,0,0,0, $x, $y,$x,$y);
            imagecopyresized($outputImage,$second,0,0,0,0, $x, $y,$x,$y);
            imagecopyresized($outputImage,$third,200,200,0,0, 100, 100, 204, 148);

            // Add the text
            //imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
            //$white = imagecolorallocate($im, 255, 255, 255);
            $text = 'School Name Here';
            $font = 'OldeEnglish.ttf';
            imagettftext($outputImage, 32, 0, 150, 150, $white, $font, $text);

            $filename =$targetPath .round(microtime(true)).'.png';
            imagepng($outputImage, $filename);

            imagedestroy($outputImage);
        }
    ?>

And the result image is enter image description here

参考 :图像复制调整大小 & 图像TTF文本

感谢您通过评论/答案提出的建议。 我也在博客上详细介绍了这一点http://sumankc.com/2016/01/30/merge-multiple-images-and-text-to-create-single-image-php-gd-library/再会 !!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将多个图像和文本合并为单个图像? 的相关文章

  • 使用 DOJO 自动完成文本框

    我正在寻找一种使用 DOJO 进行文本框自动建议的简单方法 我将查询的数据库表 使用 PHP 脚本 以 JSON 形式返回 有超过 100 000 条记录 因此这确实不应该采用 FilteringSelect 或 ComboBox 的形式
  • 如何在没有引用的情况下复制对象?

    PHP5 OOP 有据可查对象通过引用传递 http php net manual en language oop5 references php默认情况下 如果这是默认的 在我看来 有一种非默认的方式可以在没有参考的情况下进行复制 如何
  • 将数组拆分为特定数量的块

    我知道array chunk 允许将数组拆分为多个块 但块的数量根据元素的数量而变化 我需要的是始终将数组拆分为特定数量的数组 例如 4 个数组 以下代码将数组分为 3 个块 两个块各有 2 个元素 1 个块有 1 个元素 我想要的是将数组
  • 将 MySQL 结果作为 PHP 数组

    mysql 表 config name config value allow autologin 1 allow md5 0 当前的 php 代码 sth mysql query SELECT rows array while r mysq
  • 如何在html中制作多行类型的文本框?

  • 如何从字符串中删除所有数字?

    我想删除字符串 0 9 中的所有数字 我写了这段有效的代码 words preg replace 0 words remove numbers words preg replace 1 words remove numbers words
  • PHP cURL 在本地工作,在 AWS 服务器上出现错误 77

    最新更新 脚本作为管理员用户通过 SSH shell 作为 php script php 成功运行 当由 nginx 用户运行时 curl 命令无法执行 https 请求 所以我猜测这是nginx用户无法正确使用curl的问题 我已经检查了
  • 文件修改时间检查的成本

    对于Linux下包含少量字节的文件 我只需要处理自上次处理以来发生更改的时间 我通过调用 PHP 检查文件是否被更改clearstatcache filemtime 定期 由于整个文件总是很小 因此删除对 filemtime 的调用并通过将
  • 来自本地 XML 的模拟 SoapClient 响应

    我想用文件中的 XML 来模拟 SoapClient 的响应 我如何创建一个 stdClass 对象 就像 SoapClient 从文件返回一样 客户端已经包装了 SoapClient 因此可以轻松模拟响应 我的模拟是这样的 soapCli
  • 创建 Facebook 测试用户时访问令牌出现问题

    我正在尝试为我的 Facebook 应用程序创建测试用户 他们在 11 月份的博客文章 http developers facebook com blog post 429 中宣布了此功能 并在此处记录了该功能 http developer
  • chown:不允许操作

    我有问题 我需要通过 php 脚本为系统中的不同用户设置文件所有者权限 所以我通过以下命令执行此操作 其中 1002 是系统的用户 ID file put contents filename content system chown 100
  • 重新排列数组键 php [重复]

    这个问题在这里已经有答案了 我有这个数组 Array 15 gt 13 1 16 gt Mark one answer 19 gt You see a car on the hard shoulder of a motorway with
  • JavaFX 图像未在舞台中显示

    我尝试了很多次 尝试了很多方法 但都无法让自己的形象在舞台上如我所愿 我认为这可能与java寻找资源的路径有关 但我不确定 因为我刚刚开始使用视觉库 在本例中为JavaFX 这是我的目录结构 MyProject assets img myI
  • 具有更改用户代理上下文的 file_get_contents 不起作用

    我正在尝试获取页面的阅读数和点赞数 网址是 https mp weixin qq com s NPavBeHc8VdWXeSL6kfLRg https mp weixin qq com s NPavBeHc8VdWXeSL6kfLRg 您必
  • magento成功页面变量

    我正在尝试捕获一些 magento 成功页面变量以传递给我们的广告公司 到目前为止 我已经得到了这个 但变量没有输出任何内容 数据需要采用以下格式 price1 price2 price3 qty1 qty2 qty3 sku1 sku2
  • 更改API数据输出的布局

    我是 API 集成和 PHP 的新手 我最近将 VIN 解码器集成到我的应用程序中 在输入框中输入车辆的 VIN 选择提交 然后就会显示 API 数据库中有关该车辆的所有信息 数据存储为关联数组 其中包含类别及其相应元素 例如 对于 VIN
  • 使用 fopen() 包装器创建 ZIP 文件

    如何使用以下命令创建 ZIP 文件fopen 包装器 http es php net manual en wrappers compression php 这显然是not道路
  • 使用 PHP 和 OAuth 访问 SkyDrive

    我想使用 PHP 访问 skyDrive 我想检索文件和文件夹列表 下载 上传和删除文件 我有一个 microsoft dev clientID 和 clientSecret 有人可以帮助我开始使用 OAuth 连接到 skyDrive 并
  • Doctrine DQL 从 join 返回平面数组

    我通过 DQL 中的常规 LEFT JOIN 选择 3 个实体 它们通过连接表关联 连接表还定义了实体以及带注释的关系 查询执行没有问题 但我的结果作为平面数组返回 我期望一个包含三个实体作为每个索引的数组元素的数组 SELECT e1 e
  • 如何在 Carbon Laravel 中添加日期和另一个日期?

    在我的 laravel 项目中 我想将日期时间增加到前一个日期时间 这是我的代码 expire order 0 gt expire date new Carbon now gt addMonths 6 这两行的结果是 2018 01 28

随机推荐