Advertisement
Guest User

Redimensione e recorte a imagem para o centro com PHP

a guest
Oct 23rd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. <?php
  2.  
  3. if(isset($_GET['src'])){
  4.  
  5. $image = imagecreatefromjpeg($_GET['src']);
  6. $filename = 'anotherimagem.jpg';
  7.  
  8. // Largura e altura desejada do thumbnail
  9. $thumb_width = 200;
  10. $thumb_height = 150;
  11.  
  12. // Largura e altura da imagem em upload
  13. $width = imagesx($image);
  14. $height = imagesy($image);
  15.  
  16.  
  17. // Caluclos Matemáticos
  18. $original_aspect = $width / $height;
  19. $thumb_aspect = $thumb_width / $thumb_height;
  20.  
  21. if ($original_aspect >= $thumb_aspect) {
  22.  
  23. // If image is wider than thumbnail (in aspect ratio sense)
  24. $new_height = $thumb_height;
  25. $new_width = $width / ($height / $thumb_height);
  26.  
  27. } else {
  28.  
  29. // If the thumbnail is wider than the image
  30. $new_width = $thumb_width;
  31. $new_height = $height / ($width / $thumb_width);
  32. }
  33.  
  34. $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
  35.  
  36. // Redimensionando e recortando
  37. imagecopyresampled($thumb,
  38. $image,
  39. 0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
  40. 0 - ($new_height - $thumb_height) / 2, // Center the image vertically
  41. 0, 0, $new_width, $new_height, $width, $height);
  42.  
  43. // Definindo a qualidade da foto
  44. imagejpeg($thumb, $filename, 80);
  45.  
  46. }
  47.  
  48. ?>
  49.  
  50. <!DOCTYPE html>
  51. <html>
  52. <head>
  53. <title>Upload de imagens</title>
  54. </head>
  55. <body>
  56. <form method="GET" enctype="multipart/form-data">
  57. <input type="file" name="src" />
  58. <input type="submit" name="submit" value="Upload" />
  59. </form>
  60. </body>
  61. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement