php 上傳檔案並產生縮圖代碼

來源:互聯網
上載者:User

if( isset($_FILES['upImg']) )
{
 if( $userGroup[$loginArr['group']]['upload'] == 0 )
 {
  echo '{"error":"您所在的使用者組無權上傳圖片!"}';
 }
 else
 {
  $savePath = "attachment/img/".date('Y/m/d/H');

  mkDirs($savePath);

  $fileType = strtolower(strrchr($_FILES['upImg']['name'],"."));

  if ( !in_array($fileType, array(".jpg",".jpeg",".gif",".png")) )
  {
   echo '{"error":"目前僅支援格式為jpg、jpeg、gif、png的圖片!"}';
  }
  elseif( $_FILES['upImg']['size'] > 204800 )
  {
   echo '{"error":"圖片不能超過200K!"}';
  }
  else
  {
   $saveImg = $savePath."/".$loginArr['uid']."_".time().rand().$fileType;

   if( @move_uploaded_file($_FILES['upImg']['tmp_name'], $saveImg) )
   {
    echo '{"error":"","msg":"http://'.$site_domain.$site_catalog.$saveImg.'"}';
   }
   else
   {
    echo '{"error":"圖片上傳失敗!"}';
   }
  }
 }
}

if( $loginArr['state'] == 0 )
{
 echo '{"error":"您還沒有登入!"}';
}
else
{
 $avatarPath = "attachment/avatar/".($loginArr['uid']%32)."/".($loginArr['uid']%257)."/".$loginArr['uid'];

 if( isset($_FILES['upAvatar']) )
 {
  mkDirs($avatarPath);

  $fileType = strtolower(strrchr($_FILES['upAvatar']['name'],"."));

  if ( !in_array($fileType, array(".jpg",".jpeg",".gif",".png")) )
  {
   echo '{"error":"目前僅支援格式為jpg、jpeg、gif、png的圖片!"}';
  }
  elseif( $_FILES['upAvatar']['size'] > 2097152 )
  {
   echo '{"error":"圖片不能超過2MB!"}';
  }
  else
  {
   $imgInfo = @getimagesize($_FILES['upAvatar']['tmp_name']);

   if( !$imgInfo || !in_array($imgInfo[2], array(1,2,3)) )
   {
    echo '{"error":"系統無法識別您上傳的檔案!"}';
   }
   else
   {
    $TmpAvatar = $avatarPath."/temp".$fileType;
    
    if( @move_uploaded_file($_FILES['upAvatar']['tmp_name'], $TmpAvatar) )
    {
     $maxWidth = 520;

     $maxHeight = 520;

     if( $maxWidth > $imgInfo[0] || $maxHeight > $imgInfo[1] )
     {
      $maxWidth = $imgInfo[0];

      $maxHeight = $imgInfo[1];
     }
     else
     {
      if ( $imgInfo[0] < $imgInfo[1] )
       $maxWidth = ($maxHeight / $imgInfo[1]) * $imgInfo[0];
      else
       $maxHeight = ($maxWidth / $imgInfo[0]) * $imgInfo[1];
     }

     if( $maxWidth < 40 )
     {
      $maxWidth = 40;
     }

     if( $maxHeight < 40 )
     {
      $maxHeight = 40;
     }

     $image_p = imagecreatetruecolor($maxWidth, $maxHeight);

     switch($imgInfo[2])
     {
      case 1:
       $image = imagecreatefromgif($TmpAvatar);
       break;
      case 2:
       $image = imagecreatefromjpeg($TmpAvatar);
       break;
      case 3:
       $image = imagecreatefrompng($TmpAvatar);
       break;
     }

     imagecopyresampled($image_p, $image, 0, 0, 0, 0, $maxWidth, $maxHeight, $imgInfo[0], $imgInfo[1]);

     imagejpeg($image_p, $avatarPath."/temp.jpg",100);

     imagedestroy($image_p);

     imagedestroy($image);

     if( $fileType != ".jpg" && file_exists($TmpAvatar) )
     {
      unlink($TmpAvatar);
     }

     echo '{"error":"","url":"'.$avatarPath.'/temp.jpg?'.rand().'","width":"'.$maxWidth.'","height":"'.$maxHeight.'"}';
    }
    else
    {
     echo '{"error":"圖片上傳失敗!"}';
    }
   }
  }
 }

 if( isset($_POST['x'],$_POST['y'],$_POST['w'],$_POST['h']) )
 {
  if( is_numeric($_POST['x']) && is_numeric($_POST['y']) && $_POST['w'] > 0 && $_POST['h'] > 0 )
  {
   $image_p = imagecreatetruecolor(40, 40);

   $image = imagecreatefromjpeg($avatarPath."/temp.jpg");

   imagecopyresampled($image_p, $image, 0, 0, $_POST['x'], $_POST['y'], 40, 40, $_POST['w'], $_POST['h']);

   imagejpeg($image_p, $avatarPath."/40_40.jpg",100);

   imagedestroy($image_p);

   imagedestroy($image);

   unlink($avatarPath."/temp.jpg");

   echo "1";
  }
 }

 if( isset($_POST['avatar']) && $_POST['avatar'] == "delete" )
 {
  if( file_exists($avatarPath."/temp.jpg") )
  {
   unlink($avatarPath."/temp.jpg");
  }
 }
}

ob_end_flush();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.