Exemplo n.º 1
0
 public function moveUploadedFile(UploadedFile $file, $uploadBasePath, $relativePath)
 {
     $originalName = $file->getFilename() . '.' . $file->guessExtension();
     $targetFileName = $relativePath . DIRECTORY_SEPARATOR . $originalName;
     $targetFilePath = $uploadBasePath . DIRECTORY_SEPARATOR . $targetFileName;
     $ext = $file->getExtension();
     $i = 1;
     while (file_exists($targetFilePath) && md5_file($file->getPath()) != md5_file($targetFilePath)) {
         if ($ext) {
             $prev = $i == 1 ? "" : $i;
             $targetFilePath = $targetFilePath . str_replace($prev . $ext, $i++ . $ext, $targetFilePath);
         } else {
             $targetFilePath = $targetFilePath . $i++;
         }
     }
     $targetDir = 'uploads';
     if (!is_dir($targetDir)) {
         $ret = mkdir($targetDir, 777, true);
         if (!$ret) {
             throw new \RuntimeException("Could not create target directory to move temporary file into.");
         }
     }
     $file->move($targetDir, basename($targetFilePath));
     return str_replace($uploadBasePath . DIRECTORY_SEPARATOR, "", $targetFilePath);
 }
Exemplo n.º 2
0
 public function moveUploadedFile(UploadedFile $file, $uploadBasePath, $relativePath, $fileName)
 {
     $originalName = $file->getFilename();
     // use filemtime() to have a more determenistic way to determine the subpath, otherwise its hard to test.
     // $relativePath = date('Y-m', filemtime($file->getPath()));
     $targetFileName = $relativePath . DIRECTORY_SEPARATOR . $originalName;
     $targetFilePath = $uploadBasePath . DIRECTORY_SEPARATOR . $targetFileName;
     $ext = $file->getExtension();
     $i = 1;
     while (file_exists($targetFilePath) && md5_file($file->getPath()) != md5_file($targetFilePath)) {
         if ($ext) {
             $prev = $i == 1 ? "" : $i;
             $targetFilePath = $targetFilePath . str_replace($prev . $ext, $i++ . $ext, $targetFilePath);
         } else {
             $targetFilePath = $targetFilePath . $i++;
         }
     }
     $targetDir = $uploadBasePath . DIRECTORY_SEPARATOR . $relativePath;
     if (!is_dir($targetDir)) {
         $ret = mkdir($targetDir, umask(), true);
         if (!$ret) {
             throw new \RuntimeException("Could not create target directory to move temporary file into.");
         }
     }
     //$file->move($targetDir, basename($targetFilePath));
     //$file->move($targetDir, basename($fileName.'.'.$ext));
     $file->move($targetDir, basename($fileName));
     return str_replace($uploadBasePath . DIRECTORY_SEPARATOR, "", $targetFilePath);
 }
Exemplo n.º 3
0
 /**
  * Upload file
  *
  * @param null $fileName
  * @return bool|null|string
  * @throws AttachModelException
  */
 public function uploadFile($fileName = null)
 {
     if (!$this->totalFile instanceof UploadedFile) {
         return false;
     }
     if (!class_exists('Imagick')) {
         throw new AttachModelException('The requested PHP extension ext-imagick * is missing from your system.');
     }
     if (null === $fileName) {
         if (null === ($ext = $this->totalFile->guessExtension())) {
             $ext = $this->totalFile->getExtension();
         }
         $fileName = sha1(uniqid(mt_rand(), true));
         if (null !== $ext) {
             $fileName .= '.' . $ext;
         }
     } else {
         if ($this->totalFile->getClientOriginalExtension()) {
             $fileName .= '.' . $this->totalFile->getClientOriginalExtension();
         } elseif ($this->totalFile->getExtension()) {
             $fileName .= '.' . $this->totalFile->getExtension();
         }
     }
     $fullUploadPath = $this->getFullUploadPath();
     if (!is_dir($fullUploadPath)) {
         if (!mkdir($fullUploadPath, 0775, true)) {
             throw new AttachModelException('Not possible create UploadDirectory: ' . $fullUploadPath);
         }
     }
     $move = false;
     if ($this->widthHeight) {
         $move = $this->resize($fullUploadPath, $this->getOriginalPrefix() . $fileName, $this->widthHeight);
         if ($this->widthHeightPreview) {
             $this->resize($fullUploadPath, $this->getPreviewPrefix() . $fileName, $this->widthHeightPreview);
         }
     }
     if (!$move) {
         $source = new \Imagick($this->totalFile->getRealPath());
         $source->writeImage($fullUploadPath . DIRECTORY_SEPARATOR . $this->getOriginalPrefix() . $fileName);
     }
     return $fileName;
 }
Exemplo n.º 4
0
 /**
  * Returns an array of data for upload model
  *
  * @param UploadedFile $uploadedFile
  * @return array
  */
 protected function prepareUploadData(UploadedFile $uploadedFile)
 {
     return ['extension' => $uploadedFile->getExtension(), 'mimetype' => $uploadedFile->getMimeType(), 'size' => $uploadedFile->getSize(), 'name' => $uploadedFile->getBasename('.' . $uploadedFile->getExtension())];
 }
 public function testGetExtension()
 {
     $file = new UploadedFile(__DIR__ . '/Fixtures/test.gif', 'original.gif', null);
     $this->assertEquals('gif', $file->getExtension());
 }
Exemplo n.º 6
0
 /**
  * Prepares the file location name using the file dispersion feature.
  *
  * @param  \Symfony\Component\HttpFoundation\File\UploadedFile|string  $file
  * @param  string  $destination
  * @return string
  */
 public function prepareFileLocation($file, $destination = null)
 {
     $manager = $this->getManager();
     $placeholders = array_merge($manager->getPlaceholders(), [' ' => '_']);
     if ($file instanceof UploadedFile) {
         $placeholders = array_merge($placeholders, [':extension' => $file->getExtension(), ':mime' => $file->getMimeType()]);
     }
     $destination = $file instanceof UploadedFile ? $destination : $file;
     $destination = $manager->getDispersion() . $destination;
     return str_replace(array_keys($placeholders), array_values($placeholders), $destination);
 }
Exemplo n.º 7
0
 /**
  * @param UploadedFile $uploadedFile
  */
 function __construct($uploadedFile)
 {
     $path = sha1(uniqid(mt_rand(), true)) . '.' . $uploadedFile->getExtension();
     $this->setPath($path);
     $uploadedFile->move($this->getUploadRootDir(), $path);
 }
Exemplo n.º 8
0
 public function upload(UploadedFile $file)
 {
     $fileName = $file->getBasename('.' . $file->getExtension()) . '.' . $file->guessExtension();
     $file->move($this->targetDir, $fileName);
     return $fileName;
 }
 /**
  * Crea la miniatura de imagenes
  * @param UploadedFile $file 
  * @param string $path 
  * @return string Nombre del nuevo archivo
  */
 public function createThumb($file, $path)
 {
     $ext = $file->getExtension();
     $search = $this->config["images"]["images_ext"];
     if (array_search($ext, $search) !== false) {
         $fullpath = $this->getFullPath() . $path;
         $fullpaththumb = $this->getFullPath() . '/_thumbs' . $path;
         $filename = $file->getFilename();
         $filename_new = $this->removeExtension($filename) . '-' . $this->config['images']['resize']['thumbWidth'] . 'x' . $this->config['images']['resize']['thumbHeight'] . '.' . $file->getExtension();
         $fullpaththumb_name = $this->getFullPath() . '/_thumbs' . $path . $filename_new;
         if ($this->config['debug']) {
             $this->_log(__METHOD__ . " - {$fullpaththumb}");
         }
         $filethumb = new Filesystem();
         if ($filethumb->exists($fullpaththumb_name) == false) {
             if ($this->config['debug']) {
                 $this->_log(__METHOD__ . " - " . $fullpaththumb_name);
             }
             if ($filethumb->exists($fullpaththumb) == false) {
                 $filethumb->mkdir($fullpaththumb);
             }
             Imagen::open($fullpath . $file->getFilename())->zoomCrop($this->config['images']['resize']['thumbWidth'], $this->config['images']['resize']['thumbHeight'])->save($fullpaththumb_name);
         }
         return $filename_new;
     } else {
         if ($this->config['debug']) {
             $this->_log(__METHOD__ . " - {$ext}");
         }
     }
 }
