Image Cache is a very simple PHP class that accepts an image source and will compress and cache the file, move it to a new directory, and returns the new source for the image.
Support this project by donating on Gittip.
This is used on travis-ci.org for continuous integration testing.
You can either install the script manually using the require
method:
require 'ImageCache.php';
Or (preferred) you can install the script with Composer.
Install Composer in the root directory of your project, and create a composer.json
file.
In your composer.json
file:
{
"require" : {
"nielse63/phpimagecache": "dev-master"
}
}
This is currently the first release of Image Cache, so in this example you'll be able to update your script with any updates made to Image Cache. If, however, you don't want access to any potential updates, remove the tilda form the "version" value.
Navigate to your project root and run the install
command of composer.phar
.
$ php composer.phar install
From there, include the vendor/autoload.php
file in your project, and initialize the class as normal.
To test the script, install the full project on your server and navigate to the test directory. This ccontains an index file with example functions and an image directory with several images. Run the test/index.php
file to ensure that the script is compressing and compiling the sample images. If working correctly, a new directory, "compressed" will appear in your images folder.
Include the script in your project either with Composer or via the manual require
method and create a new instance of the class, using the appropriate parameters if needed:
$image = new ImageCache\ImageCache();
Possible parameters include:
$image = new ImageCache\ImageCache(
$filebase = '', $dir = null, $create_dir = true, $opts = array()
);
/**
* @param $filebase (string) - The base URL that will be included in the final output for the image source; used if image source is an absolute URL
* @param $dir (string/null) - The base directory that houses the image being compressed
* @param $create_dir (bool) - Whether or not to create a new directory for the compressed images
* @param $opts (array) - An array of available options that the user can include to the overwrite default settings
*/
Then compress the image by calling it by it's filename:
$compressed = $image->compress('image.png');
This will return an array of information on the compressed image, including the source of the compressed image, the height, and the width. It can be included in your PHP file as such:
<img src="<?php echo $compressed['src']; ?>" height="<?php echo $compressed['height']; ?>" width="<?php echo $compressed['width']; ?>">
Future development changes include:
- Integration with Tim Thumb and other image cropping libraries
- Increase browser capabilities (and further testing down to IE 8)
- Fixing the issue with 304 headers being ignored
- More options, better documentation, more forms of output, etc.
Contributing to the project would be a massive help in maintaining and extending the script. It has a lot of potential, and any help would be awesome.
If you're interested in contributing, issue a pull request on Github or email me directly at erik@312development.com.
Creative Commons Attribution Lisence:
http://freedomdefined.org/Licenses/CC-BY
Allows for files to be ignored in builds.
Support this project by donating on Gittip.