コード例 #1
0
 /**
  * Returns a cached thumbnail image.
  * $width, $height, $method='fit', $background=null
  * @return sfFilebasePluginImage $image
  */
 public function getThumbnail($width, $height, $method = 'fit', $background = null, $quality = 60, $mime = 'image/png')
 {
     $save_path = $this->filebase[$this->getFilebase()->getCacheDirectory()->getPathname() . '/' . $this->getFilename() . '_' . $width . '_' . $height . '_' . $quality . '.' . sfFilebasePluginUtil::getExtensionByMime($mime)];
     if ($save_path->fileExists()) {
         return $save_path;
     }
     $image = $this->thumbnail($width, $height, $method, $background)->setQuality($quality)->setMimeType($mime);
     return $image->saveAs($save_path);
 }
コード例 #2
0
 /**
  * Returns filename for a cached thumbnail, calculated
  * by its properties and dimensions.
  *
  * @param sfFilebasePluginFile $file
  * @param array $thumbnail_properties
  * @return sfFilebasePluginImage $filename
  */
 public function getThumbnailFileinfo(sfFilebasePluginImage $file, $dimensions, $mime)
 {
     $thumbnail_properties = $this->getScaledImageData($file, $dimensions);
     // Wrap in sfFilebasePluginImage because isImage may return false if file does not exist.
     return new sfFilebasePluginThumbnail($this->filebase->getFilebaseFile($this->filebase->getCacheDirectory() . DIRECTORY_SEPARATOR . $this->filebase->getHashForFile($file) . '_' . $thumbnail_properties['new_width'] . '_' . $thumbnail_properties['new_height'] . '.' . (sfFilebasePluginUtil::getExtensionByMime($mime) === null ? $thumbnail_properties['extension'] : sfFilebasePluginUtil::getExtensionByMime($mime))), $this->filebase, $file);
 }
