Ejemplo n.º 1
2
 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;
 }
Ejemplo n.º 2
2
 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;
 }
Ejemplo n.º 3
0
 public function setFile(UploadedFile $file)
 {
     if (!$file->isValid()) {
         throw new UploadException();
     }
     $this->file = $file;
 }
Ejemplo n.º 4
0
 /**
  * @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;
 }
Ejemplo n.º 5
0
 /**
  * Move the avatar file into storage.
  *
  * @param UploadedFile $file
  * @param User         $user
  * @return static
  *
  * @author Cali
  */
 public static function move(UploadedFile $file, User $user)
 {
     $path = 'avatars/' . $user->id . '/' . $file->hashName();
     Storage::put($path, file_get_contents($file->getRealPath()));
     $avatar = new static(['type' => 0, 'src' => $path]);
     $user->avatars()->save($avatar);
     return $avatar;
 }
Ejemplo n.º 6
0
 /**
  * Check if uploaded file is uploadable and an image.
  *
  * @return bool
  */
 private function isUploadableImage()
 {
     return $this->file->isValid() && in_array($this->file->getMimeType(), ['image/gif', 'image/jpeg', 'image/png']);
 }
Ejemplo n.º 7
0
 /**
  * Convert the given array of Symfony UploadedFiles to custom Laravel UploadedFiles.
  *
  * @param  array  $files
  * @return array
  */
 protected function convertUploadedFiles(array $files)
 {
     return array_map(function ($file) {
         if (is_null($file) || is_array($file) && empty(array_filter($file))) {
             return $file;
         }
         return is_array($file) ? $this->convertUploadedFiles($file) : UploadedFile::createFromBase($file);
     }, $files);
 }
Ejemplo n.º 8
0
 private function generateFileName(UploadedFile $file)
 {
     $id = uniqid();
     return "{$id}.{$file->extension()}";
 }
Ejemplo n.º 9
0
 /**
  * @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;
 }
Ejemplo n.º 10
0
 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;
 }
Ejemplo n.º 11
0
 /**
  * Store the uploaded file on the disk with a given name.
  *
  * @param  string  $path
  * @param  \Illuminate\Http\File|\Illuminate\Http\UploadedFile  $file
  * @param  string  $name
  * @param  string  $visibility
  * @return string|false
  */
 public function putFileAs($path, $file, $name, $visibility = null)
 {
     $stream = fopen($file->getRealPath(), 'r+');
     $result = $this->put($path = trim($path . '/' . $name, '/'), $stream, $visibility);
     if (is_resource($stream)) {
         fclose($stream);
     }
     return $result ? $path : false;
 }
Ejemplo n.º 12
0
 /**
  * @param UploadedFile $file
  *
  * @return bool
  */
 protected function isImageUploadedFile(UploadedFile $file)
 {
     $size = getimagesize($file->getRealPath());
     return (bool) $size;
 }
Ejemplo n.º 13
0
 /**
  * Make sure files are \Illuminate\Http\UploadedFile instances with the private $test property set to true.
  * Fixes issue https://github.com/Codeception/Codeception/pull/3417.
  *
  * @param array $files
  * @return array
  */
 protected function filterFiles(array $files)
 {
     $files = parent::filterFiles($files);
     if (!class_exists('Illuminate\\Http\\UploadedFile')) {
         // The \Illuminate\Http\UploadedFile class was introduced in Laravel 5.2.15,
         // so don't change the $files array if it does not exist.
         return $files;
     }
     $filtered = [];
     foreach ($files as $key => $file) {
         $filtered[$key] = UploadedFile::createFromBase($file, true);
     }
     return $filtered;
 }
Ejemplo n.º 14
0
 public function saveImage(UploadedFile $image, $cover = false)
 {
     $fileName = Uuid::uuid1() . '.' . $image->getClientOriginalExtension();
     $destination = public_path() . DIRECTORY_SEPARATOR . 'uploads';
     $uploaded = $image->move($destination, $fileName);
     if ($cover) {
         Image::make($uploaded)->fit(253, 190)->save($destination . '/253x190-' . $fileName);
     } else {
         Image::make($uploaded)->fit(45, 45)->save($destination . '/45x45-' . $fileName);
         Image::make($uploaded)->fit(120, 120)->save($destination . '/120x120-' . $fileName);
     }
     return $fileName;
 }
Ejemplo n.º 15
0
 /**
  * Convert the given array of Symfony UploadedFiles to custom Laravel UploadedFiles.
  *
  * @param  array  $files
  * @return array
  */
 protected function convertUploadedFiles(array $files)
 {
     return array_map(function ($file) {
         return is_array($file) ? $this->convertUploadedFiles($file) : UploadedFile::createFromBase($file);
     }, $files);
 }
Ejemplo n.º 16
0
 /**
  * 上传文件信息
  *
  * @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()];
 }