html5+php实现文件拖动上传功能

作者:袖梨 2022-06-25

  界面样式我是参考了一个国外的相册网站,改动不大,只是把鸟语转换成中文,以及上传时的样式也进行了改动,之所以选这个的原因就是,我很容易做扩展,它支持3种方式添加图片,一种拖拽上传,一种常规的选择文件上传,另外的就是添加网络图片。它很巧妙的把三种上传模式整合到了一起,而且你可以用IE浏览器浏览下,如果不支持HTML5,是没有拖拽上传图片的提示的,如图:

  拖拽上传最重要的就是js部分的代码,它实现了70%的功能,另外30%仅仅是把图片信息提交到后台,然后做对应的处理,比如压缩啊,裁剪啊云云。所以先来看下js实现代码吧。

 代码如下 复制代码





无标题文档





 

  

选择你的图片
开始上传


  


   拖动图片到这里
开始上传图片
  


 

 

选择网络图片


 


  
  
 






test.php文件

 代码如下 复制代码

 $r = new stdClass();
 header('content-type: application/json');
 $maxsize = 10; //Mb
 if($_FILES['xfile']['size'] > ($maxsize * 1048576)){
  $r->error = "图片大小">图片大小不超过 $maxsize MB";
 }
 $folder = 'files/';
 if(!is_dir($folder)){
  mkdir($folder);
 }
 $folder .= $_POST['folder'] ? $_POST['folder'] . '/' : '';
 if(!is_dir($folder)){
  mkdir($folder);
 }
 
 if(preg_match('/image/i', $_FILES['xfile']['type'])){
     $filename = $_POST['value'] ? $_POST['value'] : $folder . sha1(@microtime() . '-' . $_FILES['xfile']['name']) . '.jpg';
 }else{
     $tld = split(',', $_FILES['xfile']['name']);
     $tld = $tld[count($tld) - 1];
     $filename = $_POST['value'] ? $_POST['value'] : $folder . sha1(@microtime() . '-' . $_FILES['xfile']['name']) . $tld;
 }
 
 $types = Array('image/png', 'image/gif', 'image/jpeg');
 if(in_array($_FILES['xfile']['type'], $types)){
  $source = file_get_contents($_FILES["xfile"]["tmp_name"]);
  imageresize($source, $filename, $_POST['width'], $_POST['height'], $_POST['crop'], $_POST['quality']);
 }else{
  move_uploaded_file($_FILES["xfile"]["tmp_name"], $filename);
 }
 
 $path = str_replace('test.php', '', $_SERVER['SCRIPT_NAME']);
 
 $r->filename = $filename;
 $r->path = $path;
 $r->img = 'image';
 
 echo json_encode($r);
 
 function imageresize($source, $destination, $width = 0, $height = 0, $crop = false, $quality = 80) {
     $quality = $quality ? $quality : 80;
     $image = imagecreatefromstring($source);
     if ($image) {
         // Get dimensions
         $w = imagesx($image);
         $h = imagesy($image);
         if (($width && $w > $width) || ($height && $h > $height)) {
             $ratio = $w / $h;
             if (($ratio >= 1 || $height == 0) && $width && !$crop) {
                 $new_height = $width / $ratio;
                 $new_width = $width;
             } elseif ($crop && $ratio <= ($width / $height)) {
                 $new_height = $width / $ratio;
                 $new_width = $width;
             } else {
                 $new_width = $height * $ratio;
                 $new_height = $height;
             }
         } else {
             $new_width = $w;
             $new_height = $h;
         }
         $x_mid = $new_width * .5;  //horizontal middle
         $y_mid = $new_height * .5; //vertical middle
         // Resample
         error_log('height: ' . $new_height . ' - width: ' . $new_width);
         $new = imagecreatetruecolor(round($new_width), round($new_height));
         imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $w, $h);
         // Crop
         if ($crop) {
             $crop = imagecreatetruecolor($width ? $width : $new_width, $height ? $height : $new_height);
             imagecopyresampled($crop, $new, 0, 0, ($x_mid - ($width * .5)), 0, $width, $height, $width, $height);
             //($y_mid - ($height * .5))
         }
         // Output
         // Enable interlancing [for progressive JPEG]
         imageinterlace($crop ? $crop : $new, true);
 
         $dext = strtolower(pathinfo($destination, PATHINFO_EXTENSION));
         if ($dext == '') {
             $dext = $ext;
             $destination .= '.' . $ext;
         }
         switch ($dext) {
             case 'jpeg':
             case 'jpg':
                 imagejpeg($crop ? $crop : $new, $destination, $quality);
                 break;
             case 'png':
                 $pngQuality = ($quality - 100) / 11.111111;
                 $pngQuality = round(abs($pngQuality));
                 imagepng($crop ? $crop : $new, $destination, $pngQuality);
                 break;
             case 'gif':
                 imagegif($crop ? $crop : $new, $destination);
                 break;
         }
         @imagedestroy($image);
         @imagedestroy($new);
         @imagedestroy($crop);
     }
 }
?>

相关文章

精彩推荐