save.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. error_log("FILE: " . $img);
  20. //The next line wont work
  21. if (strpos($img, 'data:image/jpeg;base64,') < 10){
  22. $ext = 'jpg';
  23. $img = str_replace('data:image/jpeg;base64,', '', $img);
  24. }
  25. else if (strpos($img, 'data:image/png;base64,') < 10){
  26. $ext = 'png';
  27. $img = str_replace('data:image/png;base64,', '', $img);
  28. }
  29. $img = str_replace(' ', '+', $img);
  30. //echo "IMG idx " . $total . ": " . $img . "<br/><br/><br/>br/><br/>";
  31. $files[$total] = base64_decode($img);
  32. //$file = 'image.png';
  33. //$success = file_put_contents($file, $data);
  34. $titles[$total] = mysqli_real_escape_string($con, $_POST["title_$total"]);
  35. $descriptions[$total] = mysqli_real_escape_string($con, $_POST["description_$total"]);
  36. //Save image to file
  37. $file = 'image.' . $ext;
  38. $success = file_put_contents($file, $files[$total]);
  39. $total ++;
  40. }
  41. //Get next photo id
  42. $q_id = mysqli_query($con, "SELECT max(id) AS maxid FROM photo;");
  43. $r_id = mysqli_fetch_array($q_id);
  44. $id = $r_id["maxid"] + 1;
  45. //Store information
  46. for ($i = 0; $i < $total; $i++){
  47. //Get filename
  48. if (strlen($titles[$i]) > 0){
  49. $fname = "$id-" . permalink($titles[$i]);
  50. }
  51. else{
  52. $fname = "$id";
  53. }
  54. //Save image to file
  55. $file = "../img/galeria/$fname.$ext";
  56. $success = file_put_contents($file, $files[$i]);
  57. error_log("SUCCESS" . $success);
  58. //Convert to jpg and create miniature and preview_image
  59. //$im = new imagick("../img/galeria/$fname.png");
  60. //$handle = fopen("../img/galeria/$fname.png", 'rb');
  61. $im = new Imagick("../img/galeria/$fname.$ext");
  62. //$im->readImageFile($handle);
  63. $im->setImageBackgroundColor('white');
  64. $im = $im->flattenImages();
  65. $im->setImageFormat('jpg');
  66. $im->writeImage("../img/galeria/$fname.jpg");
  67. $im->resizeImage(800, 0, Imagick::FILTER_POINT, true);
  68. $im->resizeImage(0, 800, Imagick::FILTER_POINT, true);
  69. $im->writeImage("../img/galeria/view/$fname.jpg");
  70. $im->resizeImage(600, 0, Imagick::FILTER_POINT, true);
  71. $im->resizeImage(0, 600, Imagick::FILTER_POINT, true);
  72. $im->writeImage("../img/galeria/preview/$fname.jpg");
  73. $im->resizeImage(400, 0, Imagick::FILTER_POINT, true);
  74. $im->resizeImage(0, 400, Imagick::FILTER_POINT, true);
  75. $im->writeImage("../img/galeria/miniature/$fname.jpg");
  76. $im->resizeImage(200, 0, Imagick::FILTER_POINT, true);
  77. $im->resizeImage(0, 200, Imagick::FILTER_POINT, true);
  78. $im->writeImage("../img/galeria/thumb/$fname.jpg");
  79. $im->clear();
  80. $im->destroy();
  81. //Delete initial png
  82. //unlink("../img/galeria/$fname.png");
  83. //Get width, height ans size
  84. $s = filesize("../img/galeria/$fname.jpg");
  85. $w = getimagesize("../img/galeria/$fname.jpg")[0];
  86. $h = getimagesize("../img/galeria/$fname.jpg")[1];
  87. //Entry in database
  88. 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')");
  89. error_log("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')");
  90. mysqli_query($con, "INSERT INTO photo_album VALUES ($id, $album);");
  91. version();
  92. $id ++;
  93. }
  94. ?>