Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Image{
- public static function create_thumbnail($path, $save, $width, $height) {
- $info = getimagesize($path);
- $size = array($info[0], $info[1]);
- switch ($info["mime"]) {
- case 'image/png':
- $src = imagecreatefrompng($path);
- break;
- case 'image/jpeg':
- $src = imagecreatefromjpeg($path);
- break;
- case 'image/gif':
- $src = imagecreatefromgif($path);
- break;
- default:
- return false;
- break;
- }
- $thumb = imagecreatetruecolor($width, $height);
- $src_aspect = $size[0] / $size[1];
- $thumb_aspect = $width / $height;
- if ($src_aspect < $thumb_aspect){
- //narrower
- $scale = $width / $size[0];
- $new_size = array($width, $width / $src_aspect);
- $src_pos = array(0, ($size[1] * $scale - $height) / $scale / 2);
- }else if ($src_aspect > $thumb_aspect){
- // wider
- $scale = $height / $size[1];
- $new_size = array($height * $src_aspect, $height);
- $src_pos = array(($size[0] * $scale - $width) / $scale / 2, 0);
- }else{
- //same shape
- $new_size = array($width, $height);
- $src_pos = array(0, 0);
- }
- $new_size[0] = max($new_size[0], 1);
- $new_size[1] = max($new_size[1], 1);
- imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $size[0], $size[1]);
- if ($save ===false){//debug mode
- switch ($info["mime"]) {
- case 'image/png':
- return imagepng($thumb);
- break;
- case 'image/jpeg':
- return imagejpeg($thumb);
- break;
- case 'image/gif':
- return imagegif($thumb);
- break;
- }
- }else{//save actual images
- switch ($info["mime"]) {
- case 'image/png':
- return imagepng($thumb, $save);
- break;
- case 'image/jpeg':
- return imagejpeg($thumb, $save);
- break;
- case 'image/gif':
- return imagegif($thumb, $save);
- break;
- }
- }
- //kevin neven 09-06-2016
- }
- public static function upload($image, $path, $thumbnail = FALSE) {
- return fileManagement::upload($image, 5000, $path, array("png", "jpeg", "jpg", "gif"));
- }
- }
- <?php
- class fileManagement {
- public static function upload($a_file, $maxKB, $path, $allowedExt) {
- $path = rtrim($path,"/");
- $path .= "/";
- $file = $a_file;
- //file properties
- $fileName = $file["name"];
- $fileTmp = $file["tmp_name"];
- $fileSize = $file["size"];
- $fileError = $file["error"];
- //get the file extension
- $fileExt = explode(".", $fileName);
- $fileExt = strtolower(end($fileExt));
- if(in_array($fileExt, $allowedExt)){
- if($fileError === 0){
- if($fileSize <= $maxKB * 1024){ //check if the filesize is smaller or equal to the max size
- $fileNameAndPath = self::generateRandomFileName($fileExt,$path);
- $errorAmount = 0;
- while (file_exists($fileNameAndPath)){ //
- $fileNameAndPath = self::generateRandomFileName($fileExt,$path);
- $errorx++;
- if ($errorAmount == 5) {
- return "error";
- }
- }
- if(move_uploaded_file($fileTmp, $fileNameAndPath)){
- return $fileNameAndPath;
- }
- }
- }
- }
- }
- private static function generateRandomFileName($fileExtion, $path) {
- $filename = uniqid("",TRUE) . "." . $fileExtion; // make the file name... uniqid() returns a random string. so you would get 32y243hqtkglrhjlag.jpg
- return $path . $filename;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement