A simple gallery example

In this typical gallery example, I’ll show how you load an image from an uploaded file, resize to two different sizes, and apply watermark to them.

First, load an image directly from an uploaded file, presented in an input file field named “pic”.

  1. try
  2. {
  3.   $image = WideImage::load('pic');
  4. }
  5. catch (Exception $e)
  6. {
  7.   echo "Image isn't valid";
  8. }

Then, resize the image to a decent size, so that you avoid too large images.

  1. $image = $image->resize(1600, 1200);

Crop a 300×300 rectangle from the middle, resize to 100×100, and save a thumb.

  1. $image->crop('center', 'center', 300, 300)->
  2.     resize(100, 100)->saveToFile('image-thumg.png');

Sharpen the image up a bit, and apply watermark to the bottom-right corner of the image.

  1. $logo = WideImage::load('logo.png');
  2. $large = $image->unsharp(80, 0.5, 3)->merge($logo, 'right', 'bottom');

Show the image size and save the image.

  1. echo $large->getWidth() . 'x' . $large->getHeight();
  2. $large->saveToFile('image-large.png');

That is all.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Tumblr
  • Twitter