Advertisement
Guest User

upload

a guest
Dec 22nd, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.87 KB | None | 0 0
  1. <?php
  2.  
  3.     include '../loginssn.php';
  4.  
  5.     $mimes = array(
  6.             'image/jpeg'    => array('jpg', 'jpe', 'jpeg'),
  7.             'image/pjpeg'   => array('jpg', 'jpe', 'jpeg'),
  8.             'image/png'     => array('png')
  9.         );
  10.  
  11.     function makedir($base, $input)
  12.     {
  13.         $input = substr(sha1($input), 0, 2);
  14.         $path = $base . $input;
  15.  
  16.         if( ! file_exists($path) && ! is_dir($path)) mkdir($path);
  17.  
  18.         return $path . '/';
  19.     }
  20.  
  21.     function checkext($ext, $mimes)
  22.     {
  23.         foreach($mimes as $key => $value)
  24.         {
  25.             if(in_array($ext, $value)) return true;
  26.         }
  27.  
  28.         return false;
  29.     }
  30.  
  31.     function checkmime($mime, $mimes)
  32.     {
  33.         if(in_array($mime, array_keys($mimes))) return true;
  34.         return false;
  35.     }
  36.  
  37.     if($_FILES)
  38.     {
  39.         $max_size   = 2097152;
  40.  
  41.         # max pixels per side
  42.         $max_width  = 20000;
  43.         $max_height = 20000;
  44.  
  45.         $uploaddir  = "./images/";
  46.         $errors     = array();
  47.  
  48.         if( ! array_key_exists('user_profile_image', $_FILES) || $_FILES['user_profile_image']['error'] != 0)
  49.         {
  50.             $errors['upload'] = 'Upload error!';
  51.         }
  52.  
  53.         if(filesize($_FILES['user_profile_image']['tmp_name']) > $max_size)
  54.         {
  55.             $errors['size'] = 'The file size exceeded!';
  56.         }
  57.  
  58.         # first check
  59.         if(count($errors) > 0)
  60.         {
  61.             print_r($errors);
  62.             die('An error occurred!');
  63.         }
  64.  
  65.         $tmpfile = $_FILES['user_profile_image']['tmp_name'];
  66.         $filename = $_FILES['user_profile_image']['name'];
  67.         $ext = strtolower(pathinfo(parse_url($filename, PHP_URL_PATH), PATHINFO_EXTENSION));
  68.  
  69.         list($width,$height,$type) = getimagesize($tmpfile);
  70.         $mime = image_type_to_mime_type($type);
  71.  
  72.         if($width > $max_width)
  73.         {
  74.             $errors['max_width'] = 'The width of the file exceeded!';
  75.         }
  76.  
  77.         if($height > $max_height)
  78.         {
  79.             $errors['max_height'] = 'The height of the file exceeded!';
  80.         }
  81.  
  82.         if(checkext($ext, $mimes) === false)
  83.         {
  84.             $errors['format'] = 'The format is not allowed!';
  85.         }
  86.  
  87.         if(checkmime($mime, $mimes) === false)
  88.         {
  89.             $errors['mime'] = 'The mime is not allowed!';
  90.         }
  91.  
  92.         # second check
  93.         if(count($errors) > 0)
  94.         {
  95.             print_r($errors);
  96.             die('An error occurred!');
  97.         }
  98.        
  99.  
  100.         $path = makedir($uploaddir, $_SESSION['username']);
  101.        
  102.         $newname = sha1($_SESSION['username']).'.png';
  103.         $dest = $path . $newname;
  104.  
  105.         $img = new Imagick();
  106.         $img->readImage($tmpfile);
  107.         $img->stripImage();
  108.         $img->scaleImage(100, 100, true);
  109.         $img->writeImage($dest);
  110.         $img->clear();
  111.         $img->destroy();
  112.  
  113.         echo "<img src='$dest' />";
  114.  
  115.     }
  116.  
  117.  
  118. /**
  119.  
  120.  Usage examples:
  121.  
  122.     # from session:
  123.     $hash = sha1($_SESSION['username']);
  124.     $segment = substr($hash, 0, 2);
  125.     echo "<img src='/images/{$segment}/{$hash}.png' />";
  126.  
  127.     # manually:
  128.     $hash = sha1('cereal');
  129.     $segment = substr($hash, 0, 2);
  130.     echo "<img src='/images/{$segment}/{$hash}.png' />";
  131.  
  132.     # from result set
  133.     while($row = $stmt->fetchAll())
  134.     {
  135.         $hash = sha1($row['username']);
  136.         $segment = substr($hash, 0, 2);
  137.         echo "<img src='/images/{$segment}/{$hash}.png' />";
  138.     }
  139.  
  140. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement