/** * Render the product photos * * @param integer $productId * @param string $type * @return mixed */ public function getProductPhoto($productId, $type = 'front') { $photo = ProductPhoto::where(['product_id' => $productId, 'type' => $type])->first(); if ($photo === null) { return Response::make('', 404); } return Image::make(Storage::disk('s3')->get($photo->location))->response(); }
/** * Upload photos to storage system. * * @param Illuminate\Http\Request $request * @return App\Product */ public function savePhotos(Request $request) { collect(['front', 'back', 'custom1', 'custom2'])->map(function ($name) use($request) { if (!$request->hasFile($name) || !$request->file($name)->isValid()) { return; } $content = file_get_contents($request->file($name)); $hash = md5($content); try { $photo = new ProductPhoto(); $photo->type = $name; $photo->product_id = $this->attributes['id']; $photo->location = $hash; Storage::disk()->put($hash, $content); $photo->save(); } catch (\Exception $e) { Log::error($e->getMessage()); } }); }
/** * Execute the job. * * @return void */ public function handle() { $dir = 'tmp/' . $this->_sessionId; if (!$this->exists($dir)) { Log::warning('No files uploaded for session ' . $this->_sessionId); return; } foreach (Storage::disk('local')->files($dir) as $file) { $type = pathinfo($file)['filename']; $content = file_get_contents(storage_path() . '/app/' . $file); $name = md5($content) . '.' . pathinfo($file, PATHINFO_EXTENSION); Storage::disk('s3')->put($name, $content); Storage::disk('local')->delete($file); $photo = new ProductPhoto(); $photo->type = $type; $photo->location = $name; $photo->product_id = $this->_productId; try { $photo->save(); } catch (Exception $e) { Log::error($e->getMessage()); } } }
<?php get('/', function () { return view('layouts.index'); }); resource('/dashboard', 'DashboardController'); resource('products', 'ProductController'); resource('product-types', 'ProductTypeController'); get('/product-photos', function () { return \App\ProductPhoto::all(); }); post('products/file-upload/{id}', 'ProductController@fileUpload');
protected function makePhoto($file) { return ProductPhoto::named($file->getClientOriginalName())->move($file); }
public function getPhoto() { $photos = ProductPhoto::where('id', '>', '1482')->get(); foreach ($photos as $key => $value) { echo "<img src='{$value->photo_url}' width='360px'>"; } }