Easy resize, filter and compression class for images. Check out the examples and source code.
All image manipulations are applied to a temporary image. Only the save() method is permanent.
Requires: PHP5 and GD2 library.
Optional: jpegtran, pngcrush.
Example 1:
include 'ImageUtil.php'; // an absolute URL or path to file. Can be .jpg, .jpeg, .gif or .png $resize = new ImageUtil('http://www.mikkonen.info/imageutil/images/large/sample_1.jpg'); // set width and height $resize->resize(320, 240) // path to file. Can be .jpg, .jpeg, .gif or .png. Directory must be writable. ->save('images/small/output_1.jpg');
Example 2:
include 'ImageUtil.php'; $options = array( 'jpegtran_path' => '/path/to/jpegtran', 'pngcrush_path' => '/path/to/pngcrush', ); // optionally use different jpegtran/pngcrush path or both $resize = new ImageUtil('images/large/sample_1.jpg', $options); // set width, height and optional resize style: 'exact', 'portrait', 'landscape', 'auto' or 'crop' $resize->resize(320, 240, 'crop') // filename and optionally the save quality ->save('images/small/output_2.jpg', 70);
Example 3:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_1.jpg'); $resize->resize(320, 240, 'landscape') // add watermark image ->watermark('images/large/watermark.png') ->save('images/small/output_3.jpg');
Example 4:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_1.jpg'); $resize->resize(320, 240, 'auto') // set filename, transparency, padding and position: 'TL', 'TR', 'BL' or 'BR' ->watermark('images/large/watermark.png', 60, 5, 'TR') ->save('images/small/output_4.jpg');
Example 5:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_1.jpg') $resize->resize(320, 240); // set image to greyscale ->greyscale() // brightness -255 to 255 ->brightness('-10') ->watermark('images/large/watermark.png')->save('images/small/output_5.jpg');
Example 6:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_1.jpg'); $resize->resize(320, 240) // smoothness -12 to 12. Values outside of the range -8 to 8 are usually unusable. ->smooth(5) ->save('images/small/output_6.jpg');
Example 7:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_1.jpg'); $resize->resize(320, 240) // blur 'gaussian' or 'selective' ->blur('gaussian') ->save('images/small/output_7.jpg');
Example 8:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_3.jpg'); $resize->resize(320, 240) // set sepia ->sepia() ->save('images/small/output_8.jpg');
Example 9:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_3.jpg'); $resize->resize(320, 240) // optional RGB value: '94,38,18' and brightness -20 ->sepia('94,38,18', -20) ->save('images/small/output_9.jpg');
Example 10:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_3.jpg'); $resize->resize(320, 240) // interlace image ->interlace() ->save('images/small/output_10.jpg');
Example 11:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_3.jpg'); $resize->resize(320, 240) // size of scatter ->scatter(3) ->save('images/small/output_11.jpg');
Example 12:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_3.jpg'); $resize->resize(320, 240) // size of pixels ->pixelate(8) ->save('images/small/output_12.jpg');
Example 13:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_3.jpg'); $resize->resize(320, 240) // noise 0 to 255 ->noise(10) ->save('images/small/output_13.jpg');
Example 14:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_3.jpg'); $resize->resize(320, 240) // true or false. Uses sharpen by default. ->sharpen(false) ->save('images/small/output_14.jpg');
Example 15:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_3.jpg'); $resize->resize(320, 240)->save('images/small/output_15.jpg');
Example 16:
include 'ImageUtil.php'; $options = array( 'pngcrush_path' => '/path/to/pngcrush', ); $resize = new ImageUtil('images/large/sample_2.jpg', $options); $resize->resize(320, 240, 'auto') // contrast -255 to 255 ->contrast(10) // add watermark ->watermark('images/large/watermark.png', 40, 2, 'BL') // Save as png ->save('images/small/output_16.png');
Example 17:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_2.jpg'); $resize->resize(320, 240, 'auto')->contrast(10)->watermark('images/large/watermark.png', 40, 2, 'BL') // Same as above, but save as gif ->save('images/small/output_17.gif');
Example 18:
include 'ImageUtil.php'; $options = array( 'jpegtran_path' => '/path/to/jpegtran', ); $resize = new ImageUtil('images/large/sample_2.jpg', $options); $resize->resize(320, 240, 'auto')->contrast(10)->watermark('images/large/watermark.png', 40, 2, 'BL') // Same as above, but save as jpg ->save('images/small/output_18.jpg');
Example 19:
include 'ImageUtil.php'; $options = array( 'pngcrush_path' => '/path/to/pngcrush', ); $resize = new ImageUtil('images/large/sample_3.jpg', $options); // optional. Width and height divided by maximum circle size should be zero. $resize->resize(636, 480, 'crop') // optional ->greyscale() // maximum circle size in pixels ->rasterbate(6) // optional ->blur() ->save('images/small/output_19.png');
Example 20:
include 'ImageUtil.php'; $options = array( 'jpegtran_path' => '/path/to/jpegtran', ); $resize = new ImageUtil('images/large/sample_3.jpg', $options); $resize->resize(220, 220, 'crop') // mask image and top left position of resized image ->mask('images/polaroid.png', 17, 17) ->save('images/small/output_20.jpg');
Example 21:
include 'ImageUtil.php'; $options = array( 'jpegtran_path' => '/path/to/jpegtran', ); $resize = new ImageUtil('images/large/sample_3.jpg', $options); $resize->resize(220, 220, 'crop') // mask image, top left position of resized image and background color ->mask('images/polaroid_transparent.png', 17, 17, 'dddddd') ->save('images/small/output_21.jpg');
Example 22:
include 'ImageUtil.php'; $options = array( 'jpegtran_path' => '/path/to/jpegtran', ); $resize = new ImageUtil('images/large/sample_3.jpg', $options); $resize->resize(220, 220, 'crop')->mask('images/polaroid.png', 17, 17) // rotation angle ->rotate(-5) ->save('images/small/output_22.jpg');
Example 23:
include 'ImageUtil.php'; $options = array( 'jpegtran_path' => '/path/to/jpegtran', ); $resize = new ImageUtil('images/large/sample_3.jpg', $options); $resize->resize(220, 220, 'crop')->mask('images/polaroid_transparent.png', 17, 17, 'dddddd') // rotation angle and background color ->rotate(-5, 'dddddd') ->save('images/small/output_23.jpg');
Example 24:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_3.jpg'); $resize->resize(320, 240) // filename, save quality and don't destroy resized image resource on save ->save('path/to/output1.jpg', 85, false); // add more filters to resized image ->sepia() // destroy resized image resource on save ->save('path/to/output2.jpg', 85, true);
Example 25:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_3.jpg'); // resize original image and save $resize->resize(320, 240, 'crop')->save('path/to/output1.jpg'); // resize original image again and save $resize->resize(200, 200, 'crop')->save('path/to/output2.jpg'); // resize original image again and save $resize->resize(100, 100, 'crop')->save('path/to/output3.jpg');
Example 26:
include 'ImageUtil.php'; $resize = new ImageUtil('images/large/sample_3.jpg'); // resize original image and output without saving $resize->resize(320, 240, 'crop')->render('output.jpg');
Polaroid Gallery is free and unencumbered public domain software.