PHP-input上传图片(JS压缩->上传)后台掉扫二维码插件,扫描二维码获取二维码地址。直接进入二维码网址!
前端:
<style> div.file{display:inline-block;width:400px;height:400px;line-height:200px;position:relative;overflow:hidden;color:red;font-size:100px;} div.file input{position:absolute;width:500px;height:500px;left:50px;top:0px;zoom:1;filter:alpha(opacity=0);opacity:0;font-size:100px;} button:last-chile{width:200px;height:200px;} .btn{width:200px;height:200px; font-size:80px; } style> <form id="imageForm" class="bs-docs-example form-horizontal" method="post" action="https://www.beefvip.com/index.php?route=account/my/erweima" enctype="multipart/form-data"> <div class="file">扫一扫<input type="file" capture="camera" accept="image/*" id="image" name="image" multiple/>div> <input type="hidden" name="base64img" id="base64img"/> form> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> script> <script> $(function(){ $("input[type=file]").on('change', function(){ var filePath = $(this).val(), //获取到input的value,里面是文件的路径 fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase(), fileObj = document.getElementById($(this).attr('id')).files[0]; //上传文件的对象,要这样写才行,用jquery写法获取不到对象 var imgBase64str = ''; //存储图片的imgBase64 // 检查是否是图片 if( !fileFormat.match(/.png|.jpg|.jpeg/) ) { alert('上传错误,文件格式必须为:png/jpg/jpeg'); return; } var that = this; // 调用函数,对图片进行压缩 compress(fileObj,function(imgBase64){ imgBase64str = imgBase64;//存储转换后的base64编码 var reader = new FileReader(); file = that.files[0] reader.addEventListener("load", function () { $("#base64img").val(imgBase64str); document.forms[0].submit(); }, false); reader.readAsDataURL(file) }); }) }) // 对图片进行压缩 function compress(fileObj, callback){ if ( typeof (FileReader) === 'undefined') { console.log("当前浏览器内核不支持base64图标压缩"); //调用上传方式不压缩 directTurnIntoBase64(fileObj,callback); } else { var reader = new FileReader(); reader.onload = function (e) { //要先确保图片完整获取到,这是个异步事件 var image = new Image(); image.src=e.target.result; image.onload = function(){ square = 0.2, //定义画布的大小,也就是图片压缩之后的像素 canvas = document.createElement('canvas'), //创建canvas元素 context = canvas.getContext('2d'), imageWidth = Math.round(square*image.width), //压缩图片的大小 imageHeight = Math.round(square*image.height), data = ''; canvas.width = imageWidth; canvas.height = imageHeight; context.clearRect(0, 0, imageWidth, imageHeight); //在给定矩形内清空一个矩形 context.drawImage(this, 0, 0, imageWidth, imageHeight); var data = canvas.toDataURL('image/jpeg',0.6); //压缩完成执行回调 callback(data); }; }; reader.readAsDataURL(fileObj); } } // 不对图片进行压缩,直接转成base64 function directTurnIntoBase64(fileObj,callback){ var r = new FileReader(); // 转成base64 r.onload = function(){ //变成字符串 imgBase64 = r.result; //console.log(imgBase64); callback(imgBase64); } r.readAsDataURL(fileObj); //转成Base64格式 } script>
php后台代码:
public function erweima(){
// https://www.beefvip.com/index.php?route=account/my/erweima
//https://paystation.top/api/index/editimg
$base64img=$_REQUEST['base64img'];
$infoz_info = $this->base64_image_content($base64img,"/www/wwwroot/www.beefvip.com/upload/saoyisao/");
//echo($infoz_info['filepath']);
$path=$infoz_info['filepath'];
list($kong,$www, $wwwroot, $beefvip, $upload, $saoyisao, $img) = explode("/", $path);
$p = $beefvip.'/'.$saoyisao.'/'.$img;
//存进数据库
$this->load->model('account/allzong');
$info= $this->model_account_allzong->saoyisao($p);
// var_dump($info);
include_once 'lib/QrReader.php';// 引入识别二维码插件
// 读取二维码的缩略图并识别,识别二维码
$qrcode = new \QrReader($path);
$text = $qrcode->text(); //返回识别后的文本
// $uuid = explode("q/", $text)[1]; // 截取最后的参数
// var_dump($qrcode);
// var_dump($text);
if($text){
//header( "Location: $text" );
echo $text;
}else{
echo '0';
}
}
/**
* [将Base64图片转换为本地图片并保存]
* @param $base64_image_content [要保存的Base64]
* @param $path [要保存的路径]
* @return bool|string
*/
public function base64_image_content($base64_image_content,$path){
//匹配出图片的格式
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
$type = $result[2];
//生成文件名
$file_name = mt_rand(1,10).".{$type}";
//路径和文件名拼接
$local_file_url = $path.$file_name;
if (file_put_contents($local_file_url, base64_decode(str_replace($result[1], '', $base64_image_content)))){
return array("filename"=>$file_name,"path"=>$path,"filepath"=>$local_file_url);
}else{
return false;
}
}else{
return false;
}
}
lib二维码插件包下载地址:
链接:https://pan.baidu.com/s/1LdnHySv9-kHMbKyXbEcFnA
提取码:th68