/**
  * Create a new image handler.
  * Creates an image handler. This should never be done directly,
  * but only through the manager for configuration reasons. One can
  * get a direct reference through manager afterwards. When overwriting
  * the constructor.
  *
  * The contents of the $settings parameter may change from handler to 
  * handler. For detailed information take a look at the specific handler
  * classes.
  *
  * @param ezcImageHandlerSettings $settings Settings for the handler.
  */
 public function __construct(ezcImageHandlerSettings $settings)
 {
     parent::__construct($settings);
 }
예제 #2
0
 /**
  * Apply the given filters for the transformation.
  * Applies the conversion as defined to the given file and saves it as 
  * defined.
  *
  * @param string $fileIn  The file to transform.
  * @param string $fileOut The file to save the transformed image to.
  * @return void
  *
  * @throws ezcImageTransformationException If an error occurs during the 
  *         transformation. The returned exception contains the exception
  *         the problem resulted from in it's public $parent attribute.
  * @throws ezcBaseFileNotFoundException If the file you are trying to 
  *         transform does not exists.
  * @throws ezcBaseFilePermissionException If the file you are trying to 
  *         transform is not readable.
  */
 public function transform($fileIn, $fileOut)
 {
     // Sanity checks
     if (!is_file($fileIn)) {
         throw new ezcBaseFileNotFoundException($fileIn);
     }
     if (!is_readable($fileIn)) {
         throw new ezcBaseFilePermissionException($fileIn, ezcBaseFileException::READ);
     }
     // Start atomic file operation
     $fileTmp = tempnam(dirname($fileOut) . DIRECTORY_SEPARATOR, '.' . basename($fileOut));
     copy($fileIn, $fileTmp);
     try {
         // MIME types
         $analyzer = new ezcImageAnalyzer($fileTmp);
         // Do not process animated GIFs
         if ($analyzer->data->isAnimated) {
             copy($fileTmp, $fileOut);
             unlink($fileTmp);
             return;
         }
         $mimeIn = $analyzer->mime;
     } catch (ezcImageAnalyzerException $e) {
         // Clean up
         unlink($fileTmp);
         // Rethrow
         throw new ezcImageTransformationException($e);
     }
     $outMime = $this->getOutMime($fileTmp, $mimeIn);
     $ref = '';
     // Catch exceptions for cleanup
     try {
         // Apply the filters
         foreach ($this->filters as $filter) {
             // Avoid reopening in same handler
             if (isset($this->lastHandler)) {
                 if ($this->lastHandler->hasFilter($filter->name)) {
                     $this->lastHandler->applyFilter($ref, $filter);
                     continue;
                 } else {
                     // Handler does not support filter, save file
                     $this->lastHandler->save($ref);
                     $this->lastHandler->close($ref);
                 }
             }
             // Get handler to perform filter correctly
             $this->lastHandler = $this->converter->getHandler($filter->name, $mimeIn);
             $ref = $this->lastHandler->load($fileTmp, $mimeIn);
             $this->lastHandler->applyFilter($ref, $filter);
         }
         // When no filters are performed by a transformation, we might have no last handler here
         if (!isset($this->lastHandler)) {
             $this->lastHandler = $this->converter->getHandler(null, $mimeIn, $outMime);
             $ref = $this->lastHandler->load($fileTmp, $mimeIn);
         }
         // Perform conversion
         if ($this->lastHandler->allowsOutput($outMime)) {
             $this->lastHandler->convert($ref, $outMime);
         } else {
             // Close in last handler
             $this->lastHandler->save($ref);
             $this->lastHandler->close($ref);
             // Destroy invalid reference (has been closed)
             $ref = null;
             // Retreive correct handler
             $this->lastHandler = $this->converter->getHandler(null, $mimeIn, $outMime);
             // Load in new handler
             $ref = $this->lastHandler->load($fileTmp);
             // Perform conversion
             $this->lastHandler->convert($ref, $outMime);
         }
         // Everything done, save and close
         $this->lastHandler->save($ref, null, null, $this->saveOptions);
         $this->lastHandler->close($ref);
     } catch (ezcImageException $e) {
         // Cleanup
         if ($ref !== null) {
             $this->lastHandler->close($ref);
         }
         if (file_exists($fileTmp)) {
             unlink($fileTmp);
         }
         $this->lastHandler = null;
         // Rethrow
         throw new ezcImageTransformationException($e);
     }
     // Cleanup
     $this->lastHandler = null;
     // Finalize atomic file operation
     if (ezcBaseFeatures::os() === 'Windows' && file_exists($fileOut)) {
         // Windows does not allows overwriting files using rename,
         // therefore the file is unlinked here first.
         if (unlink($fileOut) === false) {
             // Cleanup
             unlink($fileTmp);
             throw new ezcImageFileNotProcessableException($fileOut, 'The file exists and could not be unlinked.');
         }
     }
     if (@rename($fileTmp, $fileOut) === false) {
         unlink($fileTmp);
         throw new ezcImageFileNotProcessableException($fileOut, "The temporary file {$fileTmp} could not be renamed to {$fileOut}.");
     }
 }