Exemplo n.º 10
0
 /**
  * @param UploadedFile $fileObject
  * @return array
  */
 private function getExifReadData(UploadedFile $fileObject)
 {
     Log::debug($fileObject->getPath());
     Log::debug($fileObject->getFilename());
     Log::debug($fileObject->getBasename());
     Log::debug($fileObject->getClientMimeType());
     Log::debug($fileObject->getExtension());
     Log::debug($fileObject->getType());
     $result = [];
     $mimeType = $fileObject->getClientMimeType();
     // upload_igm and thumbnail directory make
     $dirName = date('Ym');
     $uploadDir = public_path() . '/upload_img/' . $dirName;
     $thumbnailDir = public_path() . '/thumbnail/' . $dirName;
     $this->chkDir($uploadDir);
     $this->chkDir($thumbnailDir);
     //@todo オリジナル動画をリサイズする
     $extension = explode('/', $fileObject->getClientMimeType())[1];
     $newFileName = sha1($fileObject->getFilename() . time()) . '.' . $extension;
     $saveFileName = $uploadDir . '/' . $newFileName;
     $fileObject->move($uploadDir, $newFileName);
     $thumbnail = Image::make(file_get_contents($saveFileName));
     $thumbnail->resize(100, null, function ($constraint) {
         $constraint->aspectRatio();
     });
     $thumbnail->save($thumbnailDir . '/' . $newFileName, 100);
     $result['file_name'] = $dirName . '/' . $newFileName;
     if ($mimeType !== 'image/jpeg') {
         return $result;
     }
     $exif = exif_read_data($saveFileName);
     if ($exif === false) {
         return false;
     }
     $result['file_date_time'] = isset($exif['DateTimeOriginal']) ? $exif['DateTimeOriginal'] : null;
     $result['file_size'] = $exif['FileSize'];
     $result['mime_type'] = $exif['MimeType'];
     $result['width'] = $exif['COMPUTED']['Width'];
     $result['height'] = $exif['COMPUTED']['Height'];
     $result['make'] = isset($exif['Make']) ? $exif['Make'] : null;
     $result['model'] = isset($exif['Model']) ? $exif['Model'] : null;
     $result['orientation'] = isset($exif['Orientation']) ? $exif['Orientation'] : null;
     if (isset($exif["GPSLatitude"])) {
         //緯度を60進数から10進数に変換
         $lat = $this->convert_float($exif["GPSLatitude"][0]) + $this->convert_float($exif["GPSLatitude"][1]) / 60 + $this->convert_float($exif["GPSLatitude"][2]) / 3600;
         //南緯の場合はマイナスにする
         if ($exif["GPSLatitudeRef"] == "S") {
             $lat *= -1;
         }
         //経度を60進数から10進数に変換
         $lng = $this->convert_float($exif["GPSLongitude"][0]) + $this->convert_float($exif["GPSLongitude"][1]) / 60 + $this->convert_float($exif["GPSLongitude"][2]) / 3600;
         //西経の場合はマイナスにする
         if ($exif["GPSLongitudeRef"] == "W") {
             $lng *= -1;
         }
         $result['lat'] = $lat;
         $result['lng'] = $lng;
     }
     return $result;
 }