• PHP将两张身份证照片合并为一张


    最近要开发一个功能,前端上传身份证照片的正反面,后台需要将该这两张身份证照片进行合并。

    参考:

    https://blog.csdn.net/weixin_34375251/article/details/93723189

    具体代码示例:

    public function mergeImage($one = '',$two = '',$name = 'idcard')
    {
        $rootPath = input('server.REQUEST_SCHEME') . '://' . input('server.SERVER_NAME');
        $oneImgPath = $rootPath . $one;
        $twoImgPath = $rootPath . $two;
        $oneImgObj = getimagesize($oneImgPath);
        $twoImgObj = getimagesize($twoImgPath);
    
        $oneImgWidth = $oneImgObj[0];
        $oneImgHeight = $oneImgObj[1];
        $twoImgWidth = $twoImgObj[0];
        $twoImgHeight = $twoImgObj[1];
    
        $canvasWidth = $oneImgWidth > $twoImgWidth ? $oneImgWidth : $twoImgWidth;
        $canvasHeight = $oneImgHeight + $twoImgHeight;
    
        $imgOne = imagecreatefromjpeg($oneImgPath);
        $imgTwo = imagecreatefromjpeg($twoImgPath);
    
        $canvas = imagecreatetruecolor($canvasWidth,$canvasHeight);
        imagefill($canvas,0,0,imagecolorallocate($canvas,255,255,255));
    
        $imgOneX = ($canvasWidth - $oneImgWidth) / 2;
        $imgTwoX = ($canvasWidth - $twoImgWidth) / 2;
    
        imagecopyresampled($canvas,$imgOne,intval($imgOneX),0,0,0,$oneImgWidth,$oneImgHeight, $oneImgWidth,$oneImgHeight);
        imagecopyresampled($canvas,$imgTwo,intval($imgTwoX),$oneImgHeight,0,0,$twoImgWidth,$twoImgHeight, $twoImgWidth,$twoImgHeight);
    
        $desPath = 'storage/upload/idcard/'.$name.'.jpg'; // 文件名字
        file_exists($desPath)&&unlink($desPath);
        imagejpeg($canvas,$desPath,100);
        return $desPath;
    }

    具体使用,仅需要掉用该方法的时候,传入需要合并的照片即可。

    // 正面 反面 命名
    $result = $this->mergeImage($idcardFront,$idcardBack,'idcard-45124545');

    打完收工!

  • 相关阅读:
    android 调试Installation failed with message INSTALL_FAILED_USER_RESTRICTED: Install canceled by user.
    selenium 调用方法
    正则去除空行
    tmux用法
    win10专业版激活
    11.17
    git reset,git checkout区别
    git reset revert区别
    python多线程,守护线程
    win7 32位安装 mong0db
  • 原文地址:https://www.cnblogs.com/e0yu/p/16262370.html
Copyright © 2020-2023  润新知