Loading images

Loading an image is easy — you only have to call the WideImage::load() static method and specify the image source. WideImage tries to detect whether the source is a file, url, upload, a binary string or a valid GD image resource (image handle). WideImage supports the most common image formats.

  1. $image = WideImage::load();

From a file or URL

You can use the WideImage::loadFromFile() static method when loading from a file.

  1. $img = WideImage::load('path/to/image.jpg');
  2. // or
  3. $img = WideImage::loadFromFile('path/to/image.jpg');

WideImage uses the imagecreatefromstring() function to load the image no matter what extension the file has.

If you have allow_url_fopen directive enabled in your php.ini, you can also load images by URL address:

  1. $img = WideImage::load('http://example.com/image.gif');
  2. // or
  3. $img = WideImage::loadFromFile('http://example.com/image.gif');

This method works with any supported protocol/wrapper, such as http, https, ftp, ftps, …

From an uploaded file

You can load an image directly from $_FILES if you either call the generic WideImage::load() or WideImage::loadFromUpload() with post field name as the source.

For example, if you add a field with type=file and name=myimg in the HTML:

  1. <input type="file" name="myimg" />

You can load the image like this:

  1. $img = WideImage::load('myimg');
  2. // or
  3. $img = WideImage::loadFromUpload('myimg');
  5. // behind the scenes, the above is just a shortcut for:
  6. $img = WideImage::loadFromFile($_FILES['myimg']['tmp_name']);

Array fields are supported since 11.02.19:

  1. <input type="file" name="img[]" />
  2. <input type="file" name="img[]" />
  3. <input type="file" name="img[]" />
  1. $img = WideImage::load('img');
  2. // will return an array of loaded images
  4. $img = WideImage::loadFromUpload('img', 2);
  5. // will return a single image, loaded from the array with index 2

From a binary string

If you have a binary string that holds the image data, you can load it by passing the data to the WideImage::load() or WideImage::loadFromString() methods:

  1. $img = WideImage::load($data);
  2. // or
  3. $img = WideImage::loadFromString($data);

This comes in handy if you are loading images from a database, where your image data is stored in a blob field, and you retrieve the row with a query.

From a valid image resource

If you have already created an image outside WideImage (for example, with some other image library, or with php GD functions), you can pass the image resource to either WideImage::load() or WideImage::loadFromHandle() method to create a WideImage_Image object:

  1. $image_handle = imagecreatefromjpeg('path/to/a/pic.jpg');
  2. $img = WideImage::load($image_handle);
  3. // or
  4. $img = WideImage::loadFromHandle($image_handle);

When you load an image from a resource, the object takes ownership of the passed resource, which means the resource will be destroyed when this newly created image object is destroyed. You can change that by calling releaseHandle() method on the object; if the handle is released, the original resource will not be destroyed with the object, so you’ll have to destroy it by yourself.

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