save.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. $http_host = $_SERVER['HTTP_HOST'];
  3. include("../functions.php");
  4. $con = startdb('rw');
  5. //Language
  6. $lang = selectLanguage();
  7. include("../lang/lang_" . $lang . ".php");
  8. //Get post data
  9. $username = mysqli_real_escape_string($con, $_POST["username"]);
  10. $album = mysqli_real_escape_string($con, $_POST["album"]);
  11. //Get the data form the photos
  12. $total = 0;
  13. $files = [];
  14. $titles = [];
  15. $descriptions = [];
  16. while (strlen($_POST["file_$total"]) > 0){
  17. //$files[$total] = $_POST["file_$total"];
  18. $img = $_POST["file_$total"];
  19. //The next line wont work
  20. $img = str_replace('data:image/png;base64,', '', $img);
  21. $img = str_replace(' ', '+', $img);
  22. //echo "IMG idx " . $total . ": " . $img . "<br/><br/><br/>br/><br/>";
  23. $files[$total] = base64_decode($img);
  24. //$file = 'image.png';
  25. //$success = file_put_contents($file, $data);
  26. $titles[$total] = mysqli_real_escape_string($con, $_POST["title_$total"]);
  27. $descriptions[$total] = mysqli_real_escape_string($con, $_POST["description_$total"]);
  28. //Save image to file
  29. $file = 'image.png';
  30. $success = file_put_contents($file, $files[$total]);
  31. $total ++;
  32. }
  33. //Get next photo id
  34. $q_id = mysqli_query($con, "SELECT max(id) AS maxid FROM photo;");
  35. $r_id = mysqli_fetch_array($q_id);
  36. $id = $r_id["maxid"] + 1;
  37. //Store information
  38. for ($i = 0; $i < $total; $i++){
  39. //Get filename
  40. if (strlen($titles[$i]) > 0){
  41. $fname = "$id-" . permalink($titles[$i]);
  42. }
  43. else{
  44. $fname = "$id";
  45. }
  46. //Save image to file
  47. $file = "../img/galeria/$fname.png";
  48. $success = file_put_contents($file, $files[$i]);
  49. //Convert to jpg and create miniature and preview_image
  50. //$im = new imagick("../img/galeria/$fname.png");
  51. //$handle = fopen("../img/galeria/$fname.png", 'rb');
  52. $im = new Imagick("../img/galeria/$fname.png");
  53. //$im->readImageFile($handle);
  54. $im->setImageBackgroundColor('white');
  55. $im = $im->flattenImages();
  56. $im->setImageFormat('jpg');
  57. $im->writeImage("../img/galeria/$fname.jpg");
  58. $im->resizeImage(800, 0, Imagick::FILTER_POINT, true);
  59. $im->resizeImage(0, 800, Imagick::FILTER_POINT, true);
  60. $im->writeImage("../img/galeria/view/$fname.jpg");
  61. $im->resizeImage(600, 0, Imagick::FILTER_POINT, true);
  62. $im->resizeImage(0, 600, Imagick::FILTER_POINT, true);
  63. $im->writeImage("../img/galeria/preview/$fname.jpg");
  64. $im->resizeImage(400, 0, Imagick::FILTER_POINT, true);
  65. $im->resizeImage(0, 400, Imagick::FILTER_POINT, true);
  66. $im->writeImage("../img/galeria/miniature/$fname.jpg");
  67. $im->resizeImage(200, 0, Imagick::FILTER_POINT, true);
  68. $im->resizeImage(0, 200, Imagick::FILTER_POINT, true);
  69. $im->writeImage("../img/galeria/t/$fname.jpg");
  70. $im->clear();
  71. $im->destroy();
  72. //Delete initial png
  73. //unlink("../img/galeria/$fname.png");
  74. //Get width, height ans size
  75. $s = filesize("../img/galeria/$fname.jpg");
  76. $w = getimagesize("../img/galeria/$fname.jpg")[0];
  77. $h = getimagesize("../img/galeria/$fname.jpg")[1];
  78. //Entry in database
  79. mysqli_query($con, "INSERT INTO photo (id, file, permalink, title_es, title_en, title_eu, description_es, description_en, description_eu, width, height, size, approved, username) VALUES ($id, '$fname.jpg', '$fname', '$titles[$i]', '$titles[$i]', '$titles[$i]', '$descriptions[$i]', '$descriptions[$i]', '$descriptions[$i]', $w, $h, $s, 0, '$username')");
  80. mysqli_query($con, "INSERT INTO photo_album VALUES ($id, $album);");
  81. version();
  82. $id ++;
  83. }
  84. ?>