public function upload(UploadedFile $picture) { $original = pathinfo($picture->getClientOriginalName(), PATHINFO_FILENAME); $sanitize = preg_replace('/[^a-zA-Z0-9]+/', '-', $original); $fileName = $sanitize . '.' . $picture->getClientOriginalExtension(); $destination = public_path() . DIRECTORY_SEPARATOR . 'uploads/courses'; $uploaded = $picture->move($destination, $fileName); Image::make($uploaded)->fit(300, 300)->save($destination . '/300x300-' . $fileName); return $fileName; }
public static function upload(UploadedFile $file, $user, $contest = null) { $entry = new static(); DB::transaction(function () use($entry, $file, $user, $contest) { $entry->save(); // get id $entry->user()->associate($user); $entry->contest()->associate($contest); $entry->filesize = $file->getClientSize(); $entry->original_filename = $file->getClientOriginalName(); $entry->storeFile($file->getRealPath(), $file->getClientOriginalExtension()); $entry->save(); }); return $entry; }
/** * @param \Illuminate\Http\UploadedFile $file */ public function handleImageUpload($file) { // $file = $request->file('image'); or // $fileName = 'somename'; $destinationPath = public_path('uploads/mage2/themes'); $fileName = $file->getClientOriginalName(); $file->move($destinationPath, $fileName); return $destinationPath . DIRECTORY_SEPARATOR . $fileName; }
/** * @param Temp|UploadedFile|File $file * @param array $options * @return array * @throws \Exception */ public function manipulateImage($file, array $options) { // Detect file type if ($file instanceof Temp) { $sanitizedFilename = $this->sanitizeFilename($file->filename); } elseif ($file instanceof UploadedFile) { $sanitizedFilename = $this->sanitizeFilename($file->getClientOriginalName()); } elseif ($file instanceof File) { $actualFilename = str_replace('_source.', '.', $file->getFilename()); $sanitizedFilename = $this->sanitizeFilename($actualFilename); $sourceFile = clone $file; } else { throw new \Exception('Unexpected file of class ' . get_class($file)); } $images = []; $pathParts = pathinfo($sanitizedFilename); // Move uploaded file and rename it as source file if this is needed. // We need to generate unique name if the name is already in use. if (!isset($sourceFile)) { $filename = $pathParts['filename'] . '_source.' . $pathParts['extension']; $sourceFile = $file->move($this->getThumbnailPath(), $filename); } $images['original']['source'] = $sourceFile; // If we have retina factor if ($this->getRetinaFactor()) { // Generate retina image by just copying the source. $retinaFilename = $this->generateRetinaName($sanitizedFilename); FileFacade::copy($sourceFile->getRealPath(), $this->getThumbnailPath() . $retinaFilename); $images['original']['retina'] = Image::make($this->getThumbnailPath() . $retinaFilename); // The original image is scaled down version of the source. $originalImage = Image::make($sourceFile->getRealPath()); $width = round($originalImage->getWidth() / $this->getRetinaFactor()); $height = round($originalImage->getHeight() / $this->getRetinaFactor()); $originalImage->resize($width, $height); $images['original']['original_file'] = $originalImage->save($this->getThumbnailPath() . $sanitizedFilename); // Generate thumbs foreach ($options['thumbnails'] as $thumbnailName => $thumbnailOptions) { // Create retina thumb $images['thumbnails'][$thumbnailName]['retina'] = $this->createThumbnail($sourceFile->getRealPath(), $thumbnailName, $this->generateRetinaName($sanitizedFilename), $thumbnailOptions['width'] * $this->getRetinaFactor(), $thumbnailOptions['height'] * $this->getRetinaFactor(), $thumbnailOptions['type'], array_get($thumbnailOptions, 'color')); // Create original thumb $images['thumbnails'][$thumbnailName]['original'] = $this->createThumbnail($sourceFile->getRealPath(), $thumbnailName, $sanitizedFilename, $thumbnailOptions['width'], $thumbnailOptions['height'], $thumbnailOptions['type'], array_get($thumbnailOptions, 'color')); } } else { // Copy source file. $filename = $this->sanitizeFilename($file->getFilename()); FileFacade::copy($sourceFile->getRealPath(), $this->getThumbnailPath() . $filename); $images['original']['original_file'] = Image::make($this->getThumbnailPath() . $filename); // Generate thumbs foreach ($options['thumbnails'] as $thumbnailName => $thumbnailOptions) { // Create original thumb $images['thumbnails'][$thumbnailName]['original'] = $this->createThumbnail($sourceFile->getRealPath(), $thumbnailName, $sanitizedFilename, $thumbnailOptions['width'], $thumbnailOptions['height'], $thumbnailOptions['type']); } } return $images; }
public function upload(UploadedFile $file) { $original = Auth::user()->fullname . '-' . pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); $sanitize = preg_replace('/[^a-zA-Z0-9]+/', '-', $original); $fileName = $sanitize . '.' . $file->getClientOriginalExtension(); $destination = public_path() . DIRECTORY_SEPARATOR . 'uploads/assignments'; $uploaded = $file->move($destination, $fileName); return $fileName; }
/** * 上传文件信息 * * @param \Illuminate\Http\UploadedFile $file * @return array */ protected function getJosnFormatData(UploadedFile $file) { return ['original_name' => $file->getClientOriginalName(), 'mime_type' => $file->getClientMimeType(), 'extension' => $file->getClientOriginalExtension(), 'size' => $file->getClientSize()]; }