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;
 }
Example #2
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.');
 }