1 前台样式+代码
<div class="a_li"> <label class="label"><em>图片</em>:</label> <div class="input"> <div class="uploader"> <div class="license license1"><img src="/backend/images/pic.png" onerror="this.src='/backend/images/pic.png'"/></div> <input type="file" class="up_file"/> <input type="text" class="file need" name="photo" required data-msg-required="请上传缩略图" style=" margin-left:-2000px;"/> <div class="tipinfo">图片大小:750px*300px<br>图片大小小于5MB<br>支持JPGPNGGIFJPEG格式的图片</div> </div> <div class="tip"></div> </div> </div> <script> $(function(){ //单图片上传 $('.up_file').localResizeIMG({ 720, quality: 1, success: function (result) { var img = new Image(); img.src = result.base64; $('.license1').html(img); $('.license1 img').attr("onerror","this.src='/backend/images/pic.png'") $(".file").val(result.base64); } }); }) </script>
2 控制器代码+图片保存位置
$photo = $request->getParameter('photo');//图片 if(preg_match('/^(data:s*image/(w+);base64,)/', $photo, $result)){ $type = $result[2]; $imgName = date("YmdHis").rand(10000,99999).".".$type; $new_file = sfConfig::get('app_img_host') . "/banner/{$imgName}"; if (file_put_contents('./uploads/banner/'.$imgName, base64_decode(str_replace($result[1], '', $photo)))){ $photos = $new_file; } }
3 展示效果
<td><img src="<?php echo $value['photo']?>" onerror="this.src='/backend/images/default.jpg'"></td>