|
@@ -1,5 +1,7 @@
|
|
|
<?php
|
|
<?php
|
|
|
|
|
|
|
|
|
|
+define("MAXSIZE", 460);
|
|
|
|
|
+
|
|
|
class Flammenkuchen
|
|
class Flammenkuchen
|
|
|
{
|
|
{
|
|
|
private $title;
|
|
private $title;
|
|
@@ -9,6 +11,17 @@ class Flammenkuchen
|
|
|
private function __construct()
|
|
private function __construct()
|
|
|
{ }
|
|
{ }
|
|
|
|
|
|
|
|
|
|
+ public static function fromForm($title, $desc)
|
|
|
|
|
+ {
|
|
|
|
|
+ $result = new Flammenkuchen();
|
|
|
|
|
+
|
|
|
|
|
+ $result->title = htmlentities($title);
|
|
|
|
|
+ $result->descr = htmlentities($desc);
|
|
|
|
|
+ $result->img = null;
|
|
|
|
|
+
|
|
|
|
|
+ return $result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public static function fromJson($data)
|
|
public static function fromJson($data)
|
|
|
{
|
|
{
|
|
|
$result = new Flammenkuchen();
|
|
$result = new Flammenkuchen();
|
|
@@ -20,15 +33,17 @@ class Flammenkuchen
|
|
|
return $result;
|
|
return $result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static function fromForm($title, $desc)
|
|
|
|
|
|
|
+ public function toJson()
|
|
|
{
|
|
{
|
|
|
- $result = new Flammenkuchen();
|
|
|
|
|
-
|
|
|
|
|
- $result->title = htmlentities($title);
|
|
|
|
|
- $result->descr = htmlentities($desc);
|
|
|
|
|
- $result->img = null;
|
|
|
|
|
|
|
+ ob_start();
|
|
|
|
|
+ imagejpeg($this->img);
|
|
|
|
|
+ $imageEncoded = base64_encode(ob_get_clean());
|
|
|
|
|
|
|
|
- return $result;
|
|
|
|
|
|
|
+ return json_encode(array(
|
|
|
|
|
+ "title" => $this->title
|
|
|
|
|
+ ,"desc" => $this->descr
|
|
|
|
|
+ ,"img" => $imageEncoded
|
|
|
|
|
+ ));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function getTitle()
|
|
public function getTitle()
|
|
@@ -53,31 +68,43 @@ class Flammenkuchen
|
|
|
return md5(base64_encode(ob_get_clean()));
|
|
return md5(base64_encode(ob_get_clean()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function toJson()
|
|
|
|
|
|
|
+ public function createImage($type, $tmpFile)
|
|
|
{
|
|
{
|
|
|
- ob_start();
|
|
|
|
|
- imagejpeg($this->img);
|
|
|
|
|
- $imageEncoded = base64_encode(ob_get_clean());
|
|
|
|
|
-
|
|
|
|
|
- return json_encode(array(
|
|
|
|
|
- "title" => $this->title
|
|
|
|
|
- ,"desc" => $this->descr
|
|
|
|
|
- ,"img" => $imageEncoded
|
|
|
|
|
- ));
|
|
|
|
|
|
|
+ $supported_files = array("image/png", "image/jpg", "image/jpeg");
|
|
|
|
|
+ if (!in_array($type, $supported_files))
|
|
|
|
|
+ return "Invalid file format: ".$type;
|
|
|
|
|
+ $imageSize = getimagesize($tmpFile);
|
|
|
|
|
+ if ($imageSize[0] > MAXSIZE || $imageSize[1] > MAXSIZE)
|
|
|
|
|
+ {
|
|
|
|
|
+ $newSize = array(-1, -1);
|
|
|
|
|
+ if ($imageSize[0] > $imageSize[1])
|
|
|
|
|
+ $newSize = array(MAXSIZE, $imageSize[1] * MAXSIZE / $imageSize[0]);
|
|
|
|
|
+ else
|
|
|
|
|
+ $newSize = array($imageSize[0] * MAXSIZE / $imageSize[1], MAXSIZE);
|
|
|
|
|
+ $imgOrigin = $this->createImageFromType($type, $tmpFile);
|
|
|
|
|
+ if ($imgOrigin === false)
|
|
|
|
|
+ return "Error reading input file";
|
|
|
|
|
+ $this->img = imagecreatetruecolor($newSize[0], $newSize[1]);
|
|
|
|
|
+ imagecopyresampled(
|
|
|
|
|
+ $this->img, $imgOrigin, // image ressources
|
|
|
|
|
+ 0, 0, 0, 0, // Start coordinates
|
|
|
|
|
+ $newSize[0], $newSize[1], // new file size
|
|
|
|
|
+ $imageSize[0], $imageSize[1]); // actual origin size
|
|
|
|
|
+ if ($this->img === false)
|
|
|
|
|
+ return "Error resizing image";
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ $this->img = $this->createImageFromType($type, $tmpFile);
|
|
|
|
|
+ return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function createImage($type, $tmpFile)
|
|
|
|
|
|
|
+ private function createImageFromType($type, $tmpFile)
|
|
|
{
|
|
{
|
|
|
if ($type == "image/png")
|
|
if ($type == "image/png")
|
|
|
- $this->img = imagecreatefrompng($tmpFile);
|
|
|
|
|
|
|
+ return imagecreatefrompng($tmpFile);
|
|
|
else if ($type == "image/jpg" || $type == "image/jpeg")
|
|
else if ($type == "image/jpg" || $type == "image/jpeg")
|
|
|
- $this->img = imagecreatefromjpeg($tmpFile);
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- die("no such type" .$type);
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
- return $this->img !== false;
|
|
|
|
|
|
|
+ return imagecreatefromjpeg($tmpFile);
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|