-
-
Save anonymous/e487eadbac82334fea8f to your computer and use it in GitHub Desktop.
php upload script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$file_size_limit = 5242881; | |
$types_allowed = array('image/jpeg', 'image/jpg', 'image/gif', 'image/png'); | |
$location = 'uploads/'; | |
$normal = 200; | |
$small = 50; | |
function resize_image($width_height, $folder_path) { | |
global $location, $rand_filename, $image_path; | |
if (isset($rand_filename, $location)) { | |
$image_path = $location . $rand_filename; | |
if (file_exists($image_path)) { | |
$image_size = getimagesize($image_path); | |
$image_width = $image_size[0]; | |
$image_height = $image_size[1]; | |
if ($old_image = @imagecreatefromjpeg($image_path)) { | |
} elseif ($old_image = @imagecreatefromgif($image_path)) { | |
} else if ($old_image = @imagecreatefrompng($image_path)) { | |
} else { | |
echo 'Image upload error.<br/>'; | |
return false; | |
} | |
$offset = strpos($rand_filename, '.'); | |
$result_image_name = $folder_path . $rand_filename; | |
if ($image_height > $image_width) { | |
$greater_axe = $image_height; | |
$smaller_axe = $image_width; | |
} else { | |
$greater_axe = $image_width; | |
$smaller_axe = $image_height; | |
} | |
$x = ($image_width - $smaller_axe) / 2; | |
$y = ($image_height - $smaller_axe) / 2; | |
$image_croped = imagecreatetruecolor($width_height, $width_height); | |
if (@!imagecopyresampled($image_croped, $old_image, 0, 0, $x, $y, $width_height, $width_height, $smaller_axe, $smaller_axe)) { | |
echo 'Function error.<br/>'; | |
return false; | |
} | |
if (@imagejpeg($image_croped, $result_image_name)) { | |
return true; | |
} else { | |
echo 'Image upload error.<br/>'; | |
return false; | |
} | |
} else { | |
echo 'Picture does not exist.<br/>'; | |
return false; | |
} | |
} else { | |
echo 'Upload error.<br/>'; | |
return false; | |
} | |
} | |
if (!empty($_FILES["pic"])) { | |
foreach ($_FILES["pic"]["error"] as $key => $error) { | |
if ($error == UPLOAD_ERR_OK) { | |
if (isset($_FILES['pic']['name']) && !empty($_FILES['pic']['name'])) { | |
$name = $_FILES['pic']['name']; | |
$tmp_name = $_FILES['pic']['tmp_name']; | |
$file_size = $_FILES['pic']['size']; | |
$type = $_FILES['pic']['type']; | |
$errors = array(); | |
$counter = 0; | |
do { | |
$rand_filename = rand(10000, 99999) . '.jpeg'; | |
$uniq_name = $location . $rand_filename; | |
$location_contents = scandir($location); | |
$counter++; | |
if ($counter > 99999) { | |
echo 'Can not find a unique name.<br/>'; | |
$errors[] = '1'; | |
} | |
} while (in_array($rand_filename, $location_contents)); | |
if ($file_size > $file_size_limit) { | |
echo 'Image is to big - 5Mb<br/>'; | |
echo $file_size.' > '.$file_size_limit; | |
$errors[] = '1'; | |
} | |
if (!in_array($type, $types_allowed)) { | |
echo 'Onely image file'; | |
$errors[] = '1'; | |
} | |
if (empty($errors)) { | |
if (move_uploaded_file($tmp_name, $uniq_name)) { | |
if (resize_image($normal, 'uploads/normal/') && resize_image($small, 'uploads/small/')) { | |
echo 'Viss ok!'; | |
} else { | |
//unlink($image_path); | |
} | |
} else { | |
echo 'Function error, please try later.<br/>'; | |
} | |
} | |
} else { | |
echo 'Please select image'; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment