Example #1
0
 public static function create()
 {
     $foodData = json_decode($_POST["foodData"], true);
     $result = array();
     $validate = true;
     $result["foodName"] = false;
     $result["foodPrice"] = false;
     $result["foodType"] = false;
     if (!isset($foodData["foodName"]) || empty($foodData["foodName"])) {
         $result['foodName'] = true;
         $validate = false;
     }
     if (!isset($foodData["foodPrice"]) || empty($foodData["foodPrice"])) {
         $result['foodPrice'] = true;
         $validate = false;
     }
     if (!isset($foodData["foodType"]) || empty($foodData["foodType"])) {
         $result['foodType'] = true;
         $validate = false;
     }
     if (!isset($foodData["image"]) || empty($foodData["image"])) {
         $result['image'] = true;
         $validate = false;
     }
     if ($validate == false) {
         echo json_encode($result);
         return;
     }
     $x = $y = 0;
     $s = 250;
     $d = 100;
     $sizer = new ImageSizer();
     $sizer->load(self::$tmpDir . $foodData["image"]);
     $width = $sizer->getWidth();
     $height = $sizer->getHeight();
     $sizer->save(self::$imageDir . $foodData["image"]);
     $x = $width * ($foodData["cropX"] / $foodData["imageW"]);
     //echo $width." ".$foodData["cropX"]." ".$x."<br>";
     $y = $height * ($foodData["cropY"] / $foodData["imageH"]);
     //echo $height." ".$foodData["cropY"]." ".$y."<br>";
     $d = $width * ($foodData["cropW"] / $foodData["imageW"]);
     //echo $foodData["cropW"]." ".$d;
     if (intval($d) != 0) {
         $sizer->resize($x, $y, $s, $d);
         $sizer->save(self::$thumDir . $foodData["image"]);
     }
     $food = new Food();
     $food->setName($foodData["foodName"]);
     $food->setPrice($foodData["foodPrice"]);
     $food->setFoodTypeId(1);
     $food->setStatus(true);
     $food->setThumbnail($foodData["image"]);
     $food->setImage($foodData["image"]);
     $food->insertDatabase(null);
     echo json_encode($result);
 }