Advertisement
WILDAN_IZZUDIN

UPLOAD AND AUTOMATIC RESIZE IMAGE

Mar 14th, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. <?php
  2. /* PHP Script
  3. Upload Automatic Resize (Photo)
  4. Created By Wildan Izzudin.
  5.     (c) 2018 Underxploit
  6. */
  7. error_reporting(0);
  8. // --- Function Resize Image --- //
  9. function resize($resource, $width, $height) {
  10.          $new_width = 450; // New Width (px)
  11.       $new_height = 450; // New Height (px)
  12.          $new_layer = imagecreatetruecolor($new_width, $new_height); // New Layer
  13.    
  14.         imagecopyresampled($new_layer, $resource,0,0,0,0, $new_width, $new_height, $width, $height);
  15.    
  16.         return $new_layer;
  17. }
  18. // --- Function Random A-za-Z0-9 --- //
  19. function acakadut($jumlah) {  
  20.     $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz';  
  21.     $strs = '';  
  22. for($i = 0; $i < $jumlah; $i++) {  
  23.       $pos = rand(0, strlen($str)-1);  
  24.       $strs .= $str{$pos};  
  25. }  
  26.   return $strs;
  27. }  
  28.  
  29. if(isset($_POST["upload"])) {
  30.     if(is_array($_FILES)) {
  31.         $gambar = $_FILES['image']['tmp_name'];
  32.         $getimage = getimagesize($gambar);
  33.         $path = "photo/"; // Set Directory
  34.         $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
  35.         $type = $getimage[2];
  36.  
  37.  switch($type) {
  38.  
  39.  // --- Gambar PNG --- //
  40.         case IMAGETYPE_PNG:
  41.                 $resource = imagecreatefrompng($gambar);
  42.                 $new_layer = resize($resource,$getimage[0],$getimage[1]);
  43.                 imagepng($new_layer,$path.acakadut(9).'.'.$ext);
  44.                 break;
  45.                
  46.  // --- Gambar GIF --- //
  47.         case IMAGETYPE_GIF:
  48.                 $resource = imagecreatefromgif($gambar);
  49.                 $new_layer = resize($resource,$getimage[0],$getimage[1]);
  50.                 imagegif($new_layer,$path.acakadut(9).'.'.$ext);
  51.                 break;
  52.  
  53. // --- Gambar JPEG / JPG --- //
  54.         case IMAGETYPE_JPEG:
  55.                 $resource = imagecreatefromjpeg($gambar);
  56.                 $new_layer = resize($resource,$getimage[0],$getimage[1]);
  57.                 imagejpeg($new_layer,$path.acakadut(9).'.'.$ext);
  58.                 break;
  59.  
  60. // --- If Invalid Type (Ekstension) --- //
  61.             default:
  62.                     echo "Yang Lu Upload Bukan Gambar Njeng -_-";
  63.              exit;
  64.              break;
  65.     }
  66.  }
  67.  
  68. if(@copy($path.acakadut(9).'.'.$ext)) {
  69.             echo 'Gagal Njeng';
  70.         } else {
  71.             $photo = acakadut(9).'.'.$ext;
  72.             echo('File Ter-Upload -> '.$photo);
  73.       }  
  74. }
  75. echo('<!DOCTYPE html>
  76. <html>
  77. <head>
  78. <title>Upload Resize</title>
  79. </head>
  80. <body>
  81.  
  82. <div class="container">
  83. <form action="" method="post" enctype="multipart/form-data">
  84. <input type="file" name="image" />
  85. <input type="submit" name="upload" value="upload" />
  86. </form>
  87. </div>
  88. </body>
  89. </html>');
  90. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement