Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. <?php
  2. function resize_image($file, $max_resolution){
  3.  
  4. if(file_exists($file)){
  5. $original_image = imagecreatefromjpeg($file);
  6.  
  7. //resolution
  8. $original_width = imagesx($original_image);
  9. $original_height = imagesy($original_image);
  10.  
  11. //try width first
  12. $ratio = $max_resolution / $original_width;
  13. $new_width = $max_resolution;
  14. $new_height = $original_height * $ratio;
  15.  
  16. if($new_height > $max_resolution) {
  17. $ratio = $max_resolution / $original_height;
  18. $new_height = $max_resolution;
  19. $new_width = $orignial_width * ratio;
  20. }
  21. if($original_image){
  22. $new_image = imagecreatetruecolor($new_width, $new_height);
  23. imagecopyresampled($new_image,$original_image,0,0,0,0,$new_width,$new_height,$original_width,$original_height);
  24. imagejpeg($new_image, $file, 90);
  25.  
  26.  
  27. }
  28.  
  29. }
  30.  
  31. }
  32. if(isset($_FILES['image']) && $_FILES['image']['type'] == 'image/jpeg'){
  33.  
  34. move_uploaded_file($_FILES['image']['tmp_name'],$_FILES['image']['name']);
  35. $file = $_FILES['image']['name'];
  36. resize_image($file, "500");
  37.  
  38. echo "<img src='$file'/>";
  39. }
  40. ?>
  41.  
  42. <form method="post" enctype='multipart/form-data'>
  43. <input type="file" name="image"><br/>
  44. <input type="submit" value="post">
  45. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement