Example #1
0
 /**
  * Save encoded base64 image
  * 
  * @param mixed $image_src
  * @param mixed $image_type
  * @param mixed $file_type
  */
 public static function saveImageBase64($image_src, $image_type, $thumb_width = '', $thumb_height = '')
 {
     $response = array();
     $encodedString = utf8_decode($image_src);
     if ($image_type == 'image/jpeg') {
         $file_ext = 'jpg';
         $image = imagecreatefromjpeg($encodedString);
     } else {
         if ($image_type == 'image/png') {
             $file_ext = 'png';
             $image = imagecreatefrompng($encodedString);
         } else {
             if ($image_type == 'image/gif') {
                 $file_ext = 'gif';
                 $image = imagecreatefromgif($encodedString);
             } else {
                 die;
             }
         }
     }
     // Make thumbail
     $thumb_width = !empty($thumb_width) ? $thumb_width : self::$_thumb_width;
     $thumb_height = !empty($thumb_height) ? $thumb_height : self::$_thumb_height;
     // EXIF
     $image = self::_exifRotate($image, $encodedString);
     $width = imagesx($image);
     $height = imagesy($image);
     $original_aspect = $width / $height;
     $thumb_aspect = $thumb_width / $thumb_height;
     if ($original_aspect >= $thumb_aspect) {
         // If image is wider than thumbnail (in aspect ratio sense)
         $new_height = $thumb_height;
         $new_width = $width / ($height / $thumb_height);
     } else {
         // If the thumbnail is wider than the image
         $new_width = $thumb_width;
         $new_height = $height / ($width / $thumb_width);
     }
     $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
     imagealphablending($thumb, false);
     imagesavealpha($thumb, true);
     // Resize and crop
     imagecopyresampled($thumb, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
     // Save path
     $uniqid = uniqid();
     $file_path = self::$_folderPath . "/" . $uniqid . "." . $file_ext;
     $file_path_thumb = self::$_folderPath . "/" . $uniqid . "_thumb." . $file_ext;
     $file_path_absolute = APPLICATION_PATH . "/../public/";
     // Create image
     if ($image_type == 'image/jpeg') {
         $image_result = imagejpeg($image, $file_path_absolute . $file_path, 80);
         $thumb_result = imagejpeg($thumb, $file_path_absolute . $file_path_thumb, 80);
     } else {
         if ($image_type == 'image/png') {
             imagealphablending($image, false);
             imagesavealpha($image, true);
             $image_result = imagepng($image, $file_path_absolute . $file_path, 8);
             $thumb_result = imagepng($thumb, $file_path_absolute . $file_path_thumb, 8);
         } else {
             if ($image_type == 'image/gif') {
                 $image_result = imagegif($image, $file_path_absolute . $file_path, 80);
                 $thumb_result = imagegif($thumb, $file_path_absolute . $file_path_thumb, 80);
             }
         }
     }
     if ($image_result && $thumb_result) {
         $tbl = KM_Abstract::f('files');
         $image = $tbl->createRow();
         $image->path = $file_path;
         $image->path_thumb = $file_path_thumb;
         $image->width = $width;
         $image->width_thumb = $new_width;
         $image->height = $height;
         $image->height_thumb = $new_height;
         $image->mime_type = $image_type;
         $image->status_id = 1;
         $image->save();
         return $image->toArray();
     }
     return null;
 }
Example #2
0
 public function indexAction()
 {
     /* if response success */
     if ($this->type == 1) {
         $n_data = array();
         $table_arr = explode(',', $this->table);
         foreach ($table_arr as $table) {
             $_array = array();
             $data = array();
             $_data = array();
             $this->table = $table;
             $this->dbTable = $this->f($this->table);
             /* map all */
             switch ($this->method) {
                 /* Make all joins */
                 case 'map_all':
                     // get joins
                     if (isset($this->joins[$this->table])) {
                         $this->join = $this->joins[$this->table];
                     }
                     break;
                     // get last id
                 // get last id
                 case 'last_id':
                     $_array = $this->dbTable->getLastId();
                     break;
                 default:
                     break;
             }
             /** if not from  method */
             if (empty($_array)) {
                 /* get by id */
                 if (!isNE($this->id)) {
                     $_array = $this->dbTable->selectBy($this->filter, $this->id, $this->join, $this->like, $this->col, $this->method)->toArray();
                     $_array = array($_array);
                     /* get all */
                 } else {
                     $_array = $this->dbTable->selectBy($this->filter, null, $this->join, $this->like, $this->col, $this->method)->toArray();
                 }
             }
             /** build settings special case **/
             if (preg_match('/settings/', $this->table)) {
                 $_data = $this->_makeSettings($_array);
                 /** build joins **/
             } elseif (!isNE($this->join)) {
                 if (!empty($_array)) {
                     $id_array = array();
                     foreach ($_array as $key => $item) {
                         foreach ($item as $col => $v) {
                             if (strpos($col, '|') !== false) {
                                 $e = explode('|', $col);
                                 $j_table = $e[0];
                                 $j_cn = $e[1];
                                 $j_col = $e[2];
                                 $j_cn_ = str_replace('_id', '', $j_cn);
                                 $item[$j_cn_][$j_col] = $v;
                                 unset($item[$col]);
                                 $id_array[$j_cn_] = $j_cn;
                             }
                         }
                         /** set ids */
                         foreach ($id_array as $table => $id) {
                             $item[$table]['id'] = $item[$id];
                         }
                         /** get with value */
                         if (isset($this->_params['get_with'])) {
                             if (isset($item[$this->_params['get_with']])) {
                                 $_data[$item[$this->_params['get_with']]] = $item;
                             } else {
                                 $_data[] = $item;
                             }
                         } else {
                             $_data[] = $item;
                         }
                     }
                     /**
                      * join deeper columns
                      * &join_deep[]=pet,pet_breed_id,pet_breed
                      * - in table, in id, table to join
                      */
                     if (isset($this->_params['join_deep'])) {
                         $__data = $_data;
                         $_data = array();
                         foreach ($__data as $item_k => $item) {
                             foreach ($this->_params['join_deep'] as $jd) {
                                 $jd_a = explode(',', $jd);
                                 $col = $item[$jd_a[0]][$jd_a[1]];
                                 $tbl = KM_Abstract::f($jd_a[2]);
                                 $res = $tbl->selectBy(null, $col)->toArray();
                                 $item[$jd_a[0]][$jd_a[2]] = $res;
                             }
                             $_data[] = $item;
                         }
                     }
                 }
                 /** default empty array **/
             } else {
                 if (!empty($_array)) {
                     /** get with value */
                     if (isset($this->_params['get_with'])) {
                         foreach ($_array as $key => $item) {
                             $_data[$item[$this->_params['get_with']]] = $item;
                         }
                     } else {
                         $_data = $_array;
                     }
                 } else {
                     $_data = array();
                 }
             }
             $data = $_data;
             /** if multiple request */
             if (count($table_arr) > 1) {
                 $n_data[$this->table]['data'] = $data;
                 $n_data[$this->table]['count'] = count($data);
                 /** single request */
             } else {
                 $n_data = $data;
             }
         }
         /* send response */
         if (count($table_arr) > 1) {
             $this->response = array('multiple' => $n_data, 'message' => 'Success', 'type' => 1);
         } else {
             $this->response = array('data' => $n_data, 'count' => count($n_data), 'message' => 'Success', 'type' => 1);
         }
     }
 }
 public function init()
 {
     parent::init();
 }