Guest User

Untitled

a guest
Feb 15th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. <?php
  2. spl_autoload_register(function ($class_name) {
  3. include_once dirname(__FILE__) . '/entidades/' . $class_name . '.php';
  4. });
  5. session_start();
  6.  
  7. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  8. if(!isset($_SESSION["nr"])){
  9. $_SESSION["nr"] = 0;
  10. }
  11. $nr = $_SESSION["nr"];
  12.  
  13. //pasta onde colocar os ficheiros carregados:
  14. $target_dir = "album/";
  15.  
  16. //Verifica se é imagem (usando a função getimagesize():
  17. //Se não fôr imagem a função retorna FALSE:
  18. $image = getimagesize($_FILES["upload"]["tmp_name"]);
  19.  
  20. if(!$image){
  21. echo "<p style='color: #c00'>Não é uma imagem !</p>";
  22. }else{
  23. if($image[2] == IMAGETYPE_JPEG || $image[2] == IMAGETYPE_PNG || $image[2] == IMAGETYPE_GIF){
  24. //obtem o nome do ficheiro carregado de $_FILES["upload"]["name"]:
  25. $target_file = $target_dir .$nr.".".basename($_FILES["upload"]["type"]);
  26.  
  27. //move ficheiro da pasta temporaria para a pasta "uploads":
  28. move_uploaded_file($_FILES["upload"]["tmp_name"], $target_file);
  29.  
  30. $imageName = $nr.".".basename($_FILES["upload"]["type"]);
  31. thumbnail($imageName);
  32. echo "<p style='color: #0c0'>Imagem carregada !</p>";
  33. }else{
  34. echo "<p style='color: #c00'>Imagem deve ser do tipo .jpg, jpeg, .png ou .gif !</p>";
  35. }
  36. }
  37. $nr += 1;
  38. $_SESSION["nr"]=$nr;
  39.  
  40. }
  41.  
  42.  
  43. //
  44. // Função thumbnail() que cria um thumbnail de uma imagem JPG, PNG ou GIF, com as dimensões 80x80.
  45. //
  46. function thumbnail($imagename){
  47. $imagepath = 'album/' . $imagename;
  48.  
  49.  
  50. //função getimagesize() : retorna um array com a largura, a altura e o tipo da imagem
  51. $props = getimagesize($imagepath);
  52. $width = $props[0];
  53. $height = $props[1];
  54. $imageType = $props[2];
  55.  
  56. $ext = strtolower(pathinfo($imagepath,PATHINFO_EXTENSION));
  57.  
  58. switch ($imageType) {
  59. case IMAGETYPE_PNG:
  60. //função imagecreatefrompng(): cria o recurso de uma imagem a partir de uma imagem png
  61. $imageResourceId = imagecreatefrompng($imagepath);
  62. $thumb = imageResize($imageResourceId,$width,$height);
  63. $idx = strpos($imagename,".");
  64. $basename = substr($imagename,0, $idx);
  65. //guarda a imagem no formato png na pasta /uploads:
  66. imagepng($thumb,'album/'. $basename. "_thumb.". $ext);
  67. break;
  68.  
  69. case IMAGETYPE_GIF:
  70. //função imagecreatefromgif(): cria o recurso de uma imagem a partir de uma imagem gif
  71. $imageResourceId = imagecreatefromgif($imagepath);
  72. $thumb = imageResize($imageResourceId,$width,$height);
  73. $idx = strpos($imagename,".");
  74. $basename = substr($imagename,0, $idx);
  75. //guarda a imagem no formato gif na pasta /uploads:
  76. imagegif($thumb,'album/'. $basename. "_thumb.". $ext);
  77. break;
  78.  
  79. case IMAGETYPE_JPEG:
  80. //função imagecreatefromjpeg(): cria o recurso de uma imagem a partir de uma imagem jpg
  81. $imageResourceId = imagecreatefromjpeg($imagepath);
  82. $thumb = imageResize($imageResourceId,$width,$height);
  83. $idx = strpos($imagename,".");
  84. $basename = substr($imagename,0, $idx);
  85. //guarda a imagem no formato jpg na pasta /uploads:
  86. imagejpeg($thumb,'album/'. $basename. "_thumb.". $ext);
  87. break;
  88.  
  89. }
  90.  
  91. }
  92.  
  93.  
  94. //
  95. // Função imageResize() cria um thumbnail de 80x80 a partir de um recurso de uma imagem.
  96. //
  97. function imageResize($imageResourceId,$width,$height) {
  98. $targetWidth =80;
  99. $targetHeight =80;
  100.  
  101. $thumb=imagecreatetruecolor($targetWidth,$targetHeight);
  102. imagecopyresampled($thumb,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);
  103.  
  104. return $thumb;
  105.  
  106. }
  107.  
  108.  
  109. ?>
  110.  
  111.  
  112.  
  113. <!DOCTYPE html>
  114. <html>
  115. <body>
  116.  
  117. <form action="upload.php" method="post" enctype="multipart/form-data">
  118. Selecione uma imagem :<br>
  119. <input type="file" name="upload" id="upload"><br>
  120. <input type="submit" value="Carregar ficheiro" name="submit"><br>
  121. <a href=admin.php>voltar</a>
  122.  
  123. </form>
  124.  
  125. </body>
  126. </html>
Advertisement
Add Comment
Please, Sign In to add comment