在php中合并两个png图像

时间:2015-08-02 12:47:15

标签: php image gd

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

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

    ?>

2 个答案:

答案 0 :(得分:0)

使用$ im而不是$ src - 正如Sayed指出的那样,imagecreatefrompng将filename(string)作为参数 - 而不是GD资源。如果$ im已经包含可以使用的GD资源,为什么设置$ src?

imagettftext有重要的部分。如果GD无法在给定路径中找到字体,我能够重现空图标的效果。检查您的位置,权限和信箱。此外,如果您决定只将.ttf文件复制到脚本位置,请参阅imagettftext() documentation,因为有一个重要的警告,即#34; .ttf&#34;扩展

另外,要创建完全透明的图像使用: (由乔治爱迪生在PHP doc for imagefill

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

另外,来自新浪Salek的PHP doc for imagecopymerge():imagecopymerge_alpha函数在imagecopymerge中提供真正的transperency()

所以,我的解决方案:

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

答案 1 :(得分:0)

您应该使用imagecopymerge()功能。

找出链接

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