• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php+js iframe实现上传头像界面无跳转_php技巧

php 搞代码 3年前 (2022-01-26) 31次浏览 已收录 0个评论

上传头像,界面无跳转的方式很多,我用的是加个iframe那种。下面直接上代码。

html:

 <BR>//route 为后端接口 <BR>//upload/avatar 为上传的头像的保存地址 <BR>//imgurl=/upload/avatar/<?=$uid?> 这里最后的<?=$uid?>是为了后面用js实现即时显示最新的更换后的头像用的,参照下面的js部分的代码 <BR>//头像保存名称为uid.type,如1.jpg,2.png等 <BR>//$user['avatar'] 用户如果上传过头像,该用户数据库中的avatar字段将赋予时间戳,否则为空 <BR>" method="post" id="upload_form"> <BR>" style="width:100px; height:100px;" /> <BR> <BR> <BR> <BR><iframe id="iframe" name="iframe" style="display: none;"></iframe> <BR>


php:

 <BR>$token = param('token'); <BR>$user = user_from_token($token); <BR>!$user AND exit("<p class='iframe_message'>$lang[user_not_exists]</p>"); <BR>//文件存储路径 <BR>$file_path="./upload/avatar/"; <BR>//664权限为文件属主和属组用户可读和写,其他用户只读。 <BR>if(is_dir($file_path) != TRUE) mkdir($file_path, 0664) ; <BR>//定义允许上传的文件扩展名 <BR>$ext_arr = array("gif", "jpg", "jpeg", "png", "bmp"); <br><br>if (empty($_FILES) === false) { <BR>  //判断检查 <BR>  $photo_up_size > 2097152 AND exit("<p class='iframe_message'>对不起,您上传的照片超过了2M</p>"); <BR>  $_FILES["file"]["error"] > 0 AND exit("<p class='iframe_message'>文件上传发生错误:".$_FILES["file"]["error"]."</p>"); <BR>  //获得文件扩展名 <BR>  $temp_arr = explode(".", $_FILES["file"]["name"]); <BR>  $file_ext = array_pop($temp_arr); <BR>  $file_ext = trim($file_ext); <BR>  $file_ext = strtolower($file_ext); <BR>  //检查扩展名 <BR>  if (in_array($file_ext, $ext_arr) === false) { <BR>    exit("<p class='iframe_message'>上传文件扩展名是不允许的扩展名</p>"); <BR>  } <BR>  //删除目录下相同前缀的文件 <BR>  if($dh = opendir($file_path)) { <BR>    while(($file = readdir($dh)) !== false) { <BR>      $file_arr = $file.split('.'); <BR>      if($file_arr[0] == $user['uid']) unlink($file_path.$file); <BR>    } <BR>  } <BR>  //以uid重命名文件 <BR>  $new_name = $user['uid'].".".$file_ext; <BR>  //将文件移动到存储目录下 <BR>  move_uploaded_file($_FILES["file"]["tmp_name"], $file_path.$new_name); <BR>  //裁剪压缩图片 <BR>  clip($file_path.$new_name, $file_path.$new_name, 0, 0, 100, 100); <BR>  clip_thumb($file_path.$new_name, $file_path.$new_name, 100, 100); <BR>  //向数据表写入文件存储信息以便管理 <BR>  user_update($user['uid'], array('avatar'=>time())); <BR>  exit("<p class='iframe_message'>文件上传成功</p>"); <BR>} else { <BR>  exit("<p class='iframe_message'>无正确的文件上传</p>"); <BR>} <br><br><?php <br><br>function ext($filename) { <BR>return strtolower(substr(strrchr($filename, '.'), 1)); <BR>} <br><br>/* <BR>实例: <BR>thumb(APP_PATH.'xxx.jpg', APP_PATH.'xxx_thumb.jpg', 200, 200); <br><br>返回: <BR>array('filesize'=>0, 'width'=>0, 'height'=>0) <BR>*/ <BR>function thumb($sourcefile, $destfile, $forcedwidth = 80, $forcedheight = 80) { <BR>$return = array('filesize'=>0, 'width'=>0, 'height'=>0); <BR>$imgcomp = 10; <BR>$destext = ext($destfile); <BR>if(!in_array($destext, array('gif', 'jpg', 'bmp', 'png'))) { <BR>return $return; <BR>} <br><br>$imgcomp = 100 - $imgcomp; <BR>$imginfo = getimagesize($sourcefile); <BR>$src_width = $imginfo[0]; <BR>$src_height = $imginfo[1]; <BR>if($src_width == 0 || $src_height == 0) { <BR>return $return; <BR>} <BR>$src_scale = $src_width / $src_height; <BR>$des_scale = $forcedwidth / $forcedheight; <br><br>if(!function_exists('imagecreatefromjpeg')) { <BR>copy($sourcefile, $destfile); <BR>$return = array('filesize'=>filesize($destfile), 'width'=>$src_width, 'height'=>$src_height); <BR>return $return; <BR>} <br><br>// 按规定比例缩略 <BR>if($src_width <= $forcedwidth && $src_height <= $forcedheight) { <BR>$des_width = $src_width; <BR>$des_height = $src_height; <BR>} elseif($src_scale >= $des_scale) { <BR>$des_width = ($src_width >= $forcedwidth) ? $forcedwidth : $src_width; <BR>$des_height = $des_width / $src_scale; <BR>$des_height = ($des_height >= $forcedheight) ? $forcedheight : $des_height; <BR>} else { <BR>$des_height = ($src_height >= $forcedheight) ? $forcedheight : $src_height; <BR>$des_width = $des_height * $src_scale; <BR>$des_width = ($des_width >= $forcedwidth) ? $forcedwidth : $des_width; <BR>} <br><br>switch ($imginfo['mime']) { <BR>case 'image/jpeg': <BR>$img_src = imagecreatefromjpeg($sourcefile); <BR>!$img_src && $img_src = imagecreatefromgif($sourcefile); <BR>break; <BR>case 'image/gif': <BR>$img_src = imagecreatefromgif($sourcefile); <BR>!$img_src && $img_src = imagecreatefromjpeg($sourcefile); <BR>break; <BR>case 'image/png': <BR>$img_src = imagecreatefrompng($sourcefile); <BR>break; <BR>case 'image/wbmp': <BR>$img_src = imagecreatefromwbmp($sourcefile); <BR>break; <BR>default : <BR>return $return; <BR>} <br><br>$img_dst = imagecreatetruecolor($des_width, $des_height); <BR>$img_color = imagecolorallocate($img_dst, 255, 255, 255); <BR>imagefill($img_dst, 0, 0 ,$img_color); <BR>imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $des_width, $des_height, $src_width, $src_height); <BR>//$tmpfile = $temp_path.md5($destfile); <BR>$tmpfile = $destfile; <BR>switch($destext) { <BR>case 'jpg': imagejpeg($img_dst, $tmpfile, $imgcomp); break; <BR>case 'gif': imagegif($img_dst, $tmpfile, $imgcomp); break; <BR>case 'png': imagepng($img_dst, $tmpfile, version_compare(PHP_VERSION, '5.1.2') == 1 ? 7 : 70); break; <BR>} <BR>$r = array('filesize'=>filesize($tmpfile), 'width'=>$des_width, 'height'=>$des_height);; <BR>copy($tmpfile, $destfile); <BR>//unlink($tmpfile); <BR>imagedestroy($img_dst); <BR>return $r; <BR>} <BR>/* <BR>* 图片裁切 <BR>* <BR>* @param string $srcname 原图片路径(绝对路径/*.jpg) <BR>* @param string $forcedheight 裁切后生成新名称(绝对路径/rename.jpg) <BR>* @param int $sourcefile 被裁切图片的X坐标 <BR>* @param int $destfile 被裁切图片的Y坐标 <BR>* @param int $destext 被裁区域的宽度 <BR>* @param int $imgcomp 被裁区域的高度 <BR>clip('xxx/x.jpg', 'xxx/newx.jpg', 10, 40, 150, 150) <BR>*/ <BR>function clip($sourcefile, $destfile, $clipx, $clipy, $clipwidth, $clipheight) { <BR>$getimgsize = getimagesize($sourcefile); <BR>if(empty($getimgsize)) { <BR>return 0; <BR>} else { <BR>$imgwidth = $getimgsize[0]; <BR>$imgheight = $getimgsize[1]; <BR>if($imgwidth == 0 || $imgheight == 0) { <BR>return 0; <BR>} <BR>} <br><br>if(!function_exists('imagecreatefromjpeg')) { <BR>copy($sourcefile, $destfile); <BR>return filesize($destfile); <BR>} <BR>switch($getimgsize[2]) { <BR>case 1 : <BR>$imgcolor = imagecreatefromgif($sourcefile); <BR>break; <BR>case 2 : <BR>$imgcolor = imagecreatefromjpeg($sourcefile); <BR>break; <BR>case 3 : <BR>$imgcolor = imagecreatefrompng($sourcefile); <BR>break; <BR>} <BR>//$imgcolor = imagecreatefromjpeg($sourcefile); <BR>$img_dst = imagecreatetruecolor($clipwidth, $clipheight); <BR>$img_color = imagecolorallocate($img_dst, 255, 255, 255); <BR>imagefill($img_dst, 0, 0, $img_color); <BR>imagecopyresampled($img_dst, $imgcolor, 0, 0, $clipx, $clipy, $imgwidth, $imgheight, $imgwidth, $imgheight); <BR>$tmpfile = $destfile; <BR>imagejpeg($img_dst, $tmpfile, 100); <BR>$n = filesize($tmpfile); <BR>copy($tmpfile, $destfile); <BR>return $n; <BR>} <br><br>// 先裁切后缩略,因为确定了,width, height, 不需要返回宽高。 <BR>function clip_thumb($sourcefile, $destfile, $forcedwidth = 80, $forcedheight = 80) { <BR>// 获取原图片宽高 <BR>$getimgsize = getimagesize($sourcefile); <BR>if(empty($getimgsize)) { <BR>return 0; <BR>} else { <BR>$src_width = $getimgsize[0]; <BR>$src_height = $getimgsize[1]; <BR>if($src_width == 0 || $src_height == 0) { <BR>return 0; <BR>} <BR>} <br><br>$src_scale = $src_width / $src_height; <BR>$des_scale = $forcedwidth / $forcedheight; <br><br>if($src_width <= $forcedwidth && $src_height <= $forcedheight) { <BR>$des_width = $src_width; <BR>$des_height = $src_height; <BR>$n = clip($sourcefile, $destfile, 0, 0, $des_width, $des_height); <BR>return filesize($destfile); <BR>// 原图为横着的矩形 <BR>} elseif($src_scale >= $des_scale) { <BR>// 以原图的高度作为标准,进行缩略 <BR>$des_height = $src_height; <BR>$des_width = $src_height / $des_scale; <BR>$n = clip($sourcefile, $destfile, 0, 0, $des_width, $des_height); <BR>if($n <= 0) return 0; <BR>$r = thumb($destfile, $destfile, $forcedwidth, $forcedheight); <BR>return $r['filesize']; <BR>// 原图为竖着的矩形 <BR>} else { <BR>// 以原图的宽度作为标准,进行缩略 <BR>$des_width = $src_width; <BR>$des_height = $src_width / $des_scale; <BR>$n = clip($sourcefile, $destfile, 0, 0, $des_width, $des_height); <BR>if($n <= 0) return 0; <BR>$r = thumb($destfile, $destfile, $forcedwidth, $forcedheight); <BR>return $r['filesize']; <BR>} <BR>} <br><br>?> <BR>


我们php中设置返回内容,会发现,在出现相应情况后,返回内容出现在页面的iframe中,所以我们设定了相应的class,以便前端获得返回内容,做出相应处理。clip(),clip_thumb()为裁剪图片函数,可压缩图片大小,裁取图片以左上角为起点,长宽为100的正方形。

js:

 <BR>var jsubmit_file = jinput.filter('[name="submit_file"]'); <BR>var jfile = jinput.filter('[name="file"]'); <BR>var jiframe = $('#iframe'); <BR>var jthumb = $('.thumb'); <BR>var type = ''; <BR>jfile.on('change', function() { <BR>var path = jfile.val(); <BR>var file_arr = path.split('.'); <BR>type = file_arr[file_arr.length-1]; <BR>jsubmit_file.trigger('click'); <BR>}); <BR>jiframe.on('load', function() { <BR>var jiframe_message = $(window.frames['iframe'].document).find('.iframe_message'); <BR>if(jiframe_message.attr('status') != 0) { <BR>var url = this.contentWindow.location.href; <BR>var url_arr = url.split('='); <BR>jthumb.attr('src', url_arr[1] + '.' + type); <BR>} <BR>alert(jiframe_message.text()); <BR>}); <BR>


这样基本就实现了/本文来源gao@!dai!ma.com搞$$代^@码5网@搞代gaodaima码图片上传、上传结果提示、即时显示上传的最新头像这几个功能,网上有各种插件,虽然功能丰富,就是体积太大,这个看我们取舍了。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php+js iframe实现上传头像界面无跳转_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址