• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

php合并图片

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<?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);

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP的深copy和浅copy发布时间:2022-07-10
下一篇:
PHP数据类型验证和获取发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap