【问题标题】:How to merge multiple images and text into single image?如何将多个图像和文本合并为单个图像?
【发布时间】:2016-05-04 05:33:11
【问题描述】:

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

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

例如。出现在界面上的图像与图像和文本的组合。 (它设法以css定位出现) 这是由下面的两张图片和文字组成的

这是我尝试拍摄的图像: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);
 }
?>

但这给了我整个黑色的图像

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

已编辑

  • 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);
            }
        ?>

以及输出图像

【问题讨论】:

  • 我建议使用带有 alpha 通道的 png 而不是 jpeg 图像。否则很难达到预期的效果。
  • 参考以下链接stackoverflow.com/questions/4419383/…希望对你有用
  • 尝试使用imagecopy() 而不是imagecopymerge()
  • @MarkSetchell imagecopy() 改变了结果,但仍然没有占用整个宽度
  • 您要么需要在复制之前缩放图像,要么创建一个不是 1000x1000 像素,而是最大图像大小的图像

标签: php image php-gd


【解决方案1】:

首先,jpeg 图像没有 alpha 通道 - 这意味着图像中的每个像素都有某种颜色,而没有关于其透明度的信息。可能很难为此创建解决方法,但您可以谷歌搜索它。如果可以 - 我建议您使用包含透明度信息的 PNG 格式。

其次 - 尝试阅读 PHP docs for imagecopymerge 中的 cmets 部分。 This comment 提供了一些您可以使用的有用信息:

Sina Salek: 我刚刚检查了 PHP 的问题跟踪器,一位核心开发人员说 这个函数从来都不是为了支持 alpha 通道!和他们 拒绝提交提供的补丁!

然后评论者提供了一个我认为可行的例子。

【讨论】:

  • 是的,我实际上需要合并 png 图像,这只是测试。现在我使用了 png 图像并将它们合并,结果也更新了,
【解决方案2】:

我是这样实现的,

使用了这3张图片,img1.png,img2.png,img3.png

还有create-image.php文件

 <?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);
        }
    ?>

结果图像是

参考:imagecopyresized & imagettftext

感谢您通过 cmets/answers 提出的建议。 我还详细写了这篇博客http://sumankc.com/2016/01/30/merge-multiple-images-and-text-to-create-single-image-php-gd-library/ 美好的一天!!

【讨论】:

    猜你喜欢
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 2013-01-17
    • 2014-07-18
    • 2019-12-31
    相关资源
    最近更新 更多