DynamicImage
, (*1)
Create dynamically sized images on the fly so that they don't need to be resized when added or uploaded., (*2)
The goal of this script is to allow images to be generated on the fly to any size. This is done by using PHPs imagick libraries to creating images in a fraction of a second., (*3)
This script has a fully functioning file based cache system which renews when original images are updated. Images are generated to scale and generated to high resolution. There is also support for images that are hosted on seperate endpoints., (*4)
JPG
, JPEG
, PNG
and GIF
images are supported., (*5)
Params
The parameters are expected as a key
, value
array and passed when the object is created., (*6)
filename
- required
This is the original image filename including it's extension, for example, snorkel.jpg
. This image can be of any size any will be rendered in near perferct resolution., (*7)
width
- required
This is the desired width of the image., (*8)
height
- required
This is the desired height of the image., (*9)
image_directory
- This is the directory that all of your original images are held in. This cannot move 'up' through your filesystem to keep the script as a whole, more secure. This MUST also end with a trailing slash., (*10)
debug
- If you wish for errors to be turned on, then you can set this to true
to display any errors that may be occuring., (*11)
image_missing
- If the requested image isn't found then this image is returned instead. This means that you never get a blank response and there's always something that you can display., (*12)
exceptions
- Parameter to set whether or not you wish for exceptions to be thrown by the script. The default value is false
so if you want exceptions to be thrown then you must set this to true
when creating the object., (*13)
validate_image
- Flag to indicate whether or not you want to check for the requested images' existance before looking to see if the image exists in cache. This means that if the image gets updated after it's been saved to cache then the image wont be regenerated. By default this is set to true
., (*14)
invalidate_cache
- Parameter to set whether or not the cache image should be invalidated after x amount of seconds. This means you can re-generate cache images every day for example., (*15)
Example AJAX request to load an image from a seperate domain
Request URL, (*16)
http://yousite.com/loadimage.php?filename=snorkel.jpg&width=500&height=500
, (*17)
loadimage.php, (*18)
$_GET['filename'],
'width' => $_GET['width'],
'height' => $_GET['height']
];
$DI = new DynamicImage($settings);
echo json_encode($DI->file);
?>
HTML, (*19)
<img src="<?=$DI->file;?>">
If you have your images saved in a seperate folder, such as /images
, you can set Dynamic Images to use that folder like so:, (*20)
$_GET['filename'],
'width' => $_GET['width'],
'height' => $_GET['height'],
'image_directory' => 'images/'
];
$DI = new DynamicImage($settings);
echo json_encode($DI->file);
?>
Potential Issues
Make sure that your cache folder is writable and that the apache user has permission to write on your machine. Usually user www-data
., (*21)