uploadimage.php 3.4 KB

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