php合并图片

<?php

class image{

    /**
     * @param string $backgroundImage  背景图
     * @param string $smallImage       小图
     * @param int $smallX              小图相对于背景图x轴位移
     * @param int $smallY              小图相对于背景图Y轴位移
     * @param string $output_path      输出图片的路径
     * @param string $output_filename  输出图片的名字
     * @return array
     */
    public function bindImages($backgroundImage='',$smallImage='',$smallX=0,$smallY=0,$output_path='',$output_filename='')
    {
        if(empty($backgroundImage) || empty($smallImage))
        {
            return array('result'=>"fail",'msg'=>"图片参数为空");
        }
        if(!file_exists($backgroundImage))
        {
            return array('result'=>"fail",'msg'=>"背景图片文件不存在");
        }
        if(!file_exists($smallImage))
        {
            return array('result'=>"fail",'msg'=>"小图片文件不存在");

        }
        if(empty($output_path))
        {
            $output_path='./';
        }

        //需要生成随机名称
        if(empty($output_filename))
        {
            $output_filename=md5(time()).'.png';
        }

        //imagecreatefrompng():创建一块画布,并从 PNG 文件或 URL 地址载入一副图像
        $image_1 = imagecreatefrompng($backgroundImage);
        $image_2 = imagecreatefrompng($smallImage);

        //分配图像资源
        $image_3 = imageCreatetruecolor(imagesx($image_1),imagesy($image_1));
        $color = imagecolorallocate($image_3, 255, 255, 255);
        imagefill($image_3, 0, 0, $color);
        imageColorTransparent($image_3, $color);

        //图像拷贝
        imagecopyresampled($image_3,$image_1,0,0,0,0,imagesx($image_1),imagesy($image_1),imagesx($image_1),imagesy($image_1));

        imagecopymerge($image_3,$image_2, $smallX,$smallY,0,0,imagesx($image_2),imagesy($image_2), 100);
        //将画布保存到指定的png文件
        $result=imagepng($image_3, $output_path.$output_filename);


        if($result==true)
        {
            return array('result'=>"success",'msg'=>"合成成功",'imgurl'=>$output_path.$output_filename);
        }
        else
        {
            return array('result'=>"fail",'msg'=>"合成失败");
        }
    }
}

$obj=new image();
$t=$obj->bindImages('background.png','3.png',200,200);
原文地址:https://www.cnblogs.com/norm/p/7285294.html