PHP图片拼接util

签名图片拼接 

<?php
header("Content-type: text/html; charset=utf-8");

$res = new createIng();
$filename = "123" . '.jpg';

$reply = array();
$reply['font1'] = 905;
$reply['font2'] = 905;
$reply['reply7'] = "你也来生成一个试试看";
$reply['ewm'] = "[\"./ewm.jpg\",\"./ewm.jpg\",\"./ewm.jpg\"]";

$res->createImg($reply,"刘腾",1);

/*
for ($x=0; $x<=110; $x++) {
    $res->createImg($reply,$x.".png","马化腾",1);
}*/
echo 222;

class createIng
{
    public function createImg($reply,  $name, $count)
    {
        if ($count == 1) {
            $type = $reply['font1'];
        } elseif ($count == 2) {
            $type = $reply['font2'];
        }
        //id1:字体类型(905为一笔商务签名)  id2签名背景  id4:签名配色  id6 签名颜色
        $post_data = array('id' => $name, 'id1607' => '', 'id1609' => 'jiqie', 'id1608' => 'jiqie_com', 'id1' => 2, 'id2' => '#fce7c0', 'id3' => '', 'id4' => '#FFFFFF', 'idi' => 'jiqie', 'id5' => '', 'id6' => '#000000');
       // $post_data = array('id' => $name, 'id1607' => '', 'id1609' => 'jiqie', 'id1608' => 'jiqie_com', 'id1' => $type, 'id2' => '#fce7c0', 'id3' => '', 'id4' => '#FFFFFF', 'idi' => 'jiqie', 'id5' => '', 'id6' => '#000000');
        //加请求头
        $header=array('Accept-Language:zh-CN','x-appkey:114816004000028','x-apsignature:933931F9124593865313864503D477035C0F6A0C551804320036A2A1C5DF38297C9A4D30BB1714EC53214BD92112FB31B4A6FAB466EEF245710CC83D840D410A7592D262B09D0A5D0FE3A2295A81F32D4C75EBD65FA846004A42248B096EDE2FEE84EDEBEBEC321C237D99483AB51235FCB900AD501C07A9CAD2F415C36DED82','x-apversion:1.0','Content-Type:application/x-www-form-urlencoded','charset:utf-8','Accept:application/json','X-APFormat:json');
        //获取图片
        $res = $this->tocurl('http://www.yishuzi.com/b/re13.php',$header,$post_data);
        //$res = $this->tocurl('http://www.yishuzi.com/a/re.php',$header,$post_data);
        $reg = '/<img (.*?)+src=[\'"](.*?)[\'"]/i';
        preg_match_all( $reg , $res , $results );
        //打印出来
        // print_r($results[2][0]);die;

        //把整个文件读入一个字符串
        $content = file_get_contents($results[2][0]);
        $src_im = imagecreatefromstring($content);


        //创建一个画布
        $im = imagecreate(480, 350);
        //默认颜色是黑色,设置为透明
        $white = imagecolorallocate($im, 0,0,0);
        //给$im画布添加属性
        imagecolortransparent($im, $white);


        //二维码大小
        $newwidth = 120;
        $newheight = 120;

        if (!empty($reply['ewm'])) {
            $ewms = json_decode($reply['ewm'],true);
            if (empty($reply['ewm'])) {
                $ewm = $reply['ewm'];
            } else {
                shuffle($ewms);
                $ewm = $ewms[0];
            }
        }


        $src_im1 = imagecreatefromstring(file_get_contents($ewm));
        //创建的是一幅大小为 x和 y的图像(默认为黑色)
        $dst_im = imagecreatetruecolor($newwidth, $newheight);

        // 新建一个真彩色图像
        $color = imagecolorallocate($dst_im, 255, 255, 255);

        //将$dst_im图像中的透明色设定为 $color
        imagecolortransparent($dst_im, $color);

        //imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充
        imagefill($dst_im, 0, 0, $color);

        //imagecopyresized() 函数用于拷贝图像或图像的一部分并调整大小(生成缩略图),成功返回 TRUE,否则返回 FALSE。
        @imagecopyresized($dst_im, $src_im1, 0, 0, 0, 0, $newwidth, $newheight, imagesx($src_im1), imagesy($src_im1));
        //函数用于拷贝并合并图像的一部分(调整二维码的距离)
        imagecopymerge($im, $dst_im, 20, 220, 0, 0, $newwidth, $newheight, 100);

        //签名图与背景图合并
        $path_1 = "./bjImg.jpg";
        $image_1 = imagecreatefromjpeg($path_1);
        $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,$im, 0,0,0,0,imagesx($im),imagesy($im), 100);

        //加字体
        $stra = "姓名:" . $name;
        $black = imagecolorallocate($image_3, 0, 0, 0);
        imagettftext($image_3, 13, 0, 150, 293, $black, dirname(preg_replace('@\(.*\(.*$@', '', __FILE__)) . "/simkai1.ttf", $stra);
        imagecopy($image_3, $src_im, 32, 10, 30, 39, 420, 204);
        $str = $reply['reply7'];
        imagettftext($image_3, 14, 0, 150, 325, $black, dirname(preg_replace('@\(.*\(.*$@', '', __FILE__)) . "/simkai1.ttf", $str);



        //将画布保存到指定的gif文件
        imagejpeg($image_3, "./hero_gam.jpg");



        header("Content-type: image/jpeg");
        imagejpeg($image_3);
        exit();


        //压缩文件并保存在文件
      /*  imagejpeg($im, dirname(__FILE__) . $filename);

        header("Content-type: image/jpeg");
        imagejpeg($im);*/
    }


    function send_post($url, $post_data)
    {
        $postdata = http_build_query($post_data);
        $options = array('http' => array('method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $postdata, 'timeout' => 60));
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }

    /**
     * 发送数据
     * @param String $url     请求的地址
     * @param Array  $header  自定义的header数据
     * @param Array  $content POST的数据
     * @return String
     */
    public  function tocurl($url, $header,$post_data){
        $ch = curl_init();
        if(substr($url,0,5)=='https'){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));


        $response = curl_exec($ch);
        if($error=curl_error($ch)){
            die($error);
        }
        curl_close($ch);
        //var_dump($response);
        return $response;
    }

}

拼接主要代码:

//签名图与背景图合并
        $path_1 = "./bjImg.jpg";
        $image_1 = imagecreatefromjpeg($path_1);
        $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,$im, 0,0,0,0,imagesx($im),imagesy($im), 100);

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值