README 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. = sfThumbnailPlugin plugin =
  2. The `sfThumbnailPlugin` creates thumbnails from images. It relies on your
  3. choice of the [http://php.net/gd/ GD] or [http://www.imagemagick.org
  4. ImageMagick] libraries.
  5. == Installation ==
  6. To install the plugin for a symfony project, the usual process is to use the
  7. symfony command line.
  8. With symfony 1.0, use:
  9. {{{
  10. $ symfony plugin-install http://plugins.symfony-project.com/sfThumbnailPlugin
  11. }}}
  12. With symfony 1.1/1.2, use:
  13. {{{
  14. $ symfony plugin:install sfThumbnailPlugin
  15. }}}
  16. Alternatively, if you don't have PEAR installed, you can download the latest
  17. package attached to this plugin's wiki page and extract it under your project's
  18. `plugins/` directory.
  19. Clear the cache to enable the autoloading to find the new classes:
  20. {{{
  21. $ php symfony cc
  22. }}}
  23. You're done.
  24. '''Note''': If the [http://php.net/gd GD library] is not activated, you might
  25. have to uncomment the related line in your `php.ini` and restart your web
  26. server to enable PHP image handling functions.
  27. '''Note''': To use !ImageMagick, you'll need to download and install the
  28. binaries from http://www.imagemagick.org.
  29. == Contents ==
  30. The plugin contains three classes, `sfThumbnail`, `sfGDAdapter` and
  31. `sfImageMagickAdapter`. Available methods are:
  32. {{{
  33. // Initialize the thumbnail attributes
  34. __construct($maxWidth = null, $maxHeight = null, $scale = true, $inflate = true, $quality = 75, $adapterClass = null, $adapterOptions = array())
  35. // Load image file from a file system
  36. loadFile($imageFile)
  37. // Load image file from a string (GD adapter only, currently)
  38. loadData($imageString, $mimeType)
  39. // Save the thumbnail to a file
  40. save($thumbFile, $targetMime = null)
  41. }}}
  42. Supported GD image types are 'image/jpeg', 'image/png' and 'image/gif'.
  43. !ImageMagick supports over [http://www.imagemagick.org/script/formats.php 100
  44. types].
  45. Note that the $quality setting only applies to JPEG images.
  46. == Usage ==
  47. === Creating a thumbnail from an existing image file ===
  48. The process of creating a thumbnail from an existing image file is pretty
  49. straightforward.
  50. First, you must initialize a new `sfThumbnail` object with two parameters: the
  51. maximum width and height of the desired thumbnail.
  52. {{{
  53. // Initialize the object for 150x150 thumbnails
  54. $thumbnail = new sfThumbnail(150, 150);
  55. }}}
  56. Then, specify a file path to the image to reduce to the `loadFile()` method.
  57. {{{
  58. // Load the image to reduce
  59. $thumbnail->loadFile('/path/to/image/file.png');
  60. }}}
  61. Finally, ask the thumbnail object to save the thumbnail. You must provide a
  62. file path. Optionally, if you don't want the thumbnail to use the same mime
  63. type as the source image, you can specify a mime type as the second parameter.
  64. {{{
  65. // Save the thumbnail
  66. $thumbnail->save('/path/to/thumbnail/file.jpg', 'image/jpg');
  67. }}}
  68. Both the source and destination file paths must be absolute paths in your
  69. filesystem. To store files under a symfony project directory, make sure you use
  70. the
  71. [http://www.symfony-project.com/book/trunk/19-Mastering-Symfony-s-Configuration-Files#The%20Basic%20File%20Structure
  72. directory constants], accessed by `sfConfig::get()`.
  73. === Creating a thumbnail for an uploaded file ===
  74. If you upload images, you might need to create thumbnails of each uploaded file. For instance, to save a thumbnail of maximum size 150x150px at the same time as the uploaded image, the form handling action can look like:
  75. {{{
  76. public function executeUpload()
  77. {
  78. // Retrieve the name of the uploaded file
  79. $fileName = $this->getRequest()->getFileName('file');
  80. // Create the thumbnail
  81. $thumbnail = new sfThumbnail(150, 150);
  82. $thumbnail->loadFile($this->getRequest()->getFilePath('file'));
  83. $thumbnail->save(sfConfig::get('sf_upload_dir').'/thumbnail/'.$fileName, 'image/png');
  84. // Move the uploaded file to the 'uploads' directory
  85. $this->getRequest()->moveFile('file', sfConfig::get('sf_upload_dir').'/'.$fileName);
  86. // Do whatever is next
  87. $this->redirect('media/show?filename='.$fileName);
  88. }
  89. }}}
  90. Don't forget to create the `uploads/thumbnail/` directory before calling the action.
  91. == !ImageMagick-Specific Usage ==
  92. Usage is the same as above except you need to explicitly call the sfImageMagickAdapter class.
  93. {{{
  94. $thumbnail = new sfThumbnail(150, 150, true, true, 75, 'sfImageMagickAdapter');
  95. }}}
  96. === Custom Options ===
  97. The last option in the constructor is an array that you can use to pass custom
  98. options to !ImageMagick. Below are some examples of this functionality.
  99. Extract the first page from a PDF document:
  100. {{{
  101. $thumbnail = new sfThumbnail(150, 150, true, true, 75, 'sfImageMagickAdapter', array('extract' => 1));
  102. }}}
  103. "1" stands for the first page, "2" for the second page, etc.
  104. If for some reason you use a non-standard name for your !ImageMagick binary, you can specify it like so:
  105. {{{
  106. $thumbnail = new sfThumbnail(150, 150, true, true, 75, 'sfImageMagickAdapter', array('convert' => 'my_imagemagick_binary'));
  107. }}}
  108. By default sfThumbnail resizes the image in order to get to the desired width
  109. and height of the thumbnail. But what if you want to force the thumbnail to be
  110. a certain width and height but without distorting the image of the source size
  111. scale is different than the thumbnail size scale.
  112. Now you can use a custom option "method" to achieve just that but "shaving" the
  113. source image in order to get it to be the same scale as the thumbnail and them
  114. resize it to the required dimentions.
  115. When "shave_bottom" is used and the image’s width is greater than the
  116. height then sfThumbnail shaves from both left side and right side until the
  117. desired scale.
  118. There is one requirement for the "method" option to work as expected and it is
  119. to turn off scaling (set the third parameter of sfThumbnail to FALSE).
  120. {{{
  121. $thumbnail = new sfThumbnail(150, 150, false, true, 75, 'sfImageMagickAdapter', array('method' => 'shave_all'));
  122. $thumbnail->loadFile('http://www.walkerbooks.co.uk/assets_walker/dynamic/1172005677146.png');
  123. $thumbnail->save('/tmp/shave.png', 'image/png');
  124. }}}
  125. == Changelog ==
  126. === Trunk ===
  127. * fabien: added a toResource() method to get the image resource for the thumbnail
  128. === 2007-09-15 | 1.5.0 Stable ===
  129. * kupokomapa: toString($mime) method now works for both adapters
  130. * kupokomapa: loadFile() method can now accept a URI if sfWebBrowserPluging is available
  131. * kupokomapa: Implemented "method" option for sfImageMagickAdapter where "method" for now can be "shave_all" or "shave_bottom"
  132. === 2007-09-12 | 1.4.0 Stable ===
  133. * davedash: Added toString($mime) function to save file to a string
  134. === 2007-05-18 | 1.3.0 Stable ===
  135. * bmeynell: Updated README
  136. * bmeynell: Fixed ticket (#1710)
  137. * bmeynell: Added adapter support
  138. * francois: Updated README
  139. === 2006-11-29 | 1.2.0 Stable ===