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);
     }
 }
예제 #2
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;
 }