图片转Base64上传,PHP获取转码为图片
先写一下JS函数和调用
<script> /** * * @param id{string} * @param [width]{number} * @param [quality]{number} * @param [type]{string} * @constructor * @return [base64格式]{string} */ function MinImg(id, width, quality, type) { var a = this; a.width = width || 640; a.quality = quality || 0.8; a.type = type || "image/jpeg"; var readFile = function () { var file = this.files[0]; if (!file) { return } if (!/image\/\w+/.test(file.type)) { a.error('格式错误') return false } else { var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function (e) { var img = new Image(), canvas = document.createElement('canvas'), drawer = canvas.getContext("2d"); img.src = e.target.result; img.onload = function () { a.height = a.width * (img.height / img.width); canvas.width = a.width; canvas.height = a.height; drawer.drawImage(img, 0, 0, a.width, a.height); a.result = canvas.toDataURL(a.type, a.quality); a.success(a.result, e.target.result) } } } } if (typeof(FileReader) === 'undefined') { a.error('浏览器不支持'); } else { var input = document.getElementById(id); input.addEventListener('change', readFile, false); } } // 好啦,告诉你怎么调用的(^_^) var file_s = new MinImg('id','',0.95) file_s.success = function (y,n){ var i_id = $("#i_id").val(); $.post(action_img,{id:i_id,file_img:y},function(res){ // res=JSON.encode(res) if (res.code == 100){ $("#y_img").css('display','none'); var img = document.querySelector('#fileImg') img.style.display = 'block'; img.src = y; alert(res.data) }else{ alert(res.data); } },'json') } file_s.error = function (err) { alert(err) } </script>
嗯,是时候表演真正的技术了(哈哈,小装一下...)
class UploadBase64 { /** * $base64 {string} base64格式 * $link {string} 文件存放路径 * * return $arr{f_name:文件名;fileName:文件路径} */ public static function imgsList($base64,$link='./'){ $base64 = explode(',',$base64); $base64s = $base64[1]; $a = str_replace(';base64','',$base64['0']); $a = str_replace('data:image/','',$a); $data = base64_decode($base64s); $f_name = uniqid() .'.'. $a; $fileName = $link; if (!is_dir($fileName) || !is_writable($fileName)){ is_dir($fileName) or mkdir(iconv("UTF-8", "GBK", $fileName),0777,true); } $success = file_put_contents($fileName.'/'.$f_name, $data); if (!$success){ return $success; } $arr = ['f_name'=>$f_name,'fileName'=>$fileName.'/'.$f_name]; return $arr; } /** * 裁剪图片为缩略图 * $link 图片存放路径 * $status = 1 按 $wid 的倍数缩放 * $status = 2 按固定像素缩放,$wid为宽,$hei为高 */ public static function cutImgs($img,$link='./',$status=1,$wid=0,$hei=0){ list($src_w,$src_h)=getimagesize($img); $a_u = strrchr($img,'.'); $b_h = str_replace('.','',$a_u); $fileName = '../../upload/web/thumb/'.$link; if (!is_dir($fileName) || !is_writable($fileName)){ is_dir($fileName) or mkdir(iconv("UTF-8", "GBK", $fileName),0777,true); } if ($status == 1){ if($wid!=0&&$hei!=0){ $w = intval($src_w/$wid); $h = intval($src_h/$wid); }else{ $w = intval($src_w); $h = intval($src_h); } $source=imagecreatefromjpeg($img); $target = imagecreatetruecolor($w, $h); $res=imagecopyresampled($target, $source, 0, 0, 0, 0, $w,$h, $src_w, $src_h); $postfix = explode(".", $img); $files = uniqid(). rand(1000, 9999) . '.' . end($postfix); $tt=Yii::getAlias("@upload").'/web/thumb/'.$link.'/'.$files; imagejpeg($target, $tt); imagedestroy($target); return $files; } if ($status == 2){ $target = imagecreatetruecolor($wid, $hei); if ($b_h == 'png'){ $source=imagecreatefrompng($img); //设置颜色 $color=imagecolorallocate($target,255,255,255); imagecolortransparent($target,$color); imagefill($target,0,0,$color); }else if($b_h == 'gif'){ $source=imagecreatefromgif($img); //设置颜色 $color=imagecolorallocate($target,255,255,255); imagecolortransparent($target,$color); imagefill($target,0,0,$color); }else{ $source=imagecreatefromjpeg($img); } if ($src_h >= $src_w){ $y = ($src_h-$src_w)/2; $x = 0; $src_h = $src_w; }else{ $x = ($src_w-$src_h)/2; $y = 0; $src_w = $src_h; } imagecopyresampled($target, $source, 0, 0, $x, $y, $wid,$hei, $src_w, $src_h); $postfix = explode(".", $img); $files = uniqid(). rand(1000, 9999) . '.' . end($postfix); $tt=$fileName.'/'.$files; imagejpeg($target, $tt); imagedestroy($target); return $files; } } } /** * * $base64 {string} base64格式 * * $path {string} 路径 */ // 出来吧!皮卡丘!!! $file_path = UploadBase64::imgsList($base64, $path); // 上传base64的原图 // file_path['fileName']:图片路径+图片名; file_path['f_name']:图片名 $thumb = UploadBase64::cutImgs($file_path['fileName'], $path_s, 2, 450, 450);//上传缩略图,只返回图片名
打完收工(~ ̄▽ ̄)~