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);
     }
 }
 /**
  * Extract colors from an image like a human would do.
  * @param string $imagePath
  * @param int $count. Max palette size
  * @param int $minColorRatio. Minimum ratio, below colors are ignored (0 - 1)
  * @return array of colors (hex codes)
  * @throws Exception if image type not one of the following: 'jpg', 'jpeg', 'png', 'gif'
  */
 public static function extract($imagePath, $count = 1, $minColorRatio = 0)
 {
     $client = new BaseColorExtractor();
     switch (getimagesize($imagePath)[2]) {
         case IMAGETYPE_JPEG:
             $image = $client->loadJpeg($imagePath);
             break;
         case IMAGETYPE_PNG:
             $image = $client->loadPng($imagePath);
             break;
         case IMAGETYPE_GIF:
             $image = $client->loadGif($imagePath);
             break;
         default:
             throw new Exception('Image type must be one of the following: JPEG(JPG), PNG, GIF.');
     }
     $image->setMinColorRatio($minColorRatio);
     return $image->extract($count);
 }
Exemplo n.º 3
0
 /**
  * Uses PHP League's ColorExtractor class to
  * Select the most used colors from an image.
  * @todo Make input array.
  */
 public function get_image_colors($image_path, $num = 6, $image_format = null)
 {
     /**
      * @todo Auto-detect image format if not provided
      */
     $client = new ColorExtractor();
     switch ($image_format) {
         case 'jpeg':
             $load_image = $client->loadJpeg($image_path);
             $colors = $load_image->extract($num);
             break;
         case 'png':
             $load_image = $client->loadPng($image_path);
             $colors = $load_image->extract($num);
             break;
         case 'gif':
             $load_image = $client->loadGif($image_path);
             $colors = $load_image->extract($num);
             break;
     }
     return $colors;
 }
