Example #1
0
 /**
  * Writes the provided data to the file
  * 
  * Requires all previous data to be stored in memory if inside a
  * transaction, use with caution on large files!
  * 
  * If a filesystem transaction is in progress and is rolled back, the
  * previous data will be restored.
  * 
  * @param  mixed $data  The data to write to the file
  * @return fFile  The file object, to allow for method chaining
  */
 public function write($data)
 {
     $this->tossIfDeleted();
     if (!$this->isWritable()) {
         throw new fEnvironmentException('This file, %s, can not be written to because it is not writable', $this->file);
     }
     // Allow filesystem transactions
     if (fFilesystem::isInsideTransaction()) {
         fFilesystem::recordWrite($this);
     }
     file_put_contents($this->file, $data);
     return $this;
 }
Example #2
0
 /**
  * Saves any changes to the image
  * 
  * If the file type is different than the current one, removes the current
  * file once the new one is created.
  * 
  * This operation will be reverted by a filesystem transaction being rolled
  * back. If a transaction is in progress and the new image type causes a
  * new file to be created, the old file will not be deleted until the
  * transaction is committed.
  * 
  * @param  string  $new_image_type  The new file format for the image: 'NULL` (no change), `'jpg'`, `'gif'`, `'png'`
  * @param  integer $jpeg_quality    The quality setting to use for JPEG images - this may be ommitted
  * @param  boolean $overwrite       If an existing file with the same name and extension should be overwritten
  * @param  string  :$new_image_type
  * @param  boolean :$overwrite
  * @return fImage  The image object, to allow for method chaining
  */
 public function saveChanges($new_image_type = NULL, $jpeg_quality = 90, $overwrite = FALSE)
 {
     // This allows ommitting the $jpeg_quality parameter, which is very useful for non-jpegs
     $args = func_get_args();
     if (count($args) == 2 && is_bool($args[1])) {
         $overwrite = $args[1];
         $jpeg_quality = 90;
     }
     $this->tossIfDeleted();
     self::determineProcessor();
     if (self::$processor == 'none') {
         throw new fEnvironmentException("The changes to the image can't be saved because neither the GD extension or ImageMagick appears to be installed on the server");
     }
     $type = self::getImageType($this->file);
     if ($type == 'tif' && self::$processor == 'gd') {
         throw new fEnvironmentException('The image specified, %s, is a TIFF file and the GD extension can not handle TIFF files. Please install ImageMagick if you wish to manipulate TIFF files.', $this->file);
     }
     $valid_image_types = array('jpg', 'gif', 'png');
     if ($new_image_type !== NULL && !in_array($new_image_type, $valid_image_types)) {
         throw new fProgrammerException('The new image type specified, %1$s, is invalid. Must be one of: %2$s.', $new_image_type, join(', ', $valid_image_types));
     }
     if (is_numeric($jpeg_quality)) {
         $jpeg_quality = (int) round($jpeg_quality);
     }
     if (!is_integer($jpeg_quality) || $jpeg_quality < 1 || $jpeg_quality > 100) {
         throw new fProgrammerException('The JPEG quality specified, %1$s, is either not an integer, less than %2$s or greater than %3$s.', $jpeg_quality, 1, 100);
     }
     if ($new_image_type && fFilesystem::getPathInfo($this->file, 'extension') != $new_image_type) {
         if ($overwrite) {
             $path_info = fFilesystem::getPathInfo($this->file);
             $output_file = $path_info['dirname'] . $path_info['filename'] . '.' . $new_image_type;
         } else {
             $output_file = fFilesystem::makeUniqueName($this->file, $new_image_type);
         }
         if (file_exists($output_file)) {
             if (!is_writable($output_file)) {
                 throw new fEnvironmentException('Changes to the image can not be saved because the file, %s, is not writable', $output_file);
             }
         } else {
             $output_dir = dirname($output_file);
             if (!is_writable($output_dir)) {
                 throw new fEnvironmentException('Changes to the image can not be saved because the directory to save the new file, %s, is not writable', $output_dir);
             }
         }
     } else {
         $output_file = $this->file;
         if (!is_writable($output_file)) {
             throw new fEnvironmentException('Changes to the image can not be saved because the file, %s, is not writable', $output_file);
         }
     }
     // If we don't have any changes and no name change, just exit
     if (!$this->pending_modifications && $output_file == $this->file) {
         return $this;
     }
     // Wrap changes to the image into the filesystem transaction
     if ($output_file == $this->file && fFilesystem::isInsideTransaction()) {
         fFilesystem::recordWrite($this);
     }
     if (self::$processor == 'gd') {
         $this->processWithGD($output_file, $jpeg_quality);
     } elseif (self::$processor == 'imagemagick') {
         $this->processWithImageMagick($output_file, $jpeg_quality);
     }
     $old_file = $this->file;
     fFilesystem::updateFilenameMap($this->file, $output_file);
     // If we created a new image, delete the old one
     if ($output_file != $old_file) {
         $old_image = new fImage($old_file);
         $old_image->delete();
     }
     $this->pending_modifications = array();
     return $this;
 }