コード例 #3
0
 $t->ok(sfFilebasePluginUtil::isAbsolutePathname('c:/hanswurst'));
 $t->ok(sfFilebasePluginUtil::isAbsolutePathname('c:\\hanswurst\\kaese'));
 $t->ok(sfFilebasePluginUtil::isAbsolutePathname('c:\\hanswurst\\kaese'));
 $t->diag('sfFilebasePluginUtil::realpath()');
 $t->ok($p = sfFilebasePluginUtil::realpath('/hanswurst'), $p);
 $t->ok($p = sfFilebasePluginUtil::realpath('/hanswurst/kaese'), $p);
 $t->ok($p = sfFilebasePluginUtil::realpath('c:/hanswurst/kaese'), $p);
 $t->ok($p = sfFilebasePluginUtil::realpath('c:/hanswurst'), $p);
 $t->ok($p = sfFilebasePluginUtil::realpath('c:\\hanswurst\\kaese'), $p);
 $t->ok($p = sfFilebasePluginUtil::realpath('c:\\hanswurst\\kaese'), $p);
 $t->ok(!($p = sfFilebasePluginUtil::realpath('c:\\..\\hanssimaus\\hanswurst\\kaese')), 'false');
 $t->ok($p = sfFilebasePluginUtil::realpath('c:\\hanssimaus\\hanswurst\\..\\kaese'), $p);
 $t->ok($p = sfFilebasePluginUtil::realpath('c:\\hanssimaus\\hanswurst\\würstelchen\\..\\..\\..\\kaese\\..'), $p);
 $t->ok($p = sfFilebasePluginUtil::realpath('/hanswurst/kaese/../'), $p);
 $t->ok($p = sfFilebasePluginUtil::realpath('hanswurst/kaese/../naseweis'), $p);
 $t->ok($p = sfFilebasePluginUtil::realpath('hanswurst/../kaese/../naseweis'), $p);
 # DO THE DOCTRINE BEHAVIOUR TEST. USE THIS ONLY IF THE MODEL IS GENERATED
 if (class_exists('testerer')) {
     $t->diag('Try something with the Doctrine-Behaviour');
     $f_test = new testerer();
     $t->isa_ok($f_test->setFile($i2), 'Doctrine_Template_File', 'Bound file to doctrine record object');
     try {
         $f_test->save();
         $t->pass('Saved doctrine record');
     } catch (Exception $e) {
         $t->fail('Failed saving doctrine record');
     }
     $t->isa_ok($f_test->getFile(), 'sfFilebasePluginImage', 'Doctrine_Template_File::getFile() returns sfFilebasePluginFile');
     $f_test->setFile($d3);
     $f_test->save();
     $t->isa_ok($f_test->getFile(), 'sfFilebasePluginDirectory', 'Set and get a sfFilebasePluginDirectory.');
コード例 #4
0
 /**
  * Returns a human readable string file type.
  * This could be for example "jpeg image" or "wma audio"
  * 
  * @return string
  */
 public function getHumanReadableFileType()
 {
     return sfFilebasePluginUtil::getStringTypeByExtension($this->getExtension());
 }
コード例 #5
0
 /**
  * Checks if this file is a child of the given filebase
  * directory.
  * @param mixed sfFilebaseDirectory | string $file
  */
 public function fileLiesWithin($file, $container)
 {
     $file = $this->getFilebaseFile($file);
     $container = $this->getFilebaseFile($container);
     // file = /path/to/lalala/bumsdi
     // container = /path/to
     if (strpos(sfFilebasePluginUtil::unifySlashes($file->getPathname()), sfFilebasePluginUtil::unifySlashes($container->getPathname()) . '/') === 0) {
         return true;
     }
     return false;
 }
コード例 #6
0
 /**
  * Returns true if sfFilebasePluginFile is an
  * <strong>web</strong> image file. Used to factory
  * a sfFilebasePluginImage instance by sfFilebasePlugin::
  * getFilebaseFile()
  *
  * @see sfFilebasePluginUtil::getIsWebImage()
  * @param sfFilebaseUtilFile $file
  * @return boolean true if file is an image.
  */
 public function getIsWebImage(sfFilebasePluginFile $file)
 {
     return sfFilebasePluginUtil::getIsWebImage($file);
 }
コード例 #7
0
 /**
  * This validator returns a sfFilebasePluginUploadedFile object.
  *
  * The input value must be an instance of sfFilebasePluginUploadedFile
  *
  * @see sfValidatorBase
  * @see sfFilebasePluginUploadedFile
  * @return sfFilebasePluginUploadedFile $uploaded_file
  */
 protected function doClean($value)
 {
     // Path name to save the file in
     $path_name = null;
     if ($value->hasError()) {
         switch ($value->getError()) {
             case sfFilebasePluginUploadedFile::UPLOAD_ERR_INI_SIZE:
                 $max = sfFilebasePluginUtil::getMaxUploadFileSize();
                 if ($this->getOption('max_size')) {
                     $max = min($max, $this->getOption('max_size'));
                 }
                 throw new sfValidatorError($this, 'max_size', array('max_size' => $max, 'size' => (int) $value['size']));
             case sfFilebasePluginUploadedFile::UPLOAD_ERR_FORM_SIZE:
                 throw new sfValidatorError($this, 'max_size', array('max_size' => 0, 'size' => (int) $value['size']));
             case sfFilebasePluginUploadedFile::UPLOAD_ERR_PARTIAL:
                 throw new sfValidatorError($this, 'partial');
             case sfFilebasePluginUploadedFile::UPLOAD_ERR_NO_TMP_DIR:
                 throw new sfValidatorError($this, 'no_tmp_dir');
             case sfFilebasePluginUploadedFile::UPLOAD_ERR_CANT_WRITE:
                 throw new sfValidatorError($this, 'cant_write');
             case sfFilebasePluginUploadedFile::UPLOAD_ERR_EXTENSION:
                 throw new sfValidatorError($this, 'extension');
         }
     }
     // check file size
     if ($this->hasOption('max_size') && $this->getOption('max_size') < (int) $value->getSize()) {
         throw new sfValidatorError($this, 'max_size', array('max_size' => $this->getOption('max_size'), 'size' => (int) $value->getSize()));
     }
     // Raw type the browser provided
     // @todo: Eventually check mime of tmp-file by Util as a fallback?
     $mimeType = $value->getType();
     // check mime type
     if ($this->hasOption('mime_types')) {
         $mimeTypes = is_array($this->getOption('mime_types')) ? $this->getOption('mime_types') : $this->getMimeTypesFromCategory($this->getOption('mime_types'));
         if (!in_array($mimeType, $mimeTypes)) {
             throw new sfValidatorError($this, 'mime_types', array('mime_types' => $mimeTypes, 'mime_type' => $mimeType));
         }
     }
     $path = $this->getOption('filebase')->getFilebaseFile($this->getOption('path'));
     $path_name = $this->getOption('filebase')->getFilebaseFile($path->getPathname() . '/' . $value->getOriginalName());
     if ($path_name->fileExists()) {
         if (!$this->getOption('allow_overwrite')) {
             throw new sfValidatorError($this, 'file_exists', array('file' => $path_name->getPathname()));
         }
     }
     $class_name = $this->getOption('validated_file_class');
     if (is_string($class_name)) {
         return $class_name == 'sfFilebasePluginUploadedFile' ? $value : new $class_name($path_name->getFilename(), $mimeType, $value->getTmpName()->getPathname(), $value->getSize(), $path_name->getPath());
     } else {
         return array('name' => $value->getOriginalName(), 'tmp_name' => $value->getTempName(), 'type' => $value->getType(), 'error' => $value->getError(), 'extension' => $value->getExtension());
     }
 }
 /**
  * Returns the file extension, based on the content type of the file.
  *
  * @param  string $default  The default extension to return if none was given
  *
  * @return string The extension (with the dot)
  */
 public function getExtension($default = '')
 {
     $f = $this->manager->getFilebase()->getFilebaseFile($this->getTempName());
     return ($mime = sfFilebasePluginUtil::getMimeType($f, null, ltrim($this->getOriginalExtension(), '.'))) === null ? $default : '.' . sfFilebasePluginUtil::getExtensionByMime($mime, $default);
 }
コード例 #9
0
 /**
  * Saves the uploaded file.
  *
  * This method can throw exceptions if there is a problem when saving the file.
  *
  * If you don't pass a file name, it will be generated by the generateFilename method.
  * This will only work if you have passed a path when initializing this instance.
  *
  * @param  string $file      The file path to save the file
  * @param  int    $fileMode  The octal mode to use for the new file
  * @param  bool   $create    Indicates that we should make the directory before moving the file
  * @param  int    $dirMode   The octal mode to use when creating the directory
  *
  * @return string The filename without the $this->path prefix
  *
  * @throws Exception
  */
 public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777)
 {
     $filebase = null;
     if ($file === null) {
         if ($this->originalName === null) {
             $file = $this->generateFilename();
         } else {
             $file = $this->originalName;
         }
     }
     // Check if $file contains an absolute pathname
     if (sfFilebasePluginUtil::isAbsolutePathname($file)) {
         $fb = sfFilebasePlugin::getInstance();
         $f = $fb->getFilebaseFile($file);
         $filebase = sfFilebasePlugin::getInstance($f->getPath(), null, $create);
     } else {
         if ($this->path === null) {
             $filebase = sfFilebasePlugin::getInstance(null, null, $create);
         } else {
             $filebase = sfFilebasePlugin::getInstance($this->path, null, $create);
         }
     }
     $file = $filebase->getFilebaseFile($file);
     // copy the temp file to the destination file
     $f = $filebase->copyFile($this->getTempName(), $file->getPathname());
     // chmod our file
     $file->chmod($fileMode);
     $this->savedName = $file->getPathname();
     return $file;
 }