Exemplo n.º 4
0
 public function updateImage()
 {
     if (!Request::ajax()) {
         return App::abort(404);
     }
     $arrReturn = ['status' => 'error'];
     $id = Input::has('id') ? Input::get('id') : 0;
     if ($id) {
         $create = false;
         try {
             $image = VIImage::findorFail((int) Input::get('id'));
         } catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
             return App::abort(404);
         }
         $message = 'has been updated successful';
     } else {
         $create = true;
         $image = new VIImage();
         $message = 'has been created successful';
     }
     if ($create && !Input::hasFile('image')) {
         return ['status' => 'error', 'message' => 'You need to upload at least 1 image.'];
     }
     $image->name = Input::get('name');
     $image->short_name = Str::slug($image->name);
     $image->description = Input::get('description');
     $image->keywords = Input::get('keywords');
     $image->keywords = rtrim(trim($image->keywords), ',');
     $image->model = Input::get('model');
     $image->model = rtrim(trim($image->model), ',');
     $image->artist = Input::get('artist');
     $image->age_from = Input::get('age_from');
     $image->age_to = Input::get('age_to');
     $image->gender = Input::get('gender');
     $image->number_people = Input::get('number_people');
     $pass = $image->valid();
     if ($pass->passes()) {
         if (Input::hasFile('image')) {
             $color_extractor = new ColorExtractor();
             $myfile = Input::file('image');
             $mime_type = $myfile->getClientMimeType();
             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]);
                     }
                 }
             }
             $image->main_color = $main_color;
         }
         $image->save();
         //insert into statistic_images table
         if ($create) {
             StatisticImage::create(['image_id' => $image->id, 'view' => '0', 'download' => '0']);
         }
         foreach (['category_id' => 'categories', 'collection_id' => 'collections'] as $key => $value) {
             $arrOld = $remove = $add = [];
             $old = $image->{$value};
             $data = Input::has($key) ? Input::get($key) : [];
             $data = (array) json_decode($data[0]);
             if (!empty($old)) {
                 foreach ($old as $val) {
                     if (!in_array($val->id, $data)) {
                         $remove[] = $val->id;
                     } else {
                         $arrOld[] = $val->id;
                     }
                 }
             }
             foreach ($data as $id) {
                 if (!$id) {
                     continue;
                 }
                 if (!in_array($id, $arrOld)) {
                     $add[] = $id;
                 }
             }
             if (!empty($remove)) {
                 $image->{$value}()->detach($remove);
             }
             if (!empty($add)) {
                 $image->{$value}()->attach($add);
             }
             foreach ($add as $v) {
                 $arrOld[] = $v;
             }
             $image->{'arr' . ucfirst($value)} = $arrOld;
         }
         $path = public_path('assets' . DS . 'upload' . DS . 'images' . DS . $image->id);
         if ($create) {
             $main = new VIImageDetail();
             $main->image_id = $image->id;
             $main->type = 'main';
             File::makeDirectory($path, 0755, true);
         } else {
             $main = VIImageDetail::where('type', 'main')->where('image_id', $image->id)->first();
             File::delete(public_path($main->path));
         }
         if (Input::hasFile('image')) {
             $file = Input::file('image');
             $name = $image->short_name . '.' . $file->getClientOriginalExtension();
             $file->move($path, $name);
             $imageChange = true;
         } else {
             if (Input::has('choose_image') && Input::has('choose_name')) {
                 $chooseImage = Input::get('choose_image');
                 $name = Input::get('choose_name');
                 file_put_contents($path . DS . $name, file_get_contents($chooseImage));
                 $imageChange = true;
             }
         }
         if (isset($imageChange)) {
             $main->path = 'assets/upload/images/' . $image->id . '/' . $name;
             $img = Image::make($path . DS . $name);
             $main->width = $img->width();
             $main->height = $img->height();
             $main->ratio = $main->width / $main->height;
             $img = new Imagick($path . DS . $name);
             $dpi = $img->getImageResolution();
             $main->dpi = $dpi['x'] > $dpi['y'] ? $dpi['x'] : $dpi['y'];
             $main->size = $img->getImageLength();
             $main->extension = strtolower($img->getImageFormat());
             $main->save();
             BackgroundProcess::makeSize($main->detail_id);
             $image->changeImg = true;
             if ($create) {
                 $image->dimension = $main->width . 'x' . $main->height;
                 $image->newImg = true;
                 $image->path = URL . '/pic/large-thumb/' . $image->short_name . '-' . $image->id . '.jpg';
             }
         }
         $arrReturn['status'] = 'ok';
         $arrReturn['message'] = "{$image->name} {$message}.";
         $arrReturn['data'] = $image;
         return $arrReturn;
     }
     $arrReturn['message'] = '';
     $arrErr = $pass->messages()->all();
     foreach ($arrErr as $value) {
         $arrReturn['message'] .= "{$value}\n";
     }
     return $arrReturn;
 }
Exemplo n.º 5
0
 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');
 }
Exemplo n.º 6
0
 private function getImageColorInfo($filename)
 {
     $filenameSample = $filename . '.sample.jpg';
     if (file_exists($filename)) {
         if (!file_exists($filenameSample)) {
             $imageFile = Image::fromFile($filename);
             $imageFile->resize(200, 200, Image::FILL);
             $imageFile->save($filenameSample, 100, Image::JPEG);
         }
         $client = new Client();
         $image = $client->loadJpeg($filenameSample);
         $palette = $image->extract(6);
         $dominantColor = ColorThief::getColor($filenameSample);
         $dominantColor = \Aprila\Utils\Colors::rgbToHex($dominantColor);
         $color = new Color($dominantColor);
         $isDark = $color->isDark();
         $imageColorInfo = ['main' => $dominantColor, 'palette' => $palette, 'isDark' => $isDark];
         FileSystem::delete($filenameSample);
         return $imageColorInfo;
     }
     return [];
 }