root/trunk/plugins/sfThumbnailPlugin/README

Revision 35, 6.4 kB (checked in by nperriault, 8 months ago)

added local version of sfThumbnailPlugin

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