Example #1
0
 public function upload($form_data)
 {
     $validator = Validator::make($form_data, Image::$rules, Image::$messages);
     if ($validator->fails()) {
         return Response::json(['error' => TRUE, 'message' => $validator->messages()->first(), 'code' => 400], 400);
     }
     $photo = $form_data['file'];
     $this->temp_name = $form_data['temp_key'];
     $this->module = $form_data['module'];
     if (!empty($this->module) && $this->module == 'products') {
         $originalName = $photo->getClientOriginalName();
         $originalNameWithoutExt = substr($originalName, 0, strlen($originalName) - 4);
         if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
             $filename = iconv("UTF-8", "WINDOWS-1251", html_entity_decode(htmlentities(basename($originalNameWithoutExt), ENT_QUOTES, 'UTF-8')));
         } else {
             $filename = html_entity_decode(htmlentities(basename($originalNameWithoutExt), ENT_QUOTES, 'UTF-8'));
         }
         $filenameExt = $filename . '.jpg';
         $uploadSuccess1 = $this->original($photo, $filenameExt);
         $uploadSuccess2 = $this->sm_icon($photo, $filenameExt);
         $uploadSuccess3 = $this->md_icon($photo, $filenameExt);
         $uploadSuccess4 = $this->lg_icon($photo, $filenameExt);
         //If image is uploaded and thumbnails created
         if (!$uploadSuccess1 || !$uploadSuccess2 || !$uploadSuccess3 || !$uploadSuccess4) {
             //Store to database
             if (!empty($form_data['target'])) {
                 $image = ['img' => $filenameExt, 'position' => 0];
                 Model_Products::storeImages($image, $form_data['target']);
             }
             return Response::json(['error' => TRUE, 'message' => 'Server error while uploading', 'code' => 500], 500);
         }
     } elseif (!empty($this->module) && $this->module == 'tables') {
         $filename = basename($this->temp_name);
         $filenameExt = $filename . '.png';
         $uploadSuccess1 = $this->table_img($photo, $filenameExt);
         //If image is uploaded and thumbnails created
         if (!$uploadSuccess1) {
             return Response::json(['error' => TRUE, 'message' => 'Server error while uploading', 'code' => 500], 500);
         }
     } elseif (!empty($this->module) && $this->module == 'sliders') {
         $originalName = $photo->getClientOriginalName();
         $originalNameWithoutExt = substr($originalName, 0, strlen($originalName) - 4);
         $filename = basename($originalNameWithoutExt);
         $filenameExt = $filename . '.jpg';
         $uploadSuccess1 = $this->sliders($photo, $filenameExt);
         //If image is uploaded and thumbnails created
         if (!$uploadSuccess1) {
             return Response::json(['error' => TRUE, 'message' => 'Server error while uploading', 'code' => 500], 500);
         }
     }
     //		$sessionImage = new Image;
     //		$sessionImage->filename      = $allowed_filename;
     //		$sessionImage->original_name = $originalName;
     //		$sessionImage->save();
     return Response::json(['error' => FALSE, 'code' => 200], 200);
 }