コード例 #10
0
 /**
  * Rotates an image to $deg degree
  *
  * @uses  imagerotate():     Hinweis: Diese Funktion steht nur zur Verfügung, wenn PHP mit der GD Bibliothek übersetzt wurde, die mit PHP zusammen erhältlich ist.
  * @todo Implement fallback. Its a performance issue.
  * @experimental
  * @param float  $deg: The amount to rotate
  * @param string $bgcolor: The background color in html hexadecimal notation
  * @return sfFilebasePluginImage $image: THe rotated image
  */
 public function rotate($deg, $bgcolor)
 {
     $bgcolor = 0;
     if (!function_exists('imagerotate')) {
         throw new sfFilebasePluginException('Imagerotate() is not supported by your gd-version, you must compile php with the pre-built gd2-library.');
     }
     if (!is_resource($this->source_resource) || !$this->destination instanceof sfFilebasePluginImage) {
         throw new sfFilebasePluginException('You must set a source and a destination image to resize.');
     }
     $this->destination_resource = imagerotate($this->source_resource, $deg, sfFilebasePluginUtil::parseHTMLColor($bgcolor), true);
     if (!is_resource($this->destination_resource)) {
         throw new sfFilebasePluginException(sprintf('Failed to rotate image %s.', $this->source));
     }
     return true;
 }