uploadimage.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. include("../functions.php");
  3. $con = startdb("rw");
  4. if (!checkSession($con)){
  5. exit (-1);
  6. }
  7. else{
  8. //Get data
  9. $type = mysqli_real_escape_string($con, $_GET["type"]);
  10. $id = mysqli_real_escape_string($con, $_GET["id"]);
  11. $file = $_FILES["img"];
  12. //Check if image file
  13. $check = getimagesize($_FILES["img"]["tmp_name"]);
  14. if($check == false) {
  15. exit(-2);
  16. }
  17. //Upload the image to its destination
  18. switch ($type){
  19. case 'header':
  20. $q = mysqli_query($con, "SELECT * FROM festival WHERE id = $id;");
  21. if (mysqli_num_rows($q) > 0){
  22. $r = mysqli_fetch_array($q);
  23. move_uploaded_file($_FILES['img']['tmp_name'], "../../www/img/fiestas/tmp.$r[id].png");
  24. //Get image dimensions
  25. $i_w = $check[0];
  26. $i_h = $check[1];
  27. //Calculate preview and miniature dimensions
  28. if ($i_w > $i_h){
  29. $p_w = $IMG_SIZE_PREVIEW;
  30. $p_h = ceil($i_h * $IMG_SIZE_PREVIEW) / $i_w;
  31. $m_w = $IMG_SIZE_MINIATURE;
  32. $m_h = ceil($i_h * $IMG_SIZE_MINIATURE) / $i_w;
  33. }
  34. else{
  35. $p_h = $IMG_SIZE_PREVIEW;
  36. $p_w = ceil($i_h * $IMG_SIZE_PREVIEW) / $i_h;
  37. $m_h = $IMG_SIZE_MINIATURE;
  38. $m_w = ceil($i_w * $IMG_SIZE_MINIATURE) / $i_h;
  39. }
  40. //Convert to png
  41. $thumb = new Imagick();
  42. $thumb->readImage("../../www/img/fiestas/tmp.$r[id].png");
  43. $thumb->writeImage("../../www/img/fiestas/$r[year].png");
  44. $thumb->resizeImage(800,800,Imagick::FILTER_POINT, false);
  45. $thumb->writeImage("../../www/img/fiestas/view/$r[year].png");
  46. $thumb->resizeImage(600,600,Imagick::FILTER_POINT, false);
  47. $thumb->writeImage("../../www/img/fiestas/preview/$r[year].png");
  48. $thumb->resizeImage(340,340,Imagick::FILTER_POINT, false);
  49. $thumb->writeImage("../../www/img/fiestas/miniature/$r[year].png");
  50. $thumb->resizeImage(180,180,Imagick::FILTER_POINT, false);
  51. $thumb->writeImage("../../www/img/fiestas/thumb/$r[year].png");
  52. $thumb->clear();
  53. $thumb->destroy();
  54. //Update database
  55. if (strlen($r["img"]) == 0){
  56. mysqli_query($con, "UPDATE festival SET img = '$r[year].png' WHERE id = $id;");
  57. version();
  58. }
  59. }
  60. break;
  61. }
  62. }
  63. ?>