/** * Transport the file to a remote location. * * @param \Transit\File $file * @return string * @throws \Transit\Exception\TransportationException */ public function transport(File $file) { $config = $this->_config; $key = ltrim($config['folder'], '/') . $file->basename(); $response = null; // If larger then 100MB, split upload into parts if ($file->size() >= 100 * Size::MB) { $uploader = UploadBuilder::newInstance()->setClient($this->getClient())->setSource($file->path())->setBucket($config['bucket'])->setKey($key)->setMinPartSize(10 * Size::MB)->build(); try { $response = $uploader->upload(); } catch (MultipartUploadException $e) { $uploader->abort(); } } else { $response = $this->getClient()->putObject(array_filter(array('Key' => $key, 'Bucket' => $config['bucket'], 'Body' => EntityBody::factory(fopen($file->path(), 'r')), 'ACL' => $config['acl'], 'ContentType' => $file->type(), 'ServerSideEncryption' => $config['encryption'], 'StorageClass' => $config['storage'], 'Metadata' => $config['meta']))); } // Return S3 URL if successful if ($response) { $file->delete(); return sprintf('%s/%s/%s', S3Client::getEndpoint($this->getClient()->getDescription(), $config['region'], $config['scheme']), $config['bucket'], $key); } throw new TransportationException(sprintf('Failed to transport %s to Amazon S3', $file->basename())); }
/** * Rename or move the file and return its relative path. * * @param Model $model * @param \Transit\File $file * @param array $options * @return string */ protected function _renameAndMove(Model $model, File $file, array $options) { $nameCallback = null; if ($options['nameCallback'] && method_exists($model, $options['nameCallback'])) { $nameCallback = array($model, $options['nameCallback']); } $file->rename($nameCallback, $options['append'], $options['prepend']); if ($options['uploadDir']) { $file->move($options['uploadDir'], $options['overwrite']); } return (string) $options['finalPath'] . $file->basename(); }
/** * Transport the file to Amazon Glacier and return the archive ID. * * @param \Transit\File $file * @return string * @throws \Transit\Exception\TransportationException */ public function transport(File $file) { $config = $this->_config; $response = null; // If larger then 100MB, split upload into parts if ($file->size() >= 100 * Size::MB) { $uploader = UploadBuilder::newInstance()->setClient($this->getClient())->setSource($file->path())->setVaultName($config['vault'])->setAccountId($config['accountId'] ?: '-')->setPartSize(10 * Size::MB)->build(); try { $response = $uploader->upload(); } catch (MultipartUploadException $e) { $uploader->abort(); } } else { $response = $this->getClient()->uploadArchive(array_filter(array('vaultName' => $config['vault'], 'accountId' => $config['accountId'], 'body' => EntityBody::factory(fopen($file->path(), 'r'))))); } // Return archive ID if successful if ($response) { $file->delete(); return $response->getPath('archiveId'); } throw new TransportationException(sprintf('Failed to transport %s to Amazon Glacier', $file->basename())); }
/** * Transform the image using the defined options. * * @param \Transit\File $file * @param array $options * @return \Transit\File * @throws \DomainException */ protected function _process(File $file, array $options) { if (!$file->isImage()) { throw new DomainException(sprintf('%s is not a valid image', $file->basename())); } $sourcePath = $file->path(); $mimeType = $file->type(); // Create an image to work with switch ($mimeType) { case 'image/gif': $sourceImage = imagecreatefromgif($sourcePath); break; case 'image/png': $sourceImage = imagecreatefrompng($sourcePath); break; case 'image/jpg': case 'image/jpeg': case 'image/pjpeg': $sourceImage = imagecreatefromjpeg($sourcePath); break; default: throw new DomainException(sprintf('%s can not be transformed', $mimeType)); break; } // Gather options $options = $options + array('dest_x' => 0, 'dest_y' => 0, 'dest_w' => null, 'dest_h' => null, 'source_x' => 0, 'source_y' => 0, 'source_w' => $file->width(), 'source_h' => $file->height(), 'quality' => 100, 'overwrite' => false, 'target' => ''); $targetImage = imagecreatetruecolor($options['dest_w'], $options['dest_h']); // If gif/png allow transparencies if ($mimeType === 'image/gif' || $mimeType === 'image/png') { imagealphablending($targetImage, false); imagesavealpha($targetImage, true); imagefilledrectangle($targetImage, 0, 0, $options['dest_w'], $options['dest_h'], imagecolorallocatealpha($targetImage, 255, 255, 255, 127)); } // Lets take our source and apply it to the temporary file and resize imagecopyresampled($targetImage, $sourceImage, $options['dest_x'], $options['dest_y'], $options['source_x'], $options['source_y'], $options['dest_w'], $options['dest_h'], $options['source_w'], $options['source_h']); // Now write the transformed image to the server if ($options['overwrite']) { $options['target'] = $file->name(); } else { if (!$options['target']) { $class = explode('\\', get_class($this)); $class = str_replace('transformer', '', strtolower(end($class))); $options['target'] = sprintf('%s-%s-%sx%s', $file->name(), $class, round($options['dest_w']), round($options['dest_h'])); } } $targetPath = sprintf('%s%s.%s', $file->dir(), $options['target'], $file->ext()); switch ($mimeType) { case 'image/gif': imagegif($targetImage, $targetPath); break; case 'image/png': imagepng($targetImage, $targetPath); break; case 'image/jpg': case 'image/jpeg': case 'image/pjpeg': imagejpeg($targetImage, $targetPath, $options['quality']); break; } // Clear memory imagedestroy($sourceImage); imagedestroy($targetImage); return new File($targetPath); }