Author: garak
Date: 2010-09-09 16:18:07 +0200 (Thu, 09 Sep 2010)
New Revision: 30861

Modified:
   plugins/sfAssetsLibraryPlugin/branches/1.3/README
   plugins/sfAssetsLibraryPlugin/branches/1.3/lib/helper/sfAssetHelper.php
   plugins/sfAssetsLibraryPlugin/branches/1.3/lib/model/plugin/PluginsfAsset.php
   plugins/sfAssetsLibraryPlugin/branches/1.3/lib/sfAssetsLibraryTools.class.php
   
plugins/sfAssetsLibraryPlugin/branches/1.3/modules/sfAsset/templates/_asset.php
   plugins/sfAssetsLibraryPlugin/branches/1.3/package.xml
Log:
[sfAssetsLibraryPlugin] added generation of PDF thumbnails


Modified: plugins/sfAssetsLibraryPlugin/branches/1.3/README
===================================================================
--- plugins/sfAssetsLibraryPlugin/branches/1.3/README   2010-09-09 13:01:46 UTC 
(rev 30860)
+++ plugins/sfAssetsLibraryPlugin/branches/1.3/README   2010-09-09 14:18:07 UTC 
(rev 30861)
@@ -18,7 +18,7 @@
 
 This plugin for symfony 1.4 depends on 
[sfThumbnailPlugin](/plugins/sfThumbnailPlugin)
 to create thumbnails of image files. If this plugin is not yest installed, the 
symfony
-plugin dependency system will install it when you install 
sfAssetsLibraryPlugin.
+plugin dependency system will install it when you install 
`sfAssetsLibraryPlugin`.
 
 Note: If [ImageMagick](http://www.imagemagick.org/) is installed (no need for 
the PEAR
 Imagick package, the plugin calls the `convert` script of the basic Image Magic
@@ -35,11 +35,12 @@
     > php symfony plugin:install sfAssetsLibraryPlugin
 
 Alternatively, if you don't have PEAR installed, you can download the latest 
package
-attached to this plugin's wiki page and extract it under your project's 
`plugins/`
+attached to this page and extract it under your project's `plugins/`
 directory. You can also refer to the plugin's Subversion repository by doing a
-checkout or an `svn:externals` of 
http://svn.symfony-project.com/plugins/sfAssetsLibraryPlugin.
+checkout or an `svn:externals` of 
`http://svn.symfony-project.com/plugins/sfAssetsLibraryPlugin/branches/1.3`.
 
-If you use one of these alternative methods, you must publish the plugin 
assets by
+If you use one of these alternative methods, you must enable the plugin in your
+`ProjectConfiguration.class.php` file and publish the plugin assets by
 calling the `plugin:publish-assets` symfony task.
 
 2 - Build the data structures
@@ -58,7 +59,7 @@
 3 - Configure your project to use the plugin features
 
 Enable the `sfAssetsLibraryPlugin` and the `sfThumbnailPlugin` in the project
-configuration
+configuration (unless you installed using `plugin:install`)
 
     [php]
     // in myproject/config/ProjectConfiguration.class.php
@@ -103,7 +104,7 @@
 >**NOTE**
 >*nix users must call this command with the same user group as the http
 >server, because it will need write access to this directory. Alternatively,
->chmod 777 or chgrp can be used.
+>`chmod 777` or `chgrp` can be used.
 
 6 - You can now start using the plugin by browsing to the backend module's
 default page:
@@ -150,13 +151,13 @@
 The `sfAsset` module provides four main features:
  - Browse through your media files like in a filesystem.
  - Read or change metadata for a particular file
- - Perform usual filesystem operztions on your files and folder (add, move, 
rename, delete)
+ - Perform usual filesystem operations on your files and folder (add, move, 
rename, delete)
  - Search for a particular media file from its filename, description, author, 
etc.
 
 The module has two main views (`list` and `edit`) that you can easily 
customize by
 using your own CSS or overriding some of their numerous partials in your 
application.
 The templates are organized the same way as the admin generated modules, so 
applying
-your own look and feel to the sfAssetsLibrary should be easy.
+your own look and feel to the `sfAssetsLibrary` should be easy.
 
 The module is fully i18n'ed, and the plugin comes with English, French, 
Italian,
 and German translations.
@@ -202,7 +203,7 @@
 Using the assets library with TinyMCE
 -------------------------------------
 
-If you want to use the sfAssetsLibrary plugin as a replacement for tinyMCE's 
file
+If you want to use the `sfAssetsLibrary` plugin as a replacement for tinyMCE's 
file
 browser for image insertion, you must first initiate the plugin with a helper 
in
 the template:
 
@@ -225,7 +226,7 @@
 If you use the configuration above, you can add tinyMCE to all your textarea's 
with class
 "rich" (with generator.yml in admin generator, or with form templates 
otherwise).
 
-That's it, the TinyMCE file browser is now the sfAssetsLibrary's one.
+That's it, the TinyMCE file browser is now the `sfAssetsLibrary`'s one.
 
 >**TIP**: For use with an admin generated module, place the 
 >`init_asset_library()` call
 >in the `_assets.php` partial in the `templates/` directory of the module. If 
 >you miss
@@ -278,12 +279,22 @@
     $this->widgetSchema['asset_id'] = new 
sfWidgetFormAssetInput(array('asset_type' => 'image'))
 
 
+Thumbnails of PDF files
+-----------------------
+
+If you use ImageMagick, a nice thumbnail of PDF file is generated and used 
instead of default
+type icon. Since the current version of `sfThumbnailPlugin` does not support 
generation of
+single page thumbnails, this is working only with single page files. If you 
add a PDF with multiple
+pages, wrong thumbnails (with subsequent numbers in name) will be generated.
+You can apply the patch found on [this 
ticket](http://trac.symfony-project.org/ticket/9059)
+to avoid this problem.
+
+
 TODO
 ----
 
 * Check Windows compatibility
 * Make list of ignored files and folders configurable in synchronize task
 * 100% model coverage for unit tests
-* Make thumbnails for PDF files (if ImageMagick is installed)
 * Drag and drop manipulation for file and folders
 * Screencast to show installation and usage

Modified: 
plugins/sfAssetsLibraryPlugin/branches/1.3/lib/helper/sfAssetHelper.php
===================================================================
--- plugins/sfAssetsLibraryPlugin/branches/1.3/lib/helper/sfAssetHelper.php     
2010-09-09 13:01:46 UTC (rev 30860)
+++ plugins/sfAssetsLibraryPlugin/branches/1.3/lib/helper/sfAssetHelper.php     
2010-09-09 14:18:07 UTC (rev 30861)
@@ -24,9 +24,13 @@
     'title' => $asset->getDescription() . ' ' . $asset->getCopyright()
   ), $options);
 
