Flammenkuchen.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. define("MAXSIZE", 460);
  3. class Flammenkuchen
  4. {
  5. private $title;
  6. private $descr;
  7. private $img;
  8. private function __construct()
  9. { }
  10. public static function fromForm($title, $desc)
  11. {
  12. $result = new Flammenkuchen();
  13. $result->title = htmlentities($title);
  14. $result->descr = htmlentities($desc);
  15. $result->img = null;
  16. return $result;
  17. }
  18. public static function fromJson($data)
  19. {
  20. $result = new Flammenkuchen();
  21. $result->title = $data->title;
  22. $result->descr = $data->desc;
  23. $result->img = imagecreatefromstring(base64_decode($data->img));
  24. return $result;
  25. }
  26. public function toJson()
  27. {
  28. ob_start();
  29. imagejpeg($this->img);
  30. $imageEncoded = base64_encode(ob_get_clean());
  31. return json_encode(array(
  32. "title" => $this->title
  33. ,"desc" => $this->descr
  34. ,"img" => $imageEncoded
  35. ));
  36. }
  37. public function getTitle()
  38. {
  39. return $this->title;
  40. }
  41. public function getDescription()
  42. {
  43. return $this->descr;
  44. }
  45. public function getImageJpeg()
  46. {
  47. return $this->img;
  48. }
  49. public function imageMd5()
  50. {
  51. ob_start();
  52. imagejpeg($this->img);
  53. return md5(base64_encode(ob_get_clean()));
  54. }
  55. public function createImage($type, $tmpFile)
  56. {
  57. $supported_files = array("image/png", "image/jpg", "image/jpeg");
  58. if (!in_array($type, $supported_files))
  59. return "Invalid file format: ".$type;
  60. $imageSize = getimagesize($tmpFile);
  61. if ($imageSize[0] > MAXSIZE || $imageSize[1] > MAXSIZE)
  62. {
  63. $newSize = array(-1, -1);
  64. if ($imageSize[0] > $imageSize[1])
  65. $newSize = array(MAXSIZE, $imageSize[1] * MAXSIZE / $imageSize[0]);
  66. else
  67. $newSize = array($imageSize[0] * MAXSIZE / $imageSize[1], MAXSIZE);
  68. $imgOrigin = $this->createImageFromType($type, $tmpFile);
  69. if ($imgOrigin === false)
  70. return "Error reading input file";
  71. $this->img = imagecreatetruecolor($newSize[0], $newSize[1]);
  72. imagecopyresampled(
  73. $this->img, $imgOrigin, // image ressources
  74. 0, 0, 0, 0, // Start coordinates
  75. $newSize[0], $newSize[1], // new file size
  76. $imageSize[0], $imageSize[1]); // actual origin size
  77. if ($this->img === false)
  78. return "Error resizing image";
  79. }
  80. else
  81. $this->img = $this->createImageFromType($type, $tmpFile);
  82. return true;
  83. }
  84. private function createImageFromType($type, $tmpFile)
  85. {
  86. if ($type == "image/png")
  87. return imagecreatefrompng($tmpFile);
  88. else if ($type == "image/jpg" || $type == "image/jpeg")
  89. return imagecreatefromjpeg($tmpFile);
  90. return false;
  91. }
  92. }