/** * @param $userId * * @return Image */ public static function getImage($userId) { $url = self::getUrl(); $imageProcessor = new ImageProcessor(); $filename = $imageProcessor->storeFromUrl($url, 'avatars'); $image = new Image(); $image->userId = $userId; $image->directory = 'avatars'; $image->filename = $filename; $image->save(); return $image->fresh(); }
/** * @api {post} /images Upload An Image * @apiGroup Images * @apiDescription Save an image. * @apiUse RequiresAuthentication * * @param Request $request * * @return \Illuminate\Http\Response */ public function store(Request $request) { $this->requireAuthentication(); $this->validate($request, ['file' => 'required|image']); $image = $request->file('file'); $processor = new ImageProcessor($image); $filename = $processor->storeUserUploadedImage(); $image = new Image(['userId' => $this->user->id, 'filename' => $filename]); $image->save(); /** @var Image $image */ $image = $image->fresh(); return $this->response(['uploaded' => 1, 'filename' => $filename, 'url' => $image->getUrl(), 'image' => $image]); }
/** * @param string|array $imageIds * * @return Image[] * @throw HttpException */ protected function validateImages($imageIds) { if (!is_array($imageIds)) { $imageIds = explode(',', $imageIds); } $images = []; foreach ($imageIds as $imageId) { /** @var Image $image */ $image = Image::find($imageId); if (!$image) { throw new HttpException(404, "Image ({$imageId}) not found."); } if ($image->userId !== $this->user->id) { throw new HttpException(403, "User does not own image ({$imageId})."); } $images[] = $image; } return $images; }
public function makeUploadedImageThumbnail(Image $image) { // Download the previously saved image and make a thumbnail of it $contents = $this->storage->get($image->directory . '/' . $image->filename); if (!$contents) { return false; } // Temp file to store the downloaded image $tmpFile = $this->makeTmpFile(); // Save downloaded image locally file_put_contents($tmpFile->getPathname(), $contents); // and set it as the working image $this->file = $tmpFile; // Make a smaller version for showing on site $thumbFile = $this->makeThumbnailImage(100, 100); // Copy to S3 $this->storage->getDriver()->put($image->directory . 'thumbs/' . $image->filename, file_get_contents($thumbFile->getPathname()), ['StorageClass' => 'REDUCED_REDUNDANCY']); // Delete the local temp thumb unlink($thumbFile->getPathname()); // Update the image so it has a thumb $image->thumb = true; $image->save(); return true; }