erik_keresztes

Image/video upload processing example

Mar 17th, 2020
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.66 KB | None | 0 0
  1. <?php
  2.  
  3. //       ___                               __      _     _                          _                 _
  4. //      |_ _|_ __ ___   __ _  __ _  ___   / /_   _(_) __| | ___  ___    _   _ _ __ | | ___   __ _  __| |
  5. //       | || '_ ` _ \ / _` |/ _` |/ _ \ / /\ \ / / |/ _` |/ _ \/ _ \  | | | | '_ \| |/ _ \ / _` |/ _` |
  6. //       | || | | | | | (_| | (_| |  __// /  \ V /| | (_| |  __/ (_) | | |_| | |_) | | (_) | (_| | (_| |
  7. //      |___|_| |_| |_|\__,_|\__, |\___/_/ _  \_/ |_|\__,_|\___|\___/   \__,_| .__/|_|\___/ \__,_|\__,_|
  8. //       _ __  _ __ ___   ___|___/ ___ ___(_)_ __   __ _    _____  ____ _ _ _|_|__  _ __ | | ___        
  9. //      | '_ \| '__/ _ \ / __/ _ \/ __/ __| | '_ \ / _` |  / _ \ \/ / _` | '_ ` _ \| '_ \| |/ _ \      
  10. //      | |_) | | | (_) | (_|  __/\__ \__ \ | | | | (_| | |  __/>  < (_| | | | | | | |_) | |  __/      
  11. //      | .__/|_|  \___/ \___\___||___/___/_|_| |_|\__, |  \___/_/\_\__,_|_| |_| |_| .__/|_|\___|      
  12. //      |_|                                        |___/                           |_|                  
  13. //                  by Erik Keresztes (fiverr.com/erik_keresztes)
  14.  
  15. $maxSize = 5000; // max file size in kilobits
  16. $maxUncompressedSize = 1000; // compression treshold
  17.  
  18. // define allowed file types
  19. $allowedImageMIMETypes = array('image/bmp', 'image/gif', 'image/jpeg', 'image/png', 'image/webp');
  20. $allowedVideoMIMETypes = array('video/mp4', 'video/x-msvideo', 'video/x-ms-wmv', 'video/webm', 'video/mpeg', 'video/3gpp', 'video/3gpp2');
  21.  
  22. // get file extension
  23. $extension = pathinfo($_FILES['uploadedfile']['name'], PATHINFO_EXTENSION);
  24.  
  25. // get file size in kilobits
  26. $size = $_FILES["uploadedfile"]["size"] / 1000;
  27.  
  28. // if file smaller than the max size
  29. if($size < $maxSize)
  30. {
  31.     // if file type is allowed image type
  32.     if(in_array($_FILES["uploadedfile"]["type"], $allowedImageMIMETypes))
  33.     {
  34.         // get dimensions of image
  35.         $imgsize = getimagesize($_FILES["uploadedfile"]["tmp_name"]);
  36.         $dimensions = $imgsize[0] . 'x' . $imgsize[1];
  37.  
  38.         // get filename of uploaded file
  39.         $originalFileName = pathinfo($_FILES['uploadedfile']['name'], PATHINFO_FILENAME);
  40.        
  41.         // generate random 40-char filename
  42.         $fileName = implode('', explode('.', substr(uniqid(rand(), true), 0, 40)));
  43.  
  44.         // get user image folder
  45.         $imageFolder = dirname(__DIR__) . '/img/user';
  46.        
  47.         // if image is bigger than compression treshold
  48.         if($size > $maxUncompressedSize)
  49.         {
  50.             $compressedFilename = $fileName . '.jpg';
  51.  
  52.             // compress file with imagemagick
  53.             $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");
  54.             if(!empty($imagickOutput))
  55.             {
  56.                 redirect('../error?m=imgerror');
  57.                 die();
  58.             }
  59.         }
  60.  
  61.         // move uncompressed image to user folder
  62.         move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $imageFolder . '/' . $fileName . '.' . $extension);
  63.  
  64.         // add file info to database
  65.         mysqli_query($conn, "INSERT INTO files (filename, compressedFilename, originalFilename, fileSize, dimensions) VALUES ('{$fileName}.{$extension}', '{$compressedFilename}', '{$_FILES['uploadedfile']['name']}', {$size}, '{$dimensions}')");
  66.  
  67.         // get file ID from database
  68.         $fileID = end(mysqli_fetch_array(mysqli_query($conn, "SELECT id FROM files WHERE filename = '{$fileName}.{$extension}'")));
  69.     }
  70.     // if file type is allowed video type
  71.     elseif(in_array($_FILES["uploadedfile"]["type"], $allowedVideoMIMETypes))
  72.     {
  73.         // process video
  74.         include('processvideo.php')
  75.     }
  76.     else
  77.     {
  78.         redirect('../error?m=invalidfile');
  79.     }
  80. }
  81. else
  82. {
  83.     redirect('../error?m=filetoobig');
  84. }
  85.  
  86. ?>
Add Comment
Please, Sign In to add comment