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);
     }
 }
 function loadChooseDownload($image_id)
 {
     $query = VIImageDetail::select('image_details.image_id', 'image_details.detail_id', 'image_details.width', 'image_details.height', 'image_details.dpi', 'image_details.size', 'image_details.size_type')->leftJoin('images', function ($join) {
         $join->on('image_details.image_id', '=', 'images.id');
     })->where('image_details.image_id', $image_id)->orderBy('size_type')->get();
     $arrImageDetails = array();
     if (!$query->isempty()) {
         $i = 0;
         foreach ($query as $value) {
             $arrImageDetails[$i]['detail_id'] = $value->detail_id;
             $arrImageDetails[$i]['image_id'] = $value->image_id;
             $arrImageDetails[$i]['width'] = $value->width;
             $arrImageDetails[$i]['height'] = $value->height;
             $arrImageDetails[$i]['dpi'] = $value->dpi;
             $arrImageDetails[$i]['size'] = $value->size;
             $arrImageDetails[$i]['size_type'] = $value->size_type;
             switch ($value->size_type) {
                 case '1':
                     $size_name = 'Small';
                     break;
                 case '2':
                     $size_name = 'Medium';
                     break;
                 case '3':
                     $size_name = 'Large';
                     break;
                 case '4':
                     $size_name = 'Supper';
                     break;
                 default:
                     $size_name = 'Small';
                     break;
             }
             $arrImageDetails[$i]['size_name'] = $size_name;
             $i++;
         }
     }
     $htmlChooseDownload = View::make('frontend.images.choose-download')->with('arrImageDetails', $arrImageDetails)->render();
     if (Request::ajax()) {
         $arrReturn = ['status' => 'ok', 'message' => '', 'html' => $htmlChooseDownload];
         $response = Response::json($arrReturn);
         $response->header('Content-Type', 'application/json');
         return $response;
     }
     return $htmlChooseDownload;
 }
 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;
 }
 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');
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     require_once base_path('vendor/google/apiclient/src/Google/autoload.php');
     $googleDrive = Configure::getGoogleDrive();
     if (empty($googleDrive)) {
         return $this->error('Google Drive configure was empty.');
     } else {
         if (!isset($googleDrive['google_drive_email'])) {
             return $this->error('Google Drive email was not set.');
         } else {
             if (!isset($googleDrive['google_drive_key_file'])) {
                 return $this->error('Google Drive key file was not set.');
             } else {
                 if (!File::exists($googleDrive['google_drive_key_file'])) {
                     return $this->error("Google Drive key file cannot be found.\nPath: {$googleDrive['google_drive_key_file']}");
                 }
             }
         }
     }
     $auth = new Google_Auth_AssertionCredentials($googleDrive['google_drive_email'], [Google_Service_Drive::DRIVE, Google_Service_Drive::DRIVE_READONLY, Google_Service_Drive::DRIVE_FILE, Google_Service_Drive::DRIVE_METADATA_READONLY], File::get($googleDrive['google_drive_key_file']));
     $client = new Google_Client();
     $client->setAssertionCredentials($auth);
     $service = new Google_Service_Drive($client);
     $query = 'mimeType contains \'image\' and mimeType != \'image/svg+xml\'';
     $lastDate = VIImage::where('store', 'google-drive')->orderBy('id', 'desc')->pluck('created_at');
     if ($lastDate) {
         $lastDate = gmdate("Y-m-d\\TH:i:s", strtotime($lastDate));
         $query .= ' and modifiedDate > \'' . $lastDate . '\'';
     }
     $files = $service->files->listFiles(['q' => $query])->getItems();
     $arrInsert = $arrDelete = [];
     $this->getFiles($files, $arrInsert, $arrDelete);
     while (!empty($files->nextPageToken)) {
         $files = $service->files->listFiles(['q' => $query, 'pageToken' => $files->nextPageToken]);
         $this->getFiles($files, $arrInsert, $arrDelete);
     }
     if (!empty($arrDelete)) {
         $detailImages = $images = [];
         foreach ($arrDelete as $k => $file) {
             $image = VIImageDetail::select('image_id', 'detail_id')->where('path', $file)->first();
             if ($image) {
                 $detailImages[] = $image->detail_id;
                 $images[] = $image->image_id;
             }
         }
         VIImageDetail::destroy($detailImages);
         VIImage::destroy($images);
     }
     if (!empty($arrInsert)) {
         foreach ($arrInsert as $file) {
             $image = new VIImage();
             $image->name = $file['name'];
             $image->short_name = Str::slug($image->name);
             $image->store = 'google-drive';
             $image->save();
             $imageDetail = new VIImageDetail();
             $imageDetail->path = $file['link'];
             $imageDetail->width = $file['width'];
             $imageDetail->height = $file['height'];
             $imageDetail->size = $file['size'];
             $imageDetail->ratio = $file['width'] / $file['height'];
             $imageDetail->extension = $file['extension'];
             $imageDetail->type = 'main';
             $imageDetail->image_id = $image->id;
             $imageDetail->save();
         }
     }
     $this->info('Query: "' . $query . '".' . "\n" . 'Inserted ' . count($arrInsert) . ' images(s).' . "\n" . 'Deleted ' . count($arrDelete) . ' image(s).');
 }
 public function getLightbox($id_lightbox)
 {
     $lightbox = Lightbox::findorFail($id_lightbox);
     $image_lightbox = LightboxImages::where('lightbox_id', '=', $id_lightbox)->get();
     $images = array();
     if ($image_lightbox->count() > 0) {
         $i = 0;
         foreach ($image_lightbox as $key => $value) {
             $images[$i]['id'] = $value->id;
             $imgdetail = VIImageDetail::select('image_details.image_id', 'images.short_name', 'images.name');
             $imgdetail->leftJoin('images', 'images.id', '=', 'image_details.image_id');
             $imgdetail->where('image_details.image_id', '=', $value->image_id);
             $imgdetail->where('image_details.type', '=', 'main');
             $imageDetail = $imgdetail->first();
             if (is_object($imageDetail)) {
                 $images[$i]['image_id'] = $imageDetail->image_id;
                 $images[$i]['short_name'] = $imageDetail->short_name;
                 $images[$i]['name'] = $imageDetail->name;
                 $images[$i]['path'] = '/pic/thumb/' . $imageDetail->short_name . '-' . $imageDetail->image_id . '.jpg';
             }
             $i++;
         }
     }
     if (Request::ajax()) {
         $response = Response::json(array('images' => $images, 'lightbox' => $lightbox));
         $response->header('Content-Type', 'application/json');
         return $response;
     }
     $list_lightbox = Lightbox::where('user_id', '=', Auth::user()->get()->id)->where('id', '<>', $id_lightbox)->orderBy('name')->get()->toArray();
     $this->layout->content = View::make('frontend.lightbox.lightbox-detail')->with(['images' => $images, 'lightbox' => $lightbox, 'list_lightbox' => $list_lightbox]);
 }
Example #7
0
 private function sizes()
 {
     $imageDetailId = $this->option('image_detail_id');
     $image = VIImageDetail::select('images.short_name', 'images.id', 'image_details.path', 'image_details.detail_id', 'image_details.ratio')->join('images', 'images.id', '=', 'image_details.image_id')->where('image_details.detail_id', $imageDetailId)->first();
     if (!is_object($image)) {
         $this->error('Image ' . $imageDetailId . ' cannot be found.');
         return;
     }
     $this->deleteExistSizes($imageDetailId, $image->id);
     $img = Image::make(public_path($image->path));
     $width = $img->width();
     $height = $img->height();
     $w = $h = null;
     $arrSizes = $arrInsert = $extra = [];
     if ($width * $height > 24000000) {
         $sizeType = 4;
         $arrSizes = [['w' => $width / 2, 'h' => $height / 2, 'sizeType' => 3], ['w' => $image->ratio > 1 ? 1000 : null, 'h' => $image->ratio <= 1 ? 1000 : null, 'sizeType' => 2], ['w' => $image->ratio > 1 ? 500 : null, 'h' => $image->ratio <= 1 ? 500 : null, 'sizeType' => 1]];
     } else {
         if ($width > 1500 || $height > 1500) {
             $sizeType = 3;
             $arrSizes = [['w' => $image->ratio > 1 ? 1000 : null, 'h' => $image->ratio <= 1 ? 1000 : null, 'sizeType' => 2], ['w' => $image->ratio > 1 ? 500 : null, 'h' => $image->ratio <= 1 ? 500 : null, 'sizeType' => 1]];
         } else {
             if ($width >= 1000 || $height >= 1000) {
                 if ($width > 1000 || $height > 1000) {
                     $h = $w = $image->ratio > 1 ? 1000 : null;
                     if ($w == null) {
                         $h = 1000;
                     }
                     $extra = ['w' => $w, 'h' => $h];
                 }
                 $sizeType = 2;
                 $arrSizes = [['w' => $image->ratio > 1 ? 500 : null, 'h' => $image->ratio <= 1 ? 500 : null, 'sizeType' => 1]];
             } else {
                 if ($width > 500 || $height > 500) {
                     $h = $w = $image->ratio > 1 ? 500 : null;
                     if ($w == null) {
                         $h = 500;
                     }
                     $extra = ['w' => $w, 'h' => $h];
                 }
                 $sizeType = 1;
             }
         }
     }
     $arrUpdate = ['size_type' => $sizeType];
     if (!empty($extra)) {
         $img = Image::make(public_path($image->path));
         $img->resize($extra['w'], $extra['h'], function ($constraint) {
             $constraint->aspectRatio();
         });
         $img->save(public_path($image->path));
         $arrUpdate['width'] = $img->width();
         $arrUpdate['height'] = $img->height();
         $arrUpdate['size'] = $img->filesize();
     }
     $arrInsert = [];
     foreach ($arrSizes as $size) {
         $img = Image::make(public_path($image->path));
         $img->resize($size['w'], $size['h'], function ($constraint) {
             $constraint->aspectRatio();
         });
         $path = 'assets/upload/images/' . $image->id . '/' . $size['sizeType'] . '-' . $image->short_name . '.jpg';
         $img->save(public_path($path));
         $size_type = $size['sizeType'];
         $width = $img->width();
         $height = $img->height();
         $ratio = $width / $height;
         $img = new Imagick(public_path($path));
         $dpi = $img->getImageResolution();
         $dpi = $dpi['x'] > $dpi['y'] ? $dpi['x'] : $dpi['y'];
         $size = $img->getImageLength();
         $arrInsert[] = ['width' => $width, 'height' => $height, 'ratio' => $ratio, 'dpi' => $dpi, 'size' => $size, 'size_type' => $size_type, 'path' => $path, 'image_id' => $image->id, 'type' => '', 'extension' => 'jpeg'];
     }
     if (!empty($arrInsert)) {
         VIImageDetail::insert($arrInsert);
     }
     VIImageDetail::where('detail_id', $imageDetailId)->update($arrUpdate);
     $this->info(count($arrInsert) . ' image(s) has been inserted.');
 }