美文网首页
多图合成+缩放

多图合成+缩放

作者: observerb | 来源:发表于2019-06-20 19:29 被阅读0次

<?php

$QR ="image/B2.png";    //区域图

$QR2 ="image/C2.png";  //区域图

$QR3 ="image/D1.png";  //区域图

$QR4 ="image/E2.png";  //区域图

// $QR5 ="image/A1.png";

$backgroundpic_width = 750;//画布宽度

$backgroundpic_height = 1338;//画布高度

$im = imagecreatetruecolor($backgroundpic_width,$backgroundpic_height);

$bg = imagecolorallocate($im, 255, 255, 255); //设置画布的背景为白色

imagefill($im,0,0,$bg);

//imagecopyresampled介绍:(第一个参数:画布,第二个参数:输入的图片,第三个参数:X坐标,第四个参数:Y坐标,第五个参数:输出图width,第六个参数:输出图height,第七个参数:输入图片原图截取width,第八个:输入图片原图截取height)

//$dotpic = imagecreatefromstring(file_get_contents($dotpic));

$userbgpic = imagecreatefromstring(file_get_contents("image/A2.jpg"));//OB选择背景

imagecopyresampled($im, $userbgpic, 0, 0, 0, 0, 750,1338,750,1338);    //输出底图

// 第一张图

$QR = imagecreatefromstring(file_get_contents($QR));

$QR_width =  imagesx($QR);//二维码图片宽度

$QR_height = imagesy($QR);//二维码图片高度

imagecopyresampled($im, $QR, 0, 0, 0, 0, 750,1338,$QR_width,$QR_height); //输出二维码

// 第二张图

$QR2 = imagecreatefromstring(file_get_contents($QR2));

// $QR_width2 = imagesx($QR2);//二维码图片宽度

// $QR_height2 = imagesy($QR2);//二维码图片高度

imagecopyresampled($im, $QR2, 0, 0, 0, 0, 750,1338,$QR_width,$QR_height); //输出二维码

// 第三张图

$QR3 = imagecreatefromstring(file_get_contents($QR3));

// $QR_width3 = imagesx($QR3);//二维码图片宽度

// $QR_height3 = imagesy($QR3);//二维码图片高度

imagecopyresampled($im, $QR3, 0, 0, 0, 0, 750,1338,$QR_width,$QR_height); //输出二维码

// 第四张图

$QR4 = imagecreatefromstring(file_get_contents($QR4));

// $QR_width4 = imagesx($QR2);//二维码图片宽度

// $QR_height4 = imagesy($QR2);//二维码图片高度

imagecopyresampled($im, $QR4, 0, 0, 0, 0, 750,1338,$QR_width,$QR_height); //输出二维码

//imagettftext($im,20,0,150,590, $textcolor, $font,"长按此图识别图片中二维码");

header('Content-type:image/png');

imagepng($im,'ob/ob2.png');/*  */

imagedestroy($im);

imagedestroy($QR);

imagedestroy($QR2);

imagedestroy($QR3);

imagedestroy($QR4);

CreateThumbnail('ob/ob2.png',548,978,'ob/ob3.png');

function CreateThumbnail($srcFile, $toW, $toH, $toFile="") 

    { 

        if ($toFile == "") 

        { 

            $toFile = $srcFile; 

        } 

        $info = ""; 

        //返回含有4个单元的数组,0-宽,1-高,2-图像类型,3-宽高的文本描述。 

        //失败返回false并产生警告。 

        $data = getimagesize($srcFile, $info); 

        if (!$data) 

            return false; 

        //将文件载入到资源变量im中 

        switch ($data[2]) //1-GIF,2-JPG,3-PNG 

        { 

        case 1: 

            if(!function_exists("imagecreatefromgif")) 

            { 

                echo "the GD can't support .gif, please use .jpeg or .png! <a href='javascript:history.back();'>back</a>"; 

                exit(); 

            } 

            $im = imagecreatefromgif($srcFile); 

            break; 

        case 2: 

            if(!function_exists("imagecreatefromjpeg")) 

            { 

                echo "the GD can't support .jpeg, please use other picture! <a href='javascript:history.back();'>back</a>"; 

                exit(); 

            } 

            $im = imagecreatefromjpeg($srcFile); 

            break; 

        case 3: 

            $im = imagecreatefrompng($srcFile);     

            break; 

        } 

        //计算缩略图的宽高 

        $srcW = imagesx($im); 

        $srcH = imagesy($im); 

        $toWH = $toW / $toH; 

        $srcWH = $srcW / $srcH; 

        if ($toWH <= $srcWH) 

        { 

            $ftoW = $toW; 

            $ftoH = (int)($ftoW * ($srcH / $srcW)); 

        } 

        else 

        { 

            $ftoH = $toH; 

            $ftoW = (int)($ftoH * ($srcW / $srcH)); 

        } 

        if (function_exists("imagecreatetruecolor")) 

        { 

            $ni = imagecreatetruecolor($ftoW, $ftoH); //新建一个真彩色图像 

            if ($ni) 

            { 

                //重采样拷贝部分图像并调整大小 可保持较好的清晰度 

                imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); 

            } 

            else 

            { 

                //拷贝部分图像并调整大小 

                $ni = imagecreate($ftoW, $ftoH); 

                imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); 

            } 

        } 

        else 

        { 

            $ni = imagecreate($ftoW, $ftoH); 

            imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); 

        } 

        //保存到文件 统一为.png格式 

        imagepng($ni, $toFile); //以 PNG 格式将图像输出到浏览器或文件 

        ImageDestroy($ni); 

        ImageDestroy($im); 

        return true; 

    } 

相关文章

网友评论

      本文标题:多图合成+缩放

      本文链接:https://www.haomeiwen.com/subject/urfsqctx.html