-  if ($asset->isImage())
+  if ($asset->isImage() || $asset->isPdf())
   {
-    $src = $asset->getUrl($thumbType, $relativePath);
+    $src = $asset->getUrl($thumbType, $relativePath, $asset->isPdf());
+    if ($asset->isPdf() && !is_readable(sfConfig::get('sf_web_dir') . $src))
+    {
+      $src = '/sfAssetsLibraryPlugin/images/pdf.png';
+    }
   }
   elseif ($thumbType == 'full')
   {

Modified: 
plugins/sfAssetsLibraryPlugin/branches/1.3/lib/model/plugin/PluginsfAsset.php
===================================================================
--- 
plugins/sfAssetsLibraryPlugin/branches/1.3/lib/model/plugin/PluginsfAsset.php   
    2010-09-09 13:01:46 UTC (rev 30860)
+++ 
plugins/sfAssetsLibraryPlugin/branches/1.3/lib/model/plugin/PluginsfAsset.php   
    2010-09-09 14:18:07 UTC (rev 30861)
@@ -55,11 +55,12 @@
   /**
    * Gives the URL for the given thumbnail
    *
-   * @param  string $thumbnail_type
-   * @param  string $relative_path
+   * @param  string  $thumbnail_type
+   * @param  string  $relative_path
+   * @param  boolean $pdf
    * @return string
    */
-  public function getUrl($thumbnail_type = 'full', $relative_path = null)
+  public function getUrl($thumbnail_type = 'full', $relative_path = null, $pdf 
= false)
   {
     if (is_null($relative_path))
     {
@@ -78,6 +79,10 @@
     {
       $url .= sfAssetsLibraryTools::getThumbnailDir($relative_path) . 
$thumbnail_type . '_' . $this->getFilename();
     }
+    if ($pdf)
+    {
+      $url = substr($url, 0, -3) . 'jpg';
+    }
 
     return $url;
   }
@@ -87,14 +92,28 @@
     $this->setType(sfAssetsLibraryTools::getType($this->getFullPath()));
   }
 
+  /**
+   * @return boolean
+   */
   public function isImage()
   {
     return $this->getType() === 'image';
   }
 
+  /**
+   * @return boolean
+   */
+  public function isPdf()
+  {
+    return $this->getType() === 'pdf';
+  }
+
+  /**
+   * @return boolean
+   */
   public function supportsThumbnails()
   {
-    return $this->isImage() && class_exists('sfThumbnail');
+    return ($this->isImage() || $this->isPdf()) && class_exists('sfThumbnail');
   }
 
   /**
@@ -143,7 +162,7 @@
 
     if ($this->supportsThumbnails())
     {
-      sfAssetsLibraryTools::createThumbnails($this->getFolderPath(), 
$this->getFilename());
+      sfAssetsLibraryTools::createThumbnails($this->getFolderPath(), 
$this->getFilename(), $this->isPdf());
     }
   }
 

Modified: 
plugins/sfAssetsLibraryPlugin/branches/1.3/lib/sfAssetsLibraryTools.class.php
===================================================================
--- 
plugins/sfAssetsLibraryPlugin/branches/1.3/lib/sfAssetsLibraryTools.class.php   
    2010-09-09 13:01:46 UTC (rev 30860)
+++ 
plugins/sfAssetsLibraryPlugin/branches/1.3/lib/sfAssetsLibraryTools.class.php   
    2010-09-09 14:18:07 UTC (rev 30861)
@@ -274,6 +274,11 @@
     }
     else
     {
+      if (substr($filename, -4) == '.pdf')
+      {
+        $filename = substr($filename, 0, -3) . 'jpg';
+      }
+
       return self::getMediaDir(true) . self::getThumbnailDir($path) . 
$thumbnailType . '_' . $filename;
     }
   }
@@ -285,8 +290,11 @@
    *   small: { width: 80, height: 80, shave: true }  // 80x80 shaved
    *   small: [80, 80, true]                          // 80x80 shaved
    *   small: [80]                                    // 80x80 not shaved
+   * @param string $folder
+   * @param string $filename
+   * @param boolean $pdf
    */
-  public static function createThumbnails($folder, $filename)
+  public static function createThumbnails($folder, $filename, $pdf = false)
   {
     $source = self::getThumbnailPath($folder, $filename, 'full');
     $thumbnailSettings = sfConfig::get('app_sfAssetsLibrary_thumbnails', array(
@@ -298,7 +306,14 @@
       $width  = $params['width'];
       $height = $params['height'];
       $shave  = isset($params['shave']) ? $params['shave'] : false;
+      if ($pdf)
+      {
+        self::createPdfThumbnail($source, self::getThumbnailPath($folder, 
$filename, $key), $width, $height, $shave);
+      }
+      else
+      {
       self::createThumbnail($source, self::getThumbnailPath($folder, 
$filename, $key), $width, $height, $shave);
+      }
     }
   }
 
@@ -328,24 +343,69 @@
       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;
+        $newHeight = $width > 0 && $w > 0 ? ceil(($width * $h) / $w) : $height;
         $thumbnail = new sfThumbnail($width, $newHeight, true, true, 85, 
$adapter);
+      }
         $thumbnail->loadFile($source);
         $thumbnail->save($dest, $mime);
+
         return true;
       }
