Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // ___ __ _ _ _ _
- // |_ _|_ __ ___ __ _ __ _ ___ / /_ _(_) __| | ___ ___ _ _ _ __ | | ___ __ _ __| |
- // | || '_ ` _ \ / _` |/ _` |/ _ \ / /\ \ / / |/ _` |/ _ \/ _ \ | | | | '_ \| |/ _ \ / _` |/ _` |
- // | || | | | | | (_| | (_| | __// / \ V /| | (_| | __/ (_) | | |_| | |_) | | (_) | (_| | (_| |
- // |___|_| |_| |_|\__,_|\__, |\___/_/ _ \_/ |_|\__,_|\___|\___/ \__,_| .__/|_|\___/ \__,_|\__,_|
- // _ __ _ __ ___ ___|___/ ___ ___(_)_ __ __ _ _____ ____ _ _ _|_|__ _ __ | | ___
- // | '_ \| '__/ _ \ / __/ _ \/ __/ __| | '_ \ / _` | / _ \ \/ / _` | '_ ` _ \| '_ \| |/ _ \
- // | |_) | | | (_) | (_| __/\__ \__ \ | | | | (_| | | __/> < (_| | | | | | | |_) | | __/
- // | .__/|_| \___/ \___\___||___/___/_|_| |_|\__, | \___/_/\_\__,_|_| |_| |_| .__/|_|\___|
- // |_| |___/ |_|
- // by Erik Keresztes (fiverr.com/erik_keresztes)
- $maxSize = 5000; // max file size in kilobits
- $maxUncompressedSize = 1000; // compression treshold
- // define allowed file types
- $allowedImageMIMETypes = array('image/bmp', 'image/gif', 'image/jpeg', 'image/png', 'image/webp');
- $allowedVideoMIMETypes = array('video/mp4', 'video/x-msvideo', 'video/x-ms-wmv', 'video/webm', 'video/mpeg', 'video/3gpp', 'video/3gpp2');
- // get file extension
- $extension = pathinfo($_FILES['uploadedfile']['name'], PATHINFO_EXTENSION);
- // get file size in kilobits
- $size = $_FILES["uploadedfile"]["size"] / 1000;
- // if file smaller than the max size
- if($size < $maxSize)
- {
- // if file type is allowed image type
- if(in_array($_FILES["uploadedfile"]["type"], $allowedImageMIMETypes))
- {
- // get dimensions of image
- $imgsize = getimagesize($_FILES["uploadedfile"]["tmp_name"]);
- $dimensions = $imgsize[0] . 'x' . $imgsize[1];
- // get filename of uploaded file
- $originalFileName = pathinfo($_FILES['uploadedfile']['name'], PATHINFO_FILENAME);
- // generate random 40-char filename
- $fileName = implode('', explode('.', substr(uniqid(rand(), true), 0, 40)));
- // get user image folder
- $imageFolder = dirname(__DIR__) . '/img/user';
- // if image is bigger than compression treshold
- if($size > $maxUncompressedSize)
- {
- $compressedFilename = $fileName . '.jpg';
- // compress file with imagemagick
- $imagickOutput = shell_exec("{$imagickConvertPath} '{$_FILES["uploadedfile"]["tmp_name"]}[0]' -define jpeg:extent=300kb -strip -interlace Plane -gaussian-blur 0.05 -resize 200x200 {$imageFolder}/compressed/{$compressedFilename} 2>&1");
- if(!empty($imagickOutput))
- {
- redirect('../error?m=imgerror');
- die();
- }
- }
- // move uncompressed image to user folder
- move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $imageFolder . '/' . $fileName . '.' . $extension);
- // add file info to database
- mysqli_query($conn, "INSERT INTO files (filename, compressedFilename, originalFilename, fileSize, dimensions) VALUES ('{$fileName}.{$extension}', '{$compressedFilename}', '{$_FILES['uploadedfile']['name']}', {$size}, '{$dimensions}')");
- // get file ID from database
- $fileID = end(mysqli_fetch_array(mysqli_query($conn, "SELECT id FROM files WHERE filename = '{$fileName}.{$extension}'")));
- }
- // if file type is allowed video type
- elseif(in_array($_FILES["uploadedfile"]["type"], $allowedVideoMIMETypes))
- {
- // process video
- include('processvideo.php')
- }
- else
- {
- redirect('../error?m=invalidfile');
- }
- }
- else
- {
- redirect('../error?m=filetoobig');
- }
- ?>
Add Comment
Please, Sign In to add comment