【问题标题】:Merging two images *created* in php into one将 php 中 *created* 的两个图像合并为一个
【发布时间】:2014-11-28 20:22:30
【问题描述】:

我有以下输出/显示为 PNG/PHP 图像的 PHP 脚本:

-image.php

-background.php

我希望能够打开第三个脚本“main.php”,它显示 image.php 覆盖在 background.php 上

我试过用常用的方法:

    <?php

$url1="image.php"
$url2="background.php";

$dest = imagecreatefrompng($url1);
$src = imagecreatefromjpeg($url2);


imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100);

header('Content-Type: image/png');
imagepng($dest);

imagedestroy($dest);
imagedestroy($src);

?>

但这并没有奏效(大概是因为源图像是php)。

关于如何合并这两个图像的任何想法?提前致谢

【问题讨论】:

    标签: php image merge


    【解决方案1】:

    您需要向 php 解释它需要执行 image.php 而不是将其包含在其“原始”形式中。 我能想到的最简单的方法是使用 curl_init 或 file_get_contents 之类的东西并将完整的 url 添加到 php 脚本中,以便您通过 http 打开文件并要求 Web 服务器为您执行它。

    所以把你的代码改成这样:

    <?php
    
    $url1= file_get_contents("http://example.com/image.php");
    $url2= file_get_contents("http://example.com/background.php");
    
    $dest = imagecreatefrompng($url1);
    $src = imagecreatefromjpeg($url2);
    
    
    imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100);
    
    header('Content-Type: image/png');
    imagepng($dest);
    
    imagedestroy($dest);
    imagedestroy($src);
    
    ?>
    

    HTH,

    bovako

    【讨论】:

      【解决方案2】:

      您应该尝试使用include 读取文件内容,然后使用imagecreatefromstring 创建图像:

      <?php
      
      $handle = imagecreatefromstring(include('image.php'));
      

      【讨论】:

        【解决方案3】:

        谢谢,它现在似乎可以工作了。我确实必须将 imagecreate 函数从 png/JPEG 更改为字符串(如下所示):

        再次感谢!使我免于发疯

        $url1= file_get_contents("http://example.com/image.php");
        $url2= file_get_contents("http://example.com/background.php");
        
        $dest = imagecreatefromstring($url1);
        $src = imagecreatefromstring($url2);
        
        
        imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100);
        
        header('Content-Type: image/png');
        imagepng($dest);
        
        imagedestroy($dest);
        imagedestroy($src);
        
        ?>
        

        【讨论】:

          猜你喜欢
          • 2016-04-15
          • 1970-01-01
          • 1970-01-01
          • 2012-10-10
          • 2012-02-10
          • 2011-04-22
          • 2014-06-14
          • 1970-01-01
          • 2011-05-21
          相关资源
          最近更新 更多