Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include '../loginssn.php';
- $mimes = array(
- 'image/jpeg' => array('jpg', 'jpe', 'jpeg'),
- 'image/pjpeg' => array('jpg', 'jpe', 'jpeg'),
- 'image/png' => array('png')
- );
- function makedir($base, $input)
- {
- $input = substr(sha1($input), 0, 2);
- $path = $base . $input;
- if( ! file_exists($path) && ! is_dir($path)) mkdir($path);
- return $path . '/';
- }
- function checkext($ext, $mimes)
- {
- foreach($mimes as $key => $value)
- {
- if(in_array($ext, $value)) return true;
- }
- return false;
- }
- function checkmime($mime, $mimes)
- {
- if(in_array($mime, array_keys($mimes))) return true;
- return false;
- }
- if($_FILES)
- {
- $max_size = 2097152;
- # max pixels per side
- $max_width = 20000;
- $max_height = 20000;
- $uploaddir = "./images/";
- $errors = array();
- if( ! array_key_exists('user_profile_image', $_FILES) || $_FILES['user_profile_image']['error'] != 0)
- {
- $errors['upload'] = 'Upload error!';
- }
- if(filesize($_FILES['user_profile_image']['tmp_name']) > $max_size)
- {
- $errors['size'] = 'The file size exceeded!';
- }
- # first check
- if(count($errors) > 0)
- {
- print_r($errors);
- die('An error occurred!');
- }
- $tmpfile = $_FILES['user_profile_image']['tmp_name'];
- $filename = $_FILES['user_profile_image']['name'];
- $ext = strtolower(pathinfo(parse_url($filename, PHP_URL_PATH), PATHINFO_EXTENSION));
- list($width,$height,$type) = getimagesize($tmpfile);
- $mime = image_type_to_mime_type($type);
- if($width > $max_width)
- {
- $errors['max_width'] = 'The width of the file exceeded!';
- }
- if($height > $max_height)
- {
- $errors['max_height'] = 'The height of the file exceeded!';
- }
- if(checkext($ext, $mimes) === false)
- {
- $errors['format'] = 'The format is not allowed!';
- }
- if(checkmime($mime, $mimes) === false)
- {
- $errors['mime'] = 'The mime is not allowed!';
- }
- # second check
- if(count($errors) > 0)
- {
- print_r($errors);
- die('An error occurred!');
- }
- $path = makedir($uploaddir, $_SESSION['username']);
- $newname = sha1($_SESSION['username']).'.png';
- $dest = $path . $newname;
- $img = new Imagick();
- $img->readImage($tmpfile);
- $img->stripImage();
- $img->scaleImage(100, 100, true);
- $img->writeImage($dest);
- $img->clear();
- $img->destroy();
- echo "<img src='$dest' />";
- }
- /**
- Usage examples:
- # from session:
- $hash = sha1($_SESSION['username']);
- $segment = substr($hash, 0, 2);
- echo "<img src='/images/{$segment}/{$hash}.png' />";
- # manually:
- $hash = sha1('cereal');
- $segment = substr($hash, 0, 2);
- echo "<img src='/images/{$segment}/{$hash}.png' />";
- # from result set
- while($row = $stmt->fetchAll())
- {
- $hash = sha1($row['username']);
- $segment = substr($hash, 0, 2);
- echo "<img src='/images/{$segment}/{$hash}.png' />";
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement