Advertisement
adolf1994

ajaxupload.php

May 28th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.13 KB | None | 0 0
  1. <?php
  2.     function uploadImage($fileName, $maxSize, $maxW, $fullPath, $relPath, $colorR, $colorG, $colorB, $maxH = null){
  3.         $folder = $relPath;
  4.         $maxlimit = $maxSize;
  5.         $allowed_ext = "jpg,gif,png,bmp";
  6.         $match = "";
  7.         $filesize = $_FILES[$fileName]['size'];
  8.         if($filesize > 0){ 
  9.             $filename = strtolower($_FILES[$fileName]['name']);
  10.             $filename = preg_replace('/\s/', '_', $filename);
  11.             if($filesize < 1){
  12.                 $errorList[] = "File size is empty.";
  13.             }
  14.             if($filesize > $maxlimit){
  15.                 $errorList[] = "File size is too big.";
  16.             }
  17.             if(count($errorList)<1){
  18.                 $file_ext = preg_split("/\./",$filename);
  19.                 $allowed_ext = preg_split("/\,/",$allowed_ext);
  20.                 foreach($allowed_ext as $ext){
  21.                     if($ext==end($file_ext)){
  22.                         $match = "1"; // File is allowed
  23.                         $NUM = time();
  24.                         $front_name = substr($file_ext[0], 0, 15);
  25.                         $newfilename = $front_name."_".$NUM.".".end($file_ext);
  26.                         $filetype = end($file_ext);
  27.                         $save = $folder.$newfilename;
  28.                         if(!file_exists($save)){
  29.                             list($width_orig, $height_orig) = getimagesize($_FILES[$fileName]['tmp_name']);
  30.                             if($maxH == null){
  31.                                 if($width_orig < $maxW){
  32.                                     $fwidth = $width_orig;
  33.                                 }else{
  34.                                     $fwidth = $maxW;
  35.                                 }
  36.                                 $ratio_orig = $width_orig/$height_orig;
  37.                                 $fheight = $fwidth/$ratio_orig;
  38.                                
  39.                                 $blank_height = $fheight;
  40.                                 $top_offset = 0;
  41.                                    
  42.                             }else{
  43.                                 if($width_orig <= $maxW && $height_orig <= $maxH){
  44.                                     $fheight = $height_orig;
  45.                                     $fwidth = $width_orig;
  46.                                 }else{
  47.                                         if($width_orig > $maxW){
  48.                                             $ratio = ($width_orig / $maxW);
  49.                                             $fwidth = $maxW;
  50.                                             $fheight = ($height_orig / $ratio);
  51.                                             if($fheight > $maxH){
  52.                                                 $ratio = ($fheight / $maxH);
  53.                                                 $fheight = $maxH;
  54.                                                 $fwidth = ($fwidth / $ratio);
  55.                                             }
  56.                                         }
  57.                                         if($height_orig > $maxH){
  58.                                             $ratio = ($height_orig / $maxH);
  59.                                             $fheight = $maxH;
  60.                                             $fwidth = ($width_orig / $ratio);
  61.                                             if($fwidth > $maxW){
  62.                                                 $ratio = ($fwidth / $maxW);
  63.                                                 $fwidth = $maxW;
  64.                                                 $fheight = ($fheight / $ratio);
  65.                                             }
  66.                                         }
  67.                                 }
  68.                                 if($fheight == 0 || $fwidth == 0 || $height_orig == 0 || $width_orig == 0){
  69.                                         die("FATAL ERROR REPORT ERROR CODE [add-pic-line-67-orig] to <a href='http://www.atwebresults.com'>AT WEB RESULTS</a>");
  70.                                 }
  71.                                 if($fheight < 45){
  72.                                     $blank_height = 45;
  73.                                     $top_offset = round(($blank_height - $fheight)/2);
  74.                                 }else{
  75.                                     $blank_height = $fheight;
  76.                                 }
  77.                             }
  78.                             if($height_orig > $maxH OR $width_orig > $maxW){
  79.                                 switch($filetype){
  80.                                     case "gif":
  81.                                         $image = @imagecreatefromgif($_FILES[$fileName]['tmp_name']);
  82.                                     break;
  83.                                     case "jpg":
  84.                                         $image = @imagecreatefromjpeg($_FILES[$fileName]['tmp_name']);
  85.                                     break;
  86.                                     case "jpeg":
  87.                                         $image = @imagecreatefromjpeg($_FILES[$fileName]['tmp_name']);
  88.                                     break;
  89.                                     case "png":
  90.                                         $image = @imagecreatefrompng($_FILES[$fileName]['tmp_name']);
  91.                                     break;
  92.                                 }
  93.                                 $image_p = imagecreatetruecolor($fwidth, $blank_height);
  94.                                 $white = imagecolorallocate($image_p, $colorR, $colorG, $colorB);
  95.                                 imagefill($image_p, 0, 0, $white);
  96.                                 @imagecopyresampled($image_p, $image, 0, $top_offset, 0, 0, $fwidth, $fheight, $width_orig, $height_orig);
  97.                                 switch($filetype){
  98.                                     case "gif":
  99.                                         if(!@imagegif($image_p, $save)){
  100.                                             $errorList[]= "PERMISSION DENIED [GIF]";
  101.                                         }
  102.                                     break;
  103.                                     case "jpg":
  104.                                         if(!@imagejpeg($image_p, $save, 100)){
  105.                                             $errorList[]= "PERMISSION DENIED [JPG]";
  106.                                         }
  107.                                     break;
  108.                                     case "jpeg":
  109.                                         if(!@imagejpeg($image_p, $save, 100)){
  110.                                             $errorList[]= "PERMISSION DENIED [JPEG]";
  111.                                         }
  112.                                     break;
  113.                                     case "png":
  114.                                         if(!@imagepng($image_p, $save, 0)){
  115.                                             $errorList[]= "PERMISSION DENIED [PNG]";
  116.                                         }
  117.                                     break;
  118.                                 }
  119.                             }else{
  120.                                 move_uploaded_file($_FILES[$fileName]['tmp_name'], $save);
  121.                             }
  122.                             @imagedestroy($filename);
  123.                         }else{
  124.                             $errorList[]= "CANNOT MAKE IMAGE IT ALREADY EXISTS";
  125.                         }  
  126.                     }
  127.                 }      
  128.             }
  129.         }else{
  130.             $errorList[]= "NO FILE SELECTED";
  131.         }
  132.         if(!$match){
  133.             $errorList[]= "File type isn't allowed: $filename";
  134.         }
  135.         if(sizeof($errorList) == 0){
  136.             return $fullPath.$newfilename;
  137.         }else{
  138.             $eMessage = array();
  139.             for ($x=0; $x<sizeof($errorList); $x++){
  140.                 $eMessage[] = $errorList[$x];
  141.             }
  142.             return $eMessage;
  143.         }
  144.     }
  145.    
  146.     $filename = strip_tags($_REQUEST['filename']);
  147.     $maxSize = strip_tags($_REQUEST['maxSize']);
  148.     $maxW = strip_tags($_REQUEST['maxW']);
  149.     $fullPath = strip_tags($_REQUEST['fullPath']);
  150.     $relPath = strip_tags($_REQUEST['relPath']);
  151.     $colorR = 255;
  152.     $colorG = 255;
  153.     $colorB = 255;
  154.     $maxH = strip_tags($_REQUEST['maxH']);
  155.     $filesize_image = $_FILES[$filename]['size'];
  156.     if($filesize_image > 0){
  157.         $upload_image = uploadImage($filename, $maxSize, $maxW, $fullPath, $relPath, $colorR, $colorG, $colorB, $maxH);
  158.         if(is_array($upload_image)){
  159.             foreach($upload_image as $key => $value) {
  160.                 if($value == "-ERROR-") {
  161.                     unset($upload_image[$key]);
  162.                 }
  163.             }
  164.             $document = array_values($upload_image);
  165.             for ($x=0; $x<sizeof($document); $x++){
  166.                 $errorList[] = $document[$x];
  167.             }
  168.             $imgUploaded = false;
  169.         }else{
  170.             $imgUploaded = true;
  171.         }
  172.     }else{
  173.         $imgUploaded = false;
  174.         $errorList[] = "File Size Empty";
  175.     }
  176. ?>
  177. <?php
  178.     if($imgUploaded){
  179.         echo '<img src="/php_ajax_image_upload/images/success.gif" width="16" height="16" border="0" style="marin-bottom: -4px;" /> Success!<br /><img src="'.$upload_image.'" style="max-width: 200px; maxheight: 300px;" border="0" />';
  180.     }else{
  181.         echo '<img src="/php_ajax_image_upload/images/error.gif" width="16px" height="16px" border="0" style="marin-bottom: -3px;" /> Error(s) Found: ';
  182.         foreach($errorList as $value){
  183.                 echo $value.', ';
  184.         }
  185.         echo '<br><br>Contact Adolf1994 with problems';
  186.     }
  187. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement