1 //////////////////////////////////////////////////////////////
2 /// phpThumb() by James Heinrich <info@silisoftware.com> //
3 // available at http://phpthumb.sourceforge.net ///
4 //////////////////////////////////////////////////////////////
6 // This code is released under the GNU GPL: //
7 // http://www.gnu.org/copyleft/gpl.html //
9 // +-----------------------------------------------+ //
10 // | phpThumb() is free to use according to the | //
11 // | terms of the GPL. Donations also gratefully | //
12 // | GPL FAQ: http://gnu.org/licenses/gpl-faq.html | //
14 // | Donations are gratefully accepted from happy | //
15 // | users :) See http://phpthumb.sourceforge.net | //
17 // | If you like phpThumb(), please consider | //
18 // | writing a review at HotScripts.com: | //
19 // | http://www.hotscripts.com/Detailed/25654.html | //
21 // | If you do use this code somewhere, send me | //
22 // | an email and tell me how/where you used it. | //
23 // +-----------------------------------------------+ //
25 //////////////////////////////////////////////////////////////
31 phpThumb() uses the GD library to create thumbnails from
32 images (GIF, PNG or JPEG) on the fly. The output size is
33 configurable (can be larger or smaller than the source),
34 and the source may be the entire image or only a portion
35 of the original image. True color and resampling is used
36 if GD v2.0+ is available, otherwise low-color and simple
37 resizing is used. Source image can be a physical file on
38 the server or can be retrieved from a database. GIFs are
39 supported on all versions of GD even if GD does not have
40 native GIF support thanks to the GIFutil class by Fabien
41 Ezber. AntiHotlinking feature prevents other people from
42 using your server to resize their thumbnails, or link to
43 your images from another server. The cache feature
51 First, read this file.
52 Then read phpthumb.faq.txt
53 Then run /demo/phpThumb.demo.check.php
54 If you still think it's a bug, email info@silisoftware.com
61 Call phpThumb() just like you would a normal image.
63 <IMG SRC="phpThumb.php?src=/image.jpg&w=100">
64 <IMG SRC="phpThumb.php?src=http://example.com/foo.jpg">
65 See the "demo" link on http://phpthumb.sourceforge.net
66 for more usage examples). Parameters that can be passed
67 are listed below under "URL Parameters".
69 NOTE: It's recommended you use the local image filename
70 wherever possible (rather than http://) because performance
71 is much better, less (or no) use of temp files, and the
72 last-modified check for cached files doesn't work for
75 To access files over a LAN with Windows share names you
76 must use the network name (or IP) and not a mapped drive
78 //othercomputer/file.jpg - good
79 //192.168.2.1/file.jpg - good
80 z:/file.jpg - won't work
81 This is a PHP limitation (see www.php.net/file-exists)
82 Note: you may want to use "/" slashes instead of "\" if
83 you have magic_quotes_gpc enabled to avoid stripslashes
84 problems, although either slash should work if
85 magic_quotes_gpc is disabled
88 ================================
89 Alternate PATH_INFO-style Usage:
90 ================================
92 phpThumb.php can also be called by passing parameters not
93 after the usual "?" but like this:
94 phpThumb.php/<params>=<values>;<w>x<h>;<image>
96 phpThumb.php/100;pic.jpg
97 phpThumb.php/100;images/pic.jpg
98 phpThumb.php/100;/images/pic.jpg
99 phpThumb.php/100x200;pic.jpg
100 phpThumb.php/x200;pic.jpg
101 phpThumb.php/f=jpeg;q=50;100x200;pic.jpg
102 phpThumb.php/fltr[]=usm;100;pic.jpg
104 <image> must be the last item. Dimensions must be the second-
105 last item. As many key/value pairs for parameters can be
106 passed before those last two items, with each pair joined by
107 equals ("=") and seperated by semicolon (";")
110 ==============================================
111 Calling as an object (not using phpThumb.php):
112 ==============================================
114 NOTE: most people don't need to and should not do this.
115 If you just want to display resized images, please just
116 use phpThumb.php, not the object mode. To render output
117 to one (or more) files instead of the browser, you should
118 skip phpThumb.php and instantiate your own object. Please
119 take a look at /demo/phpThumb.demo.object.php for details.
121 Note: phpThumb.php is where the caching code is located, if
122 you instantiate your own phpThumb() object that code is
123 bypassed and it's up to you to handle the reading and
124 writing of cached files.
131 There are some configuration options you may (but are
132 not required to) change. Most configuration options can
133 be set when you call phpThumb() - see list below), but
134 default configuration options (such as cache directory)
135 are in phpThumb.config.php - this is the only file you
138 The configuration file is distributed as
139 phpThumb.config.php.default to prevent accidental
140 overwriting of old configuration settings. Please
141 migrate your old settings to the new file (if upgrading),
142 or delete your old config and rename the default to
146 The amount of memory required for phpThumb depends on
147 several factors: the dimensions of the source image,
148 the dimensions of the output image, whether unsharp
149 masking is applied, whether watermarks are applied, etc.
150 The auto-detection of memory limits works as a general
151 "safe" value. You may be able to exceed the auto value
152 by a small or large amount, depending on whether you
153 apply watermarks and/or sharpening, and the output size
154 of your thumbnails. I do not currently have a reliable
155 formula for calculating such things, but I will attempt
156 to craft one for future versions of phpThumb(). Until
157 then, set "max_source_pixels" in phpThumb.config.php to a
158 value that works well for you (or leave it alone if the
159 defaults give you no problems).
161 The configuration options you should maybe modify are:
162 * cache_directory - thumbnailing is slow and processor-
163 intensive. Enabling caching will dramatically speed
164 up future thumbnail serving
165 * max_source_pixels - This should be auto-detected, but
166 if auto-detection fails and you get an invalid image
167 from large source images, set this to about 20% of
168 your available PHP memory limit.
169 * imagemagick_path - If the source image is larger than
170 max_source_pixels allows, but ImageMagick is available
171 phpThumb() will use it to generate the thumbnail.
174 ///////////////////////////////////////////////////////////
175 Note: High-Security mode is recommended enabled if possible.
176 Set $PHPTHUMB_CONFIG['high_security_enabled'] in
177 phpThumb.config.php to enable it. Each call to phpThumb
178 needs to be made through the function supplied at the
179 bottom of phpThumb.config.php which create the hash:
180 require_once('phpThumb.config.php');
181 echo '<img src="'.phpThumbURL('src=pic.jpg&w=50').'">';
182 ///////////////////////////////////////////////////////////
189 src = filename of source image
190 new = create new image, not thumbnail of existing image.
191 Requires "w" and "h" parameters set.
192 [ex: &new=FF0000|75] - red background, 75% opacity
193 Set to hex color string of background. Opacity is
194 optional (defaults to 100% opaque).
195 w = max width of output thumbnail in pixels
196 h = max height of output thumbnail in pixels
197 wp = max width for portrait images
198 hp = max height for portrait images
199 wl = max width for landscape images
200 hl = max height for landscape images
201 ws = max width for square images
202 hs = max height for square images
203 f = output image format ("jpeg", "png", or "gif")
204 q = JPEG compression (1=worst, 95=best, 75=default)
205 sx = left side of source rectangle (default = 0)
206 (values 0 < sx < 1 represent percentage)
207 sy = top side of source rectangle (default = 0)
208 (values 0 < sy < 1 represent percentage)
209 sw = width of source rectangle (default = fullwidth)
210 (values 0 < sw < 1 represent percentage)
211 sh = height of source rectangle (default = fullheight)
212 (values 0 < sh < 1 represent percentage)
213 zc = zoom-crop. Will auto-crop off the larger dimension
214 so that the image will fill the smaller dimension
215 (requires both "w" and "h"). Set "zc=1" to enable.
216 (overrides both "iar" and "far")
217 bg = background hex color (default = FFFFFF)
218 bc = border hex color (default = 000000)
219 fltr = filter system. Call as an array as follows:
220 - "brit" (Brightness) [ex: &fltr[]=brit|<value>]
221 where <value> is the amount +/- to adjust brightness
223 Availble in PHP5 with bundled GD only.
224 - "cont" (Constrast) [ex: &fltr[]=cont|<value>]
225 where <value> is the amount +/- to adjust contrast
227 Availble in PHP5 with bundled GD only.
228 - "gam" (Gamma Correction) [ex: &fltr[]=gam|<value>]
229 where <value> can be a number >0 to 10+ (default 1.0)
230 Must be >0 (zero gives no effect). There is no max,
231 although beyond 10 is pretty useless. Negative
232 numbers actually do something, maybe not quite the
233 desired effect, but interesting nonetheless.
234 - "sat" (SATuration) [ex: &fltr[]=sat|<value>]
235 where <value> is a number between zero (no change)
236 and -100 (complete desaturation = grayscale), or it
237 can be any positive number for increased saturation.
238 - "ds" (DeSaturate) [ex: &fltr[]=ds|<value>]
239 is an alias for "sat" except values are inverted
240 (positive values remove color, negative values boost
242 - "gray" (Grayscale) [ex: &fltr[]=gray]
243 remove all color from image, make it grayscale
244 - "th" (Threshold) [ex: &fltr[]=th|<value>]
245 makes image greyscale, then sets all pixels brighter
246 than <value> (range 0-255) to white, and all pixels
247 darker than <value> to black
248 - "rcd" (Reduce Color Depth) [ex: &fltr[]=rcd|<c>|<d>]
249 where <c> is the number of colors (2-256) you want
250 in the output image, and <d> is "1" for dithering
251 (deault) or "0" for no dithering
252 - "clr" (Colorize) [ex: &fltr[]=clr|<value>|<color>]
253 where <value> is a number between 0 and 100 for the
254 amount of colorization, and <color> is the hex color
256 - "sep" (Sepia) [ex: &fltr[]=sep|<value>|<color>]
257 where <value> is a number between 0 and 100 for the
258 amount of colorization (default=50), and <color> is
259 the hex color to colorize to (default=A28065).
260 Note: this behaves differently when applied by
261 ImageMagick, in which case 80 is default, and lower
262 values give brighter/yellower images and higher
263 values give darker/bluer images
264 - "usm" (UnSharpMask) [ex: &fltr[]=usm|<a>|<r>|<t>]
265 where <a> is the amount (default = 80), <r> is the
266 radius (default = 0.5), <t> is the threshold
268 - "blur" (Blur) [ex: &fltr[]=blur|<radius>]
269 where (0 < <radius> < 25) (default = 1)
270 - "gblr" (Gaussian Blur) [ex: &fltr[]=gblr]
271 Availble in PHP5 with bundled GD only.
272 - "sblr" (Selective Blur) [ex: &fltr[]=gblr]
273 Availble in PHP5 with bundled GD only.
274 - "smth" (Smooth) [ex: &fltr[]=smth|<value>]
275 where <value> is the weighting value for the matrix
276 (range -10 to 10, default 6)
277 Availble in PHP5 with bundled GD only.
278 - "lvl" (Levels) [ex: &fltr[]=lvl|<channel>|<min>|<max>
279 where <channel> can be one of 'r', 'g', 'b', 'a' (for
280 Red, Green, Blue, Alpha respectively), or '*' for all
281 channels based on average grayscale value (default).
282 <min> and <max> are the clip points for the levels
283 and are set to clip 0.1% of each end by default.
284 (range = 0-255) and are set to clip 0.1% of each end
285 by default. Use -1 for min and/or max to invoke auto-
286 detect mode. Using default parameters (&fltr[]=lvl)
287 is similar to Auto Contrast in Adobe Photoshop.
288 - "wb" (White Balance) [ex: &fltr[]=wb|<c>]
289 where <c> is the target hex color to white balance
290 on, this color is what "should be" white, or light
291 gray. The filter attempts to maintain brightness so
292 any gray color can theoretically be used. If <c> is
293 omitted the filter guesses based on brightest pixels
296 [ex: &fltr[]=hist|<b>|<c>|<w>|<h>|<a>|<o>|<m>]
297 Where <b> is the color band(s) to display, from back
298 to front (one or more of "rgba*" for Red Green Blue
299 Alpha and Grayscale respectively);
300 <c> is a semicolon-seperated list of hex colors to
301 use for each graph band (defaults to FF0000, 00FF00,
302 0000FF, 999999, FFFFFF respectively);
303 <w> and <h> are the width and height of the overlaid
304 histogram in pixels, or if <= 1 then percentage of
305 source image width/height;
306 <a> is the alignment (same as for "wmi" and "wmt");
307 <o> is opacity from 0 (transparent) to 100 (opaque)
308 (requires PHP v4.3.2, otherwise 100% opaque);
309 <m> is the edge (and inter-tile) margin in percent
310 - "over" (OVERlay/underlay image) overlays an image on
311 the thumbnail, or overlays the thumbnail on another
312 image (to create a picture frame for example)
313 [ex: &fltr[]=over|<i>|<u>|<m>|<o>]
314 where <i> is the image filename; <u> is "0" (default)
315 for overlay the image on top of the thumbnail or "1"
316 for overlay the thumbnail on top of the image; <m> is
317 the margin - can be absolute pixels, or if < 1 is a
318 percentage of the thumbnail size [must be < 0.5]
319 (default is 0 for overlay and 10% for underlay);
320 <o> is opacity (0 = transparent, 100 = opaque)
321 (requires PHP v4.3.2, otherwise 100% opaque);
322 (thanks raynerapeØgmail*com, shabazz3Ømsu*edu)
323 - "wmi" (WaterMarkImage)
324 [ex: &fltr[]=wmi|<f>|<a>|<o>|<m>] where
325 <f> is the filename of the image to overlay;
326 <a> is the alignment (one of BR, BL, TR, TL, C,
327 R, L, T, B, *) where B=bottom, T=top, L=left,
328 R=right, C=centre, *=tile);
329 <o> is opacity from 0 (transparent) to 100 (opaque)
330 (requires PHP v4.3.2, otherwise 100% opaque);
331 <m> is the edge (and inter-tile) margin in percent
332 - "wmt" (WaterMarkText)
333 [ex: &fltr[]=wmt|<t>|<s>|<a>|<c>|<f>|<o>|<m>|<n>|<b>|<O>|<x>]
335 <t> is the text to use as a watermark;
336 URLencoded Unicode HTMLentities must be used for
337 characters beyond chr(127). For example, the
338 "eighth note" character (U+266A) is represented
339 as "♪" and then urlencoded to "%26%239834%3B"
340 Any instance of metacharacters will be replaced
341 with their calculated value. Currently supported:
342 ^Fb = source image filesize in bytes
343 ^Fk = source image filesize in kilobytes
344 ^Fm = source image filesize in megabytes
345 ^X = source image width in pixels
346 ^Y = source image height in pixels
347 ^x = thumbnail width in pixels
348 ^y = thumbnail height in pixels
350 <s> is the font size (1-5 for built-in font, or point
351 size for TrueType fonts);
352 <a> is the alignment (one of BR, BL, TR, TL, C, R, L,
353 T, B, * where B=bottom, T=top, L=left, R=right,
355 <c> is the hex color of the text;
356 <f> is the filename of the TTF file (optional, if
357 omitted a built-in font will be used);
358 <o> is opacity from 0 (transparent) to 100 (opaque)
359 (requires PHP v4.3.2, otherwise 100% opaque);
360 <m> is the edge (and inter-tile) margin in percent;
362 <b> is the hex color of the background;
363 <O> is background opacity from 0 (transparent) to
365 (requires PHP v4.3.2, otherwise 100% opaque);
366 <x> is the direction(s) in which the background is
367 extended (either 'x' or 'y' (or both, but both
368 will obscure entire image))
369 Note: works with TTF fonts only, not built-in
370 - "flip" [ex: &fltr[]=flip|x or &fltr[]=flip|y]
371 flip image on X or Y axis
372 - "ric" [ex: &fltr[]=ric|<x>|<y>]
373 rounds off the corners of the image (to transparent
374 for PNG output), where <x> is the horizontal radius
375 of the curve and <y> is the vertical radius
376 - "elip" [ex: &fltr[]=elip]
377 similar to rounded corners but more extreme
378 - "mask" [ex: &fltr[]=mask|filename.png]
379 greyscale values of mask are applied as the alpha
380 channel to the main image. White is opaque, black
382 - "bvl" (BeVeL) [ex: &fltr[]=bvl|<w>|<c1>|<c2>]
383 where <w> is the bevel width, <c1> is the hex color
384 for the top and left shading, <c2> is the hex color
385 for the bottom and right shading
386 - "bord" (BORDer) [ex: &fltr[]=bord|<w>|<rx>|<ry>|<c>
387 where <w> is the width in pixels, <rx> and <ry> are
388 horizontal and vertical radii for rounded corners,
389 and <c> is the hex color of the border
390 - "fram" (FRAMe) draws a frame, similar to "bord" but
392 [ex: &fltr[]=fram|<w1>|<w2>|<c1>|<c2>|<c3>]
393 where <w1> is the width of the main border, <w2> is
394 the width of each side of the bevel part, <c1> is the
395 hex color of the main border, <c2> is the highlight
396 bevel color, <c3> is the shadow bevel color
397 - "drop" (DROP shadow)
398 [ex: &fltr[]=drop|<d>|<w>|<clr>|<a>]
399 where <d> is distance from image to shadow, <w> is
400 width of shadow fade (not yet implemented), <clr> is
401 the hex color of the shadow, and <a> is the angle of
402 the shadow (default=225)
403 - "crop" (CROP image)
404 [ex: &fltr[]=crop|<l>|<r>|<t>|<b>]
405 where <l> is the number of pixels to crop from the left
406 side of the resized image; <r>, <t>, <b> are for right,
407 top and bottom respectively. Where (0 < x < 1) the
408 value will be used as a percentage of width/height.
409 Left and top crops take precedence over right and
410 bottom values. Cropping will be limited such that at
411 least 1 pixel of width and height always remains.
412 file = if set then thumbnail will be rendered to this
413 filename, not output and not cached.
414 (Deprecated. Disabled by default since v1.6.0.
415 You should instantiate your own object instead)
416 goto = URL to redirect to after rendering image to file
417 * Must begin with "http://"
418 * Requires file parameter set
419 (Deprecated. Disabled by default since v1.6.0.
420 You should instantiate your own object instead)
421 err = custom error image filename instead of showing
422 error messages (for use on production sites)
423 md5s = MD5 hash of the source image -- if this parameter is
424 passed with the hash of the source image then the
425 source image is not checked for existance or
426 modification and the cached file is used (if
427 available). If 'md5s' is passed an empty string then
428 phpThumb.php dies and outputs the correct MD5 hash
429 value. This parameter is the single-file equivalent
430 of 'cache_source_filemtime_ignore_*' configuration
432 xto = EXIF Thumbnail Only - set to only extract EXIF
433 thumbnail and not do any additional processing
434 ra = Rotate by Angle: angle of rotation in degrees
435 positive = counterclockwise, negative = clockwise
436 ar = Auto Rotate: set to "x" to use EXIF orientation
437 stored by camera. Can also be set to "l" or "L"
438 for landscape, or "p" or "P" for portrait. "l"
439 and "P" rotate the image clockwise, "L" and "p"
440 rotate the image counter-clockwise.
441 aoe = Output Allow Enlarging - override the setting for
442 $CONFIG['output_allow_enlarging'] (1=on, 0=off)
443 ("far" and "iar" both override this and allow output
445 iar = Ignore Aspect Ratio - disable proportional resizing
446 and stretch image to fit "h" & "w" (which must both
447 be set). (1=on, 0=off) (overrides "far")
448 far = Force Aspect Ratio - image will be created at size
449 specified by "w" and "h" (which must both be set).
450 Alignment: L=left,R=right,T=top,B=bottom,C=center
451 BL,BR,TL,TR use the appropriate direction if the
452 image is landscape or portrait.
453 maxb = MAXimum Byte size - output quality is auto-set to
454 fit thumbnail into "maxb" bytes (compression
455 quality is adjusted for JPEG, bit depth is adjusted
457 down = filename to save image to. If this is set the
458 browser will prompt to save to this filename rather
459 than display the image
467 * Always use the local image filename wherever possible
468 rather than a full http:// URL because performance is
469 much better, less (or no) use of temp files, and the
470 last-modified check for cached files doesn't work for
471 remote files. For example:
472 good: phpThumb.php?src=/images/nicepic.jpg
473 bad: phpThumb.php?src=/home/httpd/example/images/nicepic.jpg
474 worse: phpThumb.php?src=http://example.com/images/nicepic.jpg
476 * Thumbnails will be scaled proportionately to fit in a
477 box of at most (width * height) pixels
478 (unless "iar" is set)
480 * Thumbnail caching for URL or database sources requires
481 an absolute directory name for $config_cache_directory
482 Physical file cached thumbnails will be recreated if
483 the source file changes, but remote/database files
484 cannot (modification time isn't readily available)
486 * If you need a GUI interface to phpThumb(), or for a user
487 to specify crop settings, or something like that please
488 see the list of known programs in /demo/readme.demos.txt
490 * Cropping images can be specified with either exact pixel
491 values for sx/sy/sw/sh parameters, or if those are set
492 to a value >0 and <1 then these are interpreted as a
493 percentage of the source image width/height. For example,
494 to crop 25% off all sides, you would specify parameters:
495 phpThumb.php?src=pic.jpg&sx=.25&sy=.25&sw=.5&sh=.5
497 * phpThumb() may have tempfile access issues on servers
498 where Safe Mode is enabled, specificly when accessing
499 a file over HTTP, or when a non-bundled version of GD
500 is in use. Specifying "config_temp_directory" may help
502 * Properly resolving /~user/ style filenames requires
503 apache_lookup_uri(), which is missing or broken in
504 Apache2, or if PHP is not installed as an Apache module.
505 phpThumb() does try and work around this if it is
506 unavailble, but you may have to specify a full filename
507 for "src" if you encounter problems.
509 * phpThumb() should work with PHP v4.0.6+, but seems to
510 have a few quirks before v4.1.0
511 EXIF thumbnail extraction requires PHP v4.2.0+
512 Image rotation requires PHP v4.3.0+. There have been
513 reports of problems with PHP older than v4.3.3
514 Some image filters require PHP v5.0.0+
515 Run /demo/phpThumb.demo.check.php to examine your server
517 * phpThumb() works better and faster when ImageMagick is
518 available. Most functions will work with only GD2, but
519 speed is much faster with ImageMagick, and much larger
520 images can be processed with ImageMagick than GD.
522 * phpThumb() works with GD v1.x, but works better with
523 GD v2.0+ because of the true-color image support
524 and ImageCopyResampled(). Also, there appears to be a
525 bug in ImageCopyResized() which is used with GD v1.x
526 where the bottom and/or right line of pixels is set
527 to the background color (due to a rounding error?)
528 NOTE: Please use the bundled version of GD if at all
529 possible (with PHP v4.3.0+) because the non-bundled
530 version has bugs which may cause PHP to crash:
531 * http://bugs.php.net/bug.php?id=21518
532 * http://bugs.php.net/bug.php?id=24174
533 phpThumb() has a workaround for the above bug but
534 there may be other bugs, and the workaround is slow.
535 Alpha transparent output requires GD >= 2.0.1 and
537 Most (if not all) filters require GD v2.x to function
538 at all. But many filters can be handled by ImageMagick
541 * Filters handled by ImageMagick or GD:
542 - brit;cont;ds;sat;gray;clr;sep;gam;neg;th;rcd;flip;edge;
543 emb;lvl;blur;gblr;usm;
544 * Filters handled only by ImageMagick:
546 * Filters handled only by GD + PHP5:
548 * Filters handled only by GD2:
549 - bvl;wmi;wmt;over;wb;hist;fram;drop;mask;elip;ric;bord;