/** * Return the new item view. * * @param int $itemId * * @return \Illuminate\View\View */ public function getNew() { $categories = Category::leaves()->orderBy('category_id')->get(); if (!session()->has('photos')) { $session_photos = []; } else { foreach (session('photos') as $session_photo) { $session_photos[] = \Hamjoint\Mustard\Media\Photo::find($session_photo['photo_id']); } } return view('mustard::item.new', ['categories' => $categories, 'item' => new Item(), 'listing_durations' => ListingDuration::all(), 'item_conditions' => ItemCondition::all(), 'photos' => $session_photos]); }
/** * Delete a photo. * * @param \Illuminate\Http\Request $request * * @return \Illuminate\Http\Response */ public function postDeletePhoto(Request $request) { if (session()->has('photos')) { $photos = session('photos'); foreach ($photos as $index => $photo) { if (in_array($request->input('file'), $photo)) { $photo = Photo::find($photo['photo_id']); $photo->delete(); unset($photos[$index]); session('photos', $photos); return response('', 200); } } } return response('', 400); }
/** * Process a photo and create a record. * * @param string $file * * @return self */ public static function upload($file) { $photo = self::create(); $image_manager = new ImageManager(['driver' => 'imagick']); $disk = Storage::disk(config('mustard.storage.disk', 'local')); if (!file_exists($file)) { RuntimeException("File {$file} does not exist"); } $dest_dir = config('mustard.storage.photo.dir', 'mustard/photos'); $quality = config('mustard.storage.photo.quality', 90); $disk->makeDirectory($dest_dir); $photo_id = $photo->getKey(); Queue::push(function ($job) use($file, $quality, $dest_dir, $photo_id) { if (!file_exists($file)) { RuntimeException("File {$file} no longer exists"); } $image_manager = new ImageManager(['driver' => 'imagick']); $disk = Storage::disk(config('mustard.storage.disk', 'local')); if ($file != "{$dest_dir}/{$photo_id}.jpg") { $image = $image_manager->make($file); $disk->put("{$dest_dir}/{$photo_id}.jpg", (string) $image->encode('jpg', $quality)); $image->destroy(); } $image = $image_manager->make($file)->heighten(83); $disk->put("{$dest_dir}/{$photo_id}_s.jpg", (string) $image->encode('jpg', $quality)); $image->destroy(); $image = $image_manager->make($file)->widen(500); $disk->put("{$dest_dir}/{$photo_id}_l.jpg", (string) $image->encode('jpg', $quality)); $image->destroy(); $photo = Photo::find($photo_id); $photo->processed = true; $photo->save(); $job->delete(); }); return $photo; }