Guest User

Untitled

a guest
Mar 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. <form action="" method="post" enctype="multipart/form-data">
  2. <input name="img" type="file" />
  3. <input type="submit" name="cadastrar" />
  4. </form>
  5.  
  6.  
  7. <?php
  8. if(isset($_POST['cadastrar'])){
  9. $img = $_FILES['img'];
  10. $name =$img['name'];
  11. $tmp =$img['tmp_name'];
  12. $ext =end(explode('.',$name));
  13.  
  14. $pasta ='NOMEDAPASTA/'; //Pasta onde a imagem será salva
  15.  
  16. $permiti =array('jpg', 'jpeg', 'png');
  17. $name = uniqid().'.'.$ext; $uid = uniqid();
  18.  
  19. $upload = move_uploaded_file($tmp, $pasta.'/'.$name);}; //Faz o upload da imagem para o servidor
  20.  
  21. if($upload){
  22. function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 60){
  23. $imgsize = getimagesize($source_file);
  24. $width = $imgsize[0];
  25. $height = $imgsize[1];
  26. $mime = $imgsize['mime'];
  27. //resize and crop image by center
  28. switch($mime){
  29. case 'image/gif':
  30. $image_create = "imagecreatefromgif";
  31. $image = "imagegif";
  32. break;
  33. //resize and crop image by center
  34. case 'image/png':
  35. $image_create = "imagecreatefrompng";
  36. $image = "imagepng";
  37. $quality = 6;
  38. break;
  39. //resize and crop image by center
  40. case 'image/jpeg':
  41. $image_create = "imagecreatefromjpeg";
  42. $image = "imagejpeg";
  43. $quality = 60;
  44. break;
  45. default:
  46. return false;
  47. break;
  48. }
  49. $dst_img = imagecreatetruecolor($max_width, $max_height);
  50. $src_img = $image_create($source_file);
  51. $width_new = $height * $max_width / $max_height;
  52. $height_new = $width * $max_height / $max_width;
  53. if($width_new > $width){
  54. $h_point = (($height - $height_new) / 2);
  55. imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
  56. }else{
  57. $w_point = (($width - $width_new) / 2);
  58. imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
  59. }
  60. $image($dst_img, $dst_dir, $quality);
  61. if($dst_img)imagedestroy($dst_img);
  62. if($src_img)imagedestroy($src_img);
  63. }
  64.  
  65. //Tamanho da Imagem final
  66. resize_crop_image(300, 300, $pasta.'/'.$name, $pasta.'/'.$name);}
  67.  
  68.  
  69. ?>
Add Comment
Please, Sign In to add comment