+
+    return false;
+  }
+
+  /**
+   * Create thumbnail for a PDF file
+   * @param  string  $source
+   * @param  string  $dest
+   * @param  integer $width
+   * @param  integer $height
+   * @param  boolean $shave_all Recommanded when  "image source HEIGHT" < 
"image source WIDTH"
+   * @return boolean
+   */
+  public static function createPdfThumbnail($source, $dest, $width, $height, 
$shave_all = false)
+  {
+    if (class_exists('sfThumbnail') && 
sfConfig::get('app_sfAssetsLibrary_use_ImageMagick', false) && 
file_exists($source))
+    {
+      $adapter = 'sfImageMagickAdapter';
+      $mime = 'image/jpg';
+      if ($shave_all)
+      {
+        $thumbnail = new sfThumbnail($width, $height, false, false, 85, 
$adapter, array('method' => 'shave_all'));
+      }
+      else
+      {
+        list($w, $h) = self::getPdfSize($source);
+        $newHeight = $width > 0 && $w > 0 ? ceil(($width * $h) / $w) : $height;
+        $thumbnail = new sfThumbnail($width, $newHeight, true, false, 85, 
$adapter);
+      }
+      $thumbnail->loadFile($source);
+      $thumbnail->save($dest, $mime, true);
+
+        return true;
     }
     
     return false;
   }
 
+  public static function getPdfSize($file)
+  {
+    if (sfConfig::get('app_sfAssetsLibrary_use_ImageMagick', false) == false)
+    {
+      throw new sfException('you must enable ImageMagick in configuration.');
+    }
+    $cmd = 'identify -format "%wx%h****" ' . escapeshellarg($file);
+    exec($cmd, $output);
+    $tmp = substr($output[0], 0, strpos($output[0], '****'));
+
+    return explode('x', $tmp);
+  }
+
   /**
    * @param  string $path
    * @return string

Modified: 
plugins/sfAssetsLibraryPlugin/branches/1.3/modules/sfAsset/templates/_asset.php
===================================================================
--- 
plugins/sfAssetsLibraryPlugin/branches/1.3/modules/sfAsset/templates/_asset.php 
    2010-09-09 13:01:46 UTC (rev 30860)
+++ 
plugins/sfAssetsLibraryPlugin/branches/1.3/modules/sfAsset/templates/_asset.php 
    2010-09-09 14:18:07 UTC (rev 30861)
@@ -1,7 +1,7 @@
 <?php use_helper('sfAsset') ?>
 <div class="assetImage">
   <div class="thumbnails">
-    <?php echo link_to_asset_action(asset_image_tag($sf_asset, 'small', 
array('width' => 84), isset($folder) ? $folder->getRelativePath() : null), 
$sf_asset) ?>
+    <?php echo link_to_asset_action(asset_image_tag($sf_asset, 'small', 
array(), isset($folder) ? $folder->getRelativePath() : null), $sf_asset) ?>
   </div>
 
   <div class="assetComment">

Modified: plugins/sfAssetsLibraryPlugin/branches/1.3/package.xml
===================================================================
--- plugins/sfAssetsLibraryPlugin/branches/1.3/package.xml      2010-09-09 
13:01:46 UTC (rev 30860)
+++ plugins/sfAssetsLibraryPlugin/branches/1.3/package.xml      2010-09-09 
14:18:07 UTC (rev 30861)
@@ -22,10 +22,10 @@
    <email></email>
    <active>yes</active>
   </developer>
-  <date>2010-04-06</date>
+  <date>2010-09-03</date>
   <version>
-    <release>0.9.4</release>
-    <api>0.9.4</api>
+    <release>0.9.5</release>
+    <api>0.9.5</api>
   </version>
   <stability>
     <release>beta</release>
@@ -58,6 +58,21 @@
   <changelog>
     <release>
       <version>
+        <release>0.9.5</release>
+        <api>0.9.5</api>
+      </version>
+      <stability>
+        <release>beta</release>
+        <api>beta</api>
+      </stability>
+      <license uri="http://www.symfony-project.com/license";>MIT 
license</license>
+      <date>2010-09-03</date>
+      <notes>
+        * garak: Fixed many bugs
+      </notes>
+    </release>
+    <release>
+      <version>
         <release>0.9.4</release>
         <api>0.9.4</api>
       </version>

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to