public function run()
 {
     $faker = Faker::create();
     $imgDir = public_path() . DS . 'assets' . DS . 'upload' . DS . 'images';
     if (!File::exists($imgDir)) {
         File::makeDirectory($imgDir, 0755);
     }
     File::cleanDirectory($imgDir, true);
     foreach (range(1, 500) as $index) {
         $dir = $imgDir . DS . $index;
         if (!File::exists($dir)) {
             File::makeDirectory($dir, 0755);
         }
         $width = $faker->numberBetween(800, 1024);
         $height = $faker->numberBetween(800, 1024);
         $orgImg = $faker->image($dir, $width, $height);
         chmod($orgImg, 0755);
         $img = str_replace($dir . DS, '', $orgImg);
         $image = new Imagick($orgImg);
         $dpi = $image->getImageResolution();
         $dpi = $dpi['x'] > $dpi['y'] ? $dpi['x'] : $dpi['y'];
         $size = $image->getImageLength();
         $extension = strtolower($image->getImageFormat());
         //get 5 most used colors from the image
         $color_extractor = new ColorExtractor();
         $myfile = $orgImg;
         $mime_type = $image->getImageMimeType();
         switch ($mime_type) {
             case 'image/jpeg':
                 $palette_obj = $color_extractor->loadJpeg($myfile);
                 break;
             case 'image/png':
                 $palette_obj = $color_extractor->loadPng($myfile);
                 break;
             case 'image/gif':
                 $palette_obj = $color_extractor->loadGif($myfile);
                 break;
         }
         $main_color = '';
         if (is_object($palette_obj)) {
             $arr_palette = $palette_obj->extract(5);
             if (!empty($arr_palette)) {
                 $main_color = strtolower($arr_palette[0]);
                 for ($i = 1; $i < count($arr_palette); $i++) {
                     $main_color .= ',' . strtolower($arr_palette[$i]);
                 }
             }
         }
         //update field main_color from images table
         $image_obj = VIImage::findorFail((int) $index);
         $image_obj->main_color = $main_color;
         $image_obj->save();
         $id = VIImageDetail::insertGetId(['path' => 'assets/upload/images/' . $index . '/' . $img, 'height' => $height, 'width' => $width, 'ratio' => $width / $height, 'dpi' => $dpi, 'size' => $size, 'extension' => $extension, 'type' => 'main', 'image_id' => $index]);
         BackgroundProcess::makeSize($id);
     }
 }
 public function uploadFile()
 {
     if (Auth::user()->check()) {
         if (Input::hasFile('myfiles')) {
             $name = Input::get('name');
             $short_name = Str::slug($name);
             $description = Input::get('description');
             $keywords = Input::get('keywords');
             $keywords = rtrim(trim($keywords), ',');
             $model = Input::get('model');
             $model = rtrim(trim($model), ',');
             $artist = Input::get('artist');
             $age_from = Input::get('age_from');
             $age_to = Input::get('age_to');
             $gender = Input::get('gender');
             $number_people = Input::get('number_people');
             $type_id = Input::get('type_id');
             $arr_category_ids = Input::get('category_id');
             $faker = Faker::create();
             $destination_store = Input::get('destination_store');
             //insert to images table
             // $keywords = '';
             // for( $i = 0; $i < $faker->numberBetween(4, 9); $i++ ) {
             //     $keywords .= $faker->word.',';
             // }
             // $keywords = rtrim($keywords, ',');
             // $name = $faker->name;
             // $short_name = Str::slug($name);
             // $gender = $faker->randomElement($array = array ('male','female','both','any'));
             // $age_from = $faker->numberBetween(0, 90);
             // $age_to = $faker->numberBetween(0, 90);
             // while($age_from >$age_to){
             //     $age_from = $faker->numberBetween(0, 90);
             //     $age_to = $faker->numberBetween(0, 90);
             // }
             $ethnicity = $faker->randomElement($array = array('african', 'african_american', 'black', 'brazilian', 'chinese', 'caucasian', 'east_asian', 'hispanic', 'japanese', 'middle_eastern', 'native_american', 'pacific_islander', 'south_asian', 'southeast_asian', 'other', 'any'));
             //$number_people = $faker->numberBetween(0, 10);
             $editorial = $faker->numberBetween(0, 1);
             //$type_id = $faker->numberBetween(1, 3);
             $color_extractor = new ColorExtractor();
             $myfiles = Input::file('myfiles');
             $mime_type = $myfiles[0]->getClientMimeType();
             switch ($mime_type) {
                 case 'image/jpeg':
                     $palette_obj = $color_extractor->loadJpeg($myfiles[0]);
                     break;
                 case 'image/png':
                     $palette_obj = $color_extractor->loadPng($myfiles[0]);
                     break;
                 case 'image/gif':
                     $palette_obj = $color_extractor->loadGif($myfiles[0]);
                     break;
                 default:
                     # code...
                     break;
             }
             $main_color = '';
             if (is_object($palette_obj)) {
                 $arr_palette = $palette_obj->extract(5);
                 if (!empty($arr_palette)) {
                     $main_color = strtolower($arr_palette[0]);
                     for ($i = 1; $i < count($arr_palette); $i++) {
                         $main_color .= ',' . strtolower($arr_palette[$i]);
                     }
                 }
             }
             $image_id = VIImage::insertGetId(['name' => $name, 'short_name' => $short_name, 'description' => $description, 'keywords' => $keywords, 'main_color' => $main_color, 'type_id' => $type_id, 'model' => $model, 'artist' => $artist, 'gender' => $gender, 'age_from' => $age_from, 'age_to' => $age_to, 'ethnicity' => $ethnicity, 'number_people' => $number_people, 'editorial' => $editorial, 'author_id' => Auth::user()->get()->id, 'store' => $destination_store]);
             //insert into statistic_images table
             StatisticImage::create(['image_id' => $image_id, 'view' => '0', 'download' => '0']);
             //insert into images_categories table
             if (!empty($arr_category_ids)) {
                 foreach ($arr_category_ids as $category_id) {
                     ImageCategory::create(['image_id' => $image_id, 'category_id' => $category_id]);
                 }
             }
             $result = array();
             for ($i = 0; $i < count($myfiles); $i++) {
                 $file = $myfiles[$i];
                 $extension = strtolower($file->getClientOriginalExtension());
                 $file_name = $faker->lexify($string = '???????????????????');
                 $url = $file_name . "." . $extension;
                 //get image's information
                 $file_content = file_get_contents($file);
                 $image = new Imagick();
                 $image->pingImageBlob($file_content);
                 $dpi = $image->getImageResolution();
                 $dpi = $dpi['x'] > $dpi['y'] ? $dpi['x'] : $dpi['y'];
                 $size = $image->getImageLength();
                 $width = $image->getImageWidth();
                 $height = $image->getImageHeight();
                 $result[$i]['filename'] = $file->getClientOriginalName();
                 $result[$i]['result'] = true;
                 if ($destination_store == 'dropbox') {
                     try {
                         $this->filesystem->write($url, $file_content);
                     } catch (\Dropbox\Exception $e) {
                         $result[$i]['result'] = false;
                         echo $e->getMessage();
                     }
                 } else {
                     $upload_folder = 'assets' . DS . 'upload' . DS . 'images' . DS . $image_id;
                     $imgDir = public_path() . DS . $upload_folder;
                     if (!File::exists($imgDir)) {
                         File::makeDirectory($imgDir, 0755);
                     }
                     $url = $upload_folder . DS . $url;
                     $url = str_replace('\\', '/', $url);
                     if (!VIImage::upload($file, $imgDir, $width, true, $file_name)) {
                         $result[$i]['result'] = false;
                     }
                 }
                 //insert to image_details table
                 $id = VIImageDetail::insertGetId(['path' => $url, 'height' => $height, 'width' => $width, 'ratio' => $width / $height, 'dpi' => $dpi, 'size' => $size, 'extension' => $extension, 'type' => 'main', 'size_type' => $i + 1, 'image_id' => $image_id]);
                 if (!$id) {
                     $result[$i]['result'] = false;
                 }
             }
             Session::flash('message', $result);
         } else {
             Session::flash('message', 'Please choose the image!');
         }
         return Redirect::route('upload-page');
     }
     return Redirect::route('account-sign-in');
 }