Permalink
Browse files

Added thumbnail creation for pdf files and graphic formats supported …

…by ImageMagick.
  • Loading branch information...
1 parent fcdf964 commit fd4ea1be1481c68cf3df35204b2f610c84443051 @mgiagnoni committed Oct 27, 2010
View
2 README
@@ -115,6 +115,8 @@ Some configuration parameters can be set inside the `app.yml` file of your appli
allowed_extensions: [jpg,png,gif,txt]
# only files of these mime-types can be uploaded
mime_types: [image/jpeg,image/png,image/gif,text/plain]
+ # libray used for thumbnail creation: true ImageMagick, false (default) GD
+ use_ImageMagick: false
Note: `small` and `medium` size thumbnails are used by the plugin itself and should not be removed. More thumbnails of desired sizes can be created for use in your application.
View
21 lib/lyMediaFileSystem.class.php
@@ -140,10 +140,11 @@ public function mkdir($dir)
* Renames (or moves) a file and related thumbnails.
*
* @param string $src path of source file (can be relative to web dir).
- * @param string $dest detination path (can be relative to web dir).
- * @param bool $thumbs, if true thumbnails are also moved/renamed.
+ * @param string $dest destination path (can be relative to web dir).
+ * @param string $src_thumb, source thumbnail file, null if thumbnails are not supported.
+ * @param string $dest_thumb destination thumbnail file.
*/
- public function rename($src, $dest, $thumbs = false)
+ public function rename($src, $dest, $src_thumb = null, $dest_thumb = null)
{
$src = $this->makePathAbsolute($src);
$dest = $this->makePathAbsolute($dest);
@@ -154,7 +155,7 @@ public function rename($src, $dest, $thumbs = false)
}
rename($src, $dest);
- if($thumbs)
+ if($src_thumb)
{
$src_info = pathinfo($src);
$dest_info = pathinfo($dest);
@@ -168,8 +169,8 @@ public function rename($src, $dest, $thumbs = false)
}
foreach($this->getThumbnailTypes() as $key)
{
- $src = $src_path . $key . '_' . $src_info['basename'];
- $dest = $dest_path . $key . '_' . $dest_info['basename'];
+ $src = $src_path . $key . '_' . $src_thumb;
+ $dest = $dest_path . $key . '_' . $dest_thumb;
if(file_exists($src) && !file_exists($dest))
{
@@ -206,20 +207,20 @@ public function rmdir($dir, $rm_thumbs = true)
* Deletes a file and related thumbnails.
*
* @param string $file path of the file to delete (can be relative to web dir).
- * @param bool $thumbs, if true thubnail files are also deleted.
+ * @param string $thumb_file, thumbnail file, null if thumbnails are not supported.
*/
- public function unlink($file, $thumbs = false)
+ public function unlink($file, $thumb_file = null)
{
$file = $this->makePathAbsolute($file);
- if($thumbs)
+ if($thumb_file)
{
$info = pathinfo($file);
$path = $info['dirname'] . DIRECTORY_SEPARATOR . lyMediaThumbnails::getThumbnailFolder() . DIRECTORY_SEPARATOR;
foreach($this->getThumbnailTypes() as $key)
{
- $tfile = $path . $key . '_' . $info['basename'];
+ $tfile = $path . $key . '_' . $thumb_file;
if(file_exists($tfile))
{
View
80 lib/lyMediaThumbnails.class.php
@@ -18,22 +18,25 @@
*/
class lyMediaThumbnails
{
- protected $folder;
- protected $file;
+ protected $source;
+ protected $thumb_file;
protected $settings;
+ protected $mime_type;
/**
* Constructor.
*
- * @param string $src_folder source folder path
- * @param string $src_file source file name (image file from which thumbnails will be generated).
+ * @param string $source source file path (image file from which thumbnails will be generated).
+ * @param string $mime thumbnail mime-type
+ * @param string $thumb_file thumbnail filename (without type prefix)
*/
- public function __construct($src_folder, $src_file)
+ public function __construct($source, $mime, $thumb_file)
{
$fs = new lyMediaFileSystem();
- $this->folder = $fs->makePathAbsolute($src_folder);
- $this->file = $src_file;
+ $this->source = $fs->makePathAbsolute($source);
+ $this->thumb_file = $thumb_file;
+ $this->mime_type = $mime;
$this->settings = self::getThumbnailSettings();
}
@@ -68,14 +71,13 @@ public static function getThumbnailFolder()
*/
public function getThumbnailPath($thumb_type, $create = true)
{
- $folder = $this->folder . self::getThumbnailFolder();
-
+ $folder = pathinfo($this->source, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR . self::getThumbnailFolder();
if($create && !file_exists($folder))
{
$fs = new lyMediaFileSystem();
$fs->mkdir($folder);
}
- return $folder . DIRECTORY_SEPARATOR . $thumb_type . '_' . $this->file;
+ return $folder . DIRECTORY_SEPARATOR . $thumb_type . '_' . $this->thumb_file;
}
/**
@@ -109,47 +111,33 @@ public static function getThumbnailSettings()
/**
* Generate a thumbnail of a given type.
*
- * @param <type> $thumb_type thumbnail type
- * @param <type> $thumb_options thumbnail options (width, height and other as set in configuration).
+ * @param string $thumb_type thumbnail type
+ * @param array $thumb_options thumbnail options (width, height and other as set in configuration).
* @return bool true = success, false = failure.
*/
protected function generateThumbnail($thumb_type, $thumb_options)
{
- $source = $this->folder . $this->file;
- $dest = $this->getThumbnailPath($thumb_type);
- $width = $thumb_options['width'];
- $height = $thumb_options['height'];
- $shave_all = isset($thumb_options['shave']) ? $thumb_options['shave'] : false;
-
- if (class_exists('sfThumbnail') && file_exists($source))
+ if (!class_exists('sfThumbnail') || !file_exists($this->source))
+ {
+ return false;
+ }
+
+ $width = isset($thumb_options['width']) ? $thumb_options['width'] : null;
+ $height = isset($thumb_options['height']) ? $thumb_options['height'] : null;
+ $options = array('extract' => 1);
+ $scale = true;
+ if (isset($thumb_options['shave']) && $thumb_options['shave'] == true)
{
- if (sfConfig::get('app_lyMediaManager_use_ImageMagick', false))
- {
- $adapter = 'sfImageMagickAdapter';
- $mime = 'image/jpg';
- }
- else
- {
- $adapter = 'sfGDAdapter';
- $mime = 'image/jpeg';
- }
- if ($shave_all)
- {
- $thumbnail = new sfThumbnail($width, $height, false, true, 85, $adapter, array('method' => 'shave_all'));
- $thumbnail->loadFile($source);
- $thumbnail->save($dest, $mime);
- return true;
- }
- else
- {
- list($w, $h, $type, $attr) = getimagesize($source);
- $newHeight = $width ? ceil(($width * $h) / $w) : $height;
- $thumbnail = new sfThumbnail($width, $newHeight, true, true, 85, $adapter);
- $thumbnail->loadFile($source);
- $thumbnail->save($dest, $mime);
- return true;
- }
+ $options = array_merge(array('method' => 'shave_all'), $options);
+ $scale = false;
}
- return false;
+ $thumbnail = new sfThumbnail($width, $height,
+ $scale, true, 85,
+ sfConfig::get('app_lyMediaManager_use_ImageMagick', false) ? 'sfImageMagickAdapter' : 'sfGDAdapter',
+ $options
+ );
+ $thumbnail->loadFile($this->source);
+ $thumbnail->save($this->getThumbnailPath($thumb_type), $this->mime_type);
+ return true;
}
}
View
62 lib/model/doctrine/PluginlyMediaAsset.class.php
@@ -40,7 +40,11 @@ public function generateThumbnails()
{
return false;
}
- $tn = new lyMediaThumbnails($this->getFolderPath(), $this->getFilename());
+ $tn = new lyMediaThumbnails(
+ $this->getPath(),
+ in_array($this->getType(), array('image/png','image/gif')) ? $this->getType() : 'image/jpeg',
+ $this->getThumbnailFile(null)
+ );
$tn->generate();
return true;
}
@@ -66,7 +70,28 @@ public function getFolderPath()
}
/**
- * Returns asset thumbnail filename.
+ * Returns asset thumbnail extension.
+ *
+ * @return string
+ */
+ public function getThumbnailExtension()
+ {
+ switch($this->getType())
+ {
+ case 'image/png':
+ $ext = '.png';
+ break;
+ case 'image/gif':
+ $ext = '.gif';
+ break;
+ default:
+ $ext = '.jpg';
+ }
+ return $ext;
+ }
+
+ /**
+ * Returns asset thumbnail filename for a given thumbnail type.
*
* @param string $thumb_type thumbnail type.
*/
@@ -76,7 +101,7 @@ public function getThumbnailFile($thumb_type = 'small')
if($this->supportsThumbnails())
{
- $thumbnail = $thumb_type . '_' . $this->getFilename();
+ $thumbnail = $this->buildThumbnailFile($this->getFilename(), $thumb_type);
}
else
{
@@ -119,8 +144,8 @@ public function getThumbnailFile($thumb_type = 'small')
public function postDelete($event)
{
$record = $event->getInvoker();
- $fs = new lymediaFileSystem();
- $fs->unlink($record->getPath(), $record->supportsThumbnails());
+ $fs = new lyMediaFileSystem();
+ $fs->unlink($record->getPath(), $record->supportsThumbnails() ? $record->getThumbnailFile(null) : null);
}
/**
@@ -164,8 +189,17 @@ public function preSave($event)
$src = $src_folder->getRelativePath() . (isset($modified['filename']) ? $modified['filename'] : $record->getFileName());
$dest = $dest_folder->getRelativePath() . $record->getFileName();
+ $src_thumb = $dest_thumb = null;
+ if($record->supportsThumbnails())
+ {
+ $src_thumb = $dest_thumb = $this->buildThumbnailFile($record->getFilename(), null);
+ if(isset($modified['filename']))
+ {
+ $src_thumb = $this->buildThumbnailFile($modified['filename'], null);
+ }
+ }
$fs = new lyMediaFileSystem();
- $fs->rename($src, $dest, $record->supportsThumbnails());
+ $fs->rename($src, $dest, $src_thumb, $dest_thumb);
}
}
}
@@ -186,4 +220,20 @@ public function supportsThumbnails()
'image/gif'
)));
}
+
+ protected function buildThumbnailFile($fname, $thumb_type = 'small')
+ {
+ $info = pathinfo($fname);
+ $ext = $this->getThumbnailExtension();
+ if(ltrim($ext, '.') != $info['extension'])
+ {
+ $ext = '.' . $info['extension'] . $ext;
+ }
+ $thumb_fname = $info['filename'] . $ext;
+ if($thumb_type)
+ {
+ $thumb_fname = $thumb_type . '_' . $thumb_fname;
+ }
+ return $thumb_fname;
+ }
}
View
27 lib/test/lyMediaTestFunctional.class.php
@@ -24,19 +24,19 @@ class lyMediaTestFunctional extends sfTestFunctional
*
* @see checkFile()
*/
- public function isFile($folder, $file, $check_thumbs = true)
+ public function isFile($asset)
{
- return $this->checkFile($folder, $file, $check_thumbs);
+ return $this->checkFile($asset);
}
/**
* Checks file non existence in filesystem.
*
* @see checkFile()
*/
- public function isntFile($folder, $file, $check_thumbs = true)
+ public function isntFile($asset)
{
- return $this->checkFile($folder, $file, $check_thumbs, false);
+ return $this->checkFile($asset, false);
}
/**
@@ -60,23 +60,26 @@ public function saveEdit($class, $data)
}
/**
- * Checks if asset file and thumbnails exist in filesystem.
+ * Checks if asset file and asset thumbnails (if supported) exist in filesystem.
*
- * @param string $folder asset folder (relative path)
- * @param string $file asset filename
- * @param boolean $check_thumbs check existence/non-existence of thumbnail files
+ * @param lyMediaAsset $asset
* @param boolean $exist true=must exist, false=must not exist
*
* @return lyMediaTestFunctional current lyMediaTestFunctional instance
*/
- protected function checkFile($folder, $file, $check_thumbs = true, $exist = true)
+ protected function checkFile($asset, $exist = true)
{
$fs = new lyMediaFileSystem();
- $this->test()->is($fs->is_file($folder . $file), $exist, 'File ' . $folder . $file . ($exist ? ' has ' : ' has not ') . 'been found');
+ $file_path = $asset->getPath();
+ $this->test()->is($fs->is_file($file_path), $exist, 'File ' . $file_path . ($exist ? ' has ' : ' has not ') . 'been found');
- if($check_thumbs)
+ if($asset->supportsThumbnails())
{
- $tn = new lyMediaThumbnails($folder, $file);
+ $tn = new lyMediaThumbnails(
+ $file_path,
+ in_array($asset->getType(), array('image/png','image/gif')) ? $asset->getType() : 'image/jpeg',
+ $asset->getThumbnailFile(null)
+ );
foreach($tn->getThumbnailPaths() as $file_path)
{
$this->test()->is($fs->is_file($file_path), $exist, 'Thumbnail ' . basename($file_path) . ($exist ? ' has ' : ' has not ') . 'been found');
View
24 test/functional/lyMediaManagerActionsTest.php
@@ -177,11 +177,9 @@
$asset = lyMediaAssetTable::getInstance()
->findOneByTitle('test');
+$old = clone $asset;
-$folder = $asset->getFolderPath();
-$file = $asset->getFilename();
-
-$browser->isFile($folder, $file)->
+$browser->isFile($asset)->
info('5 - Rename asset')->
saveEdit('lyMediaAsset', array('ly_media_asset' => array(
'filename' => 'asset1_renamed.png'
@@ -199,11 +197,12 @@
info(' 5.2 - Check renamed asset files')->
info(' 5.2.1 - Old filename must not exist')->
- isntFile($folder, $file)->
+ isntFile($old)->
info(' 5.2.2 - New filename must exist')->
- isFile($folder, 'asset1_renamed.png')->
-
- info('6 - Move asset')->
+ isFile($asset)
+;
+$old = clone $asset;
+$browser-> info('6 - Move asset')->
saveEdit('lyMediaAsset', array('ly_media_asset' => array(
'folder_id' => $subf2->getId()
)))->
@@ -220,9 +219,12 @@
info(' 6.2 - Check moved asset files')->
info(' 6.2.1 - File in source folder must not exist')->
- isntFile($folder, 'asset1_renamed.png')->
- info(' 6.2.2 - File in destination folder must exist')->
- isFile($subf2->getRelativePath(), 'asset1_renamed.png')->
+ isntFile($old)
+;
+
+$asset->refresh(true);
+$browser->info(' 6.2.2 - File in destination folder must exist')->
+ isFile($asset)->
info('7 - Icons view')->
get('/ly_media_asset/icons')->

0 comments on commit fd4ea1b

Please sign in to comment.