public function create_image_from_dataurl()
 {
     if (!isset($_POST['order_id']) || !isset($_POST['item_id']) || !isset($_POST['data_url']) || !isset($_POST['title']) || !isset($_POST['format'])) {
         exit;
     }
     check_ajax_referer('fpd_ajax_nonce', '_ajax_nonce');
     $order_id = trim($_POST['order_id']);
     $item_id = trim($_POST['item_id']);
     $data_url = trim($_POST['data_url']);
     $title = sanitize_title(trim($_POST['title']));
     $format = trim($_POST['format']);
     $dpi = isset($_POST['dpi']) ? intval($_POST['dpi']) : 300;
     //create fancy product orders directory
     if (!file_exists(FPD_ORDER_DIR)) {
         wp_mkdir_p(FPD_ORDER_DIR);
     }
     //create uploads dir
     $images_dir = FPD_ORDER_DIR . 'images/';
     if (!file_exists($images_dir)) {
         wp_mkdir_p($images_dir);
     }
     //shortcode order
     if (empty($item_id)) {
         $shortcode_dir = FPD_ORDER_DIR . 'images/_shortcode/';
         if (!file_exists($shortcode_dir)) {
             wp_mkdir_p($shortcode_dir);
         }
         $item_dir = $shortcode_dir . $order_id . '/';
         if (!file_exists($item_dir)) {
             wp_mkdir_p($item_dir);
         }
     } else {
         //create order dir
         $order_dir = $images_dir . $order_id . '/';
         if (!file_exists($order_dir)) {
             wp_mkdir_p($order_dir);
         }
         //create item dir
         $item_dir = $order_dir . $item_id . '/';
         if (!file_exists($item_dir)) {
             wp_mkdir_p($item_dir);
         }
     }
     $image_path = $item_dir . $title . '.' . $format;
     $image_exist = file_exists($image_path);
     //get the base-64 from data
     $base64_str = substr($data_url, strpos($data_url, ",") + 1);
     //decode base64 string
     $decoded = base64_decode($base64_str);
     $result = file_put_contents($image_path, $decoded);
     if ($format == 'jpeg') {
         require_once FPD_PLUGIN_ADMIN_DIR . '/inc/resampler.php';
         $source = imagecreatefromjpeg($image_path);
         list($width, $height) = getimagesize($image_path);
         $resampler = new Resampler();
         $im = $resampler->resample($source, $height, $width, $format, $dpi);
         file_put_contents($image_path, $im);
     }
     header('Content-Type: application/json');
     if ($result) {
         $image_url = content_url(substr($image_path, strrpos($image_path, '/fancy_products_orders/')));
         echo json_encode(array('code' => $image_exist ? 302 : 201, 'url' => $image_url, 'title' => $title));
     } else {
         echo json_encode(array('code' => 500));
     }
     die;
 }
 private function createImageByData()
 {
     if (!isset($this->request->post['order_id']) || !isset($this->request->post['item_id']) || !isset($this->request->post['data_url']) || !isset($this->request->post['title']) || !isset($this->request->post['format'])) {
         exit;
     }
     $json = array();
     $order_id = trim($this->request->post['order_id']);
     $item_id = trim($this->request->post['item_id']);
     $data_url = trim($this->request->post['data_url']);
     $title = trim($this->request->post['title']);
     $format = trim($this->request->post['format']);
     $dpi = isset($this->request->post['dpi']) ? intval($this->request->post['dpi']) : 300;
     //create fancy product orders directory
     if (!file_exists(DIR_IMAGE)) {
         mkdir(DIR_IMAGE);
     }
     //create uploads dir
     $images_dir = DIR_IMAGE . 'design_products_orders/';
     if (!file_exists($images_dir)) {
         mkdir($images_dir);
     }
     $images_dir = DIR_IMAGE . 'design_products_orders/images/';
     if (!file_exists($images_dir)) {
         mkdir($images_dir);
     }
     //create order dir
     $order_dir = $images_dir . $order_id . '/';
     if (!file_exists($order_dir)) {
         mkdir($order_dir);
     }
     //create item dir
     $item_dir = $order_dir . $item_id . '/';
     if (!file_exists($item_dir)) {
         mkdir($item_dir);
     }
     $image_path = $item_dir . $title . '.' . $format;
     $image_exist = file_exists($image_path);
     //get the base-64 from data
     $base64_str = substr($data_url, strpos($data_url, ",") + 1);
     //decode base64 string
     $decoded = base64_decode($base64_str);
     $result = file_put_contents($image_path, $decoded);
     if ($format == 'jpeg') {
         require_once DIR_SYSTEM . '/library/resampler.php';
         $source = imagecreatefromjpeg($image_path);
         list($width, $height) = getimagesize($image_path);
         $resampler = new Resampler();
         $im = $resampler->resample($source, $height, $width, $format, $dpi);
         file_put_contents($image_path, $im);
     }
     if ($result) {
         $image_url = str_replace(DIR_IMAGE, HTTP_CATALOG . 'image/', $image_path);
         $json['code'] = $image_exist ? 302 : 201;
         $json['url'] = $image_url;
         $json['title'] = $title;
         $this->response->setOutput(json_encode($json));
     } else {
         $json['code'] = 500;
         $this->response->setOutput(json_encode($json));
     }
 }