zoukankan      html  css  js  c++  java
  • php将两张身份证图片合并到一张图

        /**
         * @desc 合并身份证的正反面到同一张图片
         * @author Jimmy
         * @date 2016-12-33
         * @param $imageSrc0 身份证正面
         * @param $imageSrc1 身份证反面
         * @param $desPath 合并后图片地址
         * @return mixed
         */
        public static function mergeImage($imageSrc0, $imageSrc1, $dirPath){
            !file_exists($dirPath)&&mkdir($dirPath);
            $imagePath0=self::rotateImage($imageSrc0,$dirPath);
            $imagePath1=self::rotateImage($imageSrc1,$dirPath);
            $size0=getimagesize($imagePath0);
            $size1=getimagesize($imagePath1);
            $image0_width=$size0[0];
            $image0_height=$size0[1];
            $image1_width=$size1[0];
            $image1_height=$size1[1];
            if($image0_width>$image1_width){
                $canvas_width=$image0_width;
            }else{
                $canvas_width=$image1_width;
            }
            $canvas_height=$image0_height+$image1_height;
            $im0=imagecreatefromjpeg($imagePath0);
            $im1=imagecreatefromjpeg($imagePath1);
            $canvas=imagecreatetruecolor($canvas_width,$canvas_height);
            imagefill($canvas,0,0,imagecolorallocate($canvas,255,255,255));
            $img0_x=($canvas_width-$image0_width)/2;
            $img1_x=($canvas_width-$image1_width)/2;
            imagecopyresampled($canvas,$im0,intval($img0_x),0,0,0,$image0_width,$image0_height, $image0_width,$image0_height);
            imagecopyresampled($canvas,$im1,intval($img1_x),$image0_height,0,0,$image1_width,$image1_height, $image1_width,$image1_height);
            $desPath = $dirPath. Uuid::createUuid().'.jpg';//这里只是使用写好的Uuid给图片取一个唯一的名字
            file_exists($desPath)&&unlink($desPath);
            imagejpeg($canvas,$desPath,100);
            if($imagePath0!=$imageSrc0)file_exists($imagePath0)&&unlink($imagePath0);
            if($imagePath1!=$imageSrc1)file_exists($imagePath1)&&unlink($imagePath1);
            return $desPath;
        }
    
        /**
         * @desc 旋转长图片为宽图片
         * @author Jimmy
         * @date 2016-12-22
         * @param $imagePath
         * @return string
         */
        public static  function rotateImage($imagePath,$dirPath){
            !file_exists($dirPath)&&mkdir($dirPath);
            $size=getimagesize($imagePath);
            if($size[0]<$size[1]){//图片需要旋转90度
                $img=imagecreatefromjpeg($imagePath);
                $rotate=imagerotate($img,90,0);
                $desPath = dirname($dirPath). Uuid::createUuid().'.jpg';
                imagejpeg($rotate,$desPath);
                return $desPath;
            }else{
                return $imagePath;
            }
        }


    注意:由于用户上传的身份证正反面有可能是横向的也有可能是竖向的,程序考虑了横图片(长度大于宽度)和竖图片(长度小于宽度)的不同情况,

    遇到竖图片需要将之先旋转为横图片,这样才能让最后合成的图片都是横向的。

  • 相关阅读:
    Linux基础知识[1]【ACL权限】
    docker 入门学习篇【基本命令与操作】
    centos7.1下 Docker环境搭建
    RHEL6.5下更新python至2.7版本
    Github初学者探索
    vmware下linux虚拟机传文件解决方案之 xftp
    mysql 常用操作命令
    常用DNS记录
    常见网络协议端口号整理
    DNS原理及其解析过程 精彩剖析
  • 原文地址:https://www.cnblogs.com/JimmyBright/p/6406889.html
Copyright © 2011-2022 走看看