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 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]); }
/** * 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).'); }
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.'); }