首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在php中合并两个png图像

在php中合并两个png图像
EN

Stack Overflow用户
提问于 2015-08-02 20:47:15
回答 2查看 520关注 0票数 0

我正在尝试在php中合并我的两个图像。一张图片正在上传到我的系统中,另一张是我创建的透明背景图片。这是我的代码。我的代码只显示了一个非图像图标。我不明白我哪里错了。

代码语言:javascript
复制
    <?php
    //Set the Content Type
    header("Content-type: image/png");
    #dispaly the image
    $file=$_GET['file'];
    // echo file_get_contents($file);
    $im = imagecreatetruecolor(250, 200);
    $black = imagecolorallocate($im, 255, 255, 255);
    $blue = imagecolorallocate($im, 0, 0, 255);

    imagecolortransparent($im, $black);

    //text to draw
    $text="hello world";
    //font path
    $font = '/usr/share/fonts/truetype/droid/DroidSans.ttf';
    // Add the text
    imagettftext($im, 15, 0, 50, 50, $blue, $font, $text);

    $dest=imagecreatefrompng($file);
    $src=imagecreatefrompng($im);

    imagealphablending($dest, false);
    imagesavealpha($dest, true);

    imagecopymerge($dest, $src, 10, 10, 0, 0, 100, 250, 200);

    imagepng($dest);

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

    ?>
EN

回答 2

Stack Overflow用户

发布于 2015-09-01 21:15:07

使用$im而不是$src -正如赛义德指出的那样,imagecreatefrompng以文件名(字符串)作为参数-而不是GD资源。如果$im已经包含可以使用的GD资源,为什么还要设置$src?

imagettftext有一个很重要的部分。如果GD在给定的路径中找不到字体,我可以重现空图标的效果。检查您的位置、权限和字母大小写。此外,如果您决定直接将.ttf文件复制到脚本位置,请参考imagettftext() documentation,因为".ttf“扩展名有重要的警告。

此外,要创建完全透明的图像,请使用:( George Edison在PHP doc for imagefill

代码语言:javascript
复制
    $im = imagecreatetruecolor(317, 196);
    $transparent =  imagecolorallocatealpha($im, 0, 0, 0, 127);
    imagefill($im, 0, 0, $transparent);
    imagesavealpha($im, TRUE);

另外,来自Sina Salek的PHP doc for imagecopymerge():imagecopymerge_alpha函数在imagecopymerge()中提供真正的透明性。

所以,我的解决方案是:

代码语言:javascript
复制
    <?php
    //Set the Content Type
    header("Content-type: image/png");
    #dispaly the image
    $file='test.png';

    $im = imagecreatetruecolor(317, 196);
    $transparent =  imagecolorallocatealpha($im, 0, 0, 0, 127);
    imagefill($im, 0, 0, $transparent);
    imagesavealpha($im, TRUE);

    $blue = imagecolorallocatealpha($im, 0, 0, 255, 0);

    //text to draw
    $text="hello world";
    putenv('GDFONTPATH=' . realpath('.'));
    $font = 'lucida';
    imagettftext($im, 20, 0, 10, 50, $blue, $font, $text);

    $dest=imagecreatefrompng($file);
    imagealphablending($dest, false);
    imagesavealpha($dest, true);

    imagecopymerge_alpha($dest, $im, 10, 10, 0, 0, 200, 180, 100);
    imagepng($dest);

    imagedestroy($dest);
    imagedestroy($im);

    function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
        // creating a cut resource
        $cut = imagecreatetruecolor($src_w, $src_h);

        // copying relevant section from background to the cut resource
        imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);

        // copying relevant section from watermark to the cut resource
        imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);

        // insert cut resource to destination image
        imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
    }
    ?>
票数 0
EN

Stack Overflow用户

发布于 2015-09-02 16:02:04

为此,您应该使用imagecopymerge()函数。

查找链接

代码语言:javascript
复制
http://php.net/manual/en/function.imagecopymerge.php
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31772036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档