The Idea
We would like to use the URL of the image to supply our wishes to the script. A possible URL could be: http://www.example.com/img.php?f(3cb7f702a5967)+w(300) Using this URL, we would like to get the image identified by 3cb7f702a5967, resized to a width of 300 pixels and the corresponding height. The possibilities of our newly created script will be:
- resize an image to a given width and/or height, possibly preserve the aspect ration
- accept new image sizes as absolute values, and as percentages of the original size
- resize an image when width or height exceed a given limit;
- return the image as a PNG or JPEG-file.
What We Need
To build and use this script, you will need PHP compiled with the GD library and JPEG-support.
The Syntax
For telling our script what to return, we will use the following syntax. The arguments are delimited by one or more +-es. An example URL could be: http://www.example.com/img.php?f(3cb7f702a5967)+w(300)
| Code |
Meaning |
| f(3cb7f702a5967) |
The 13-character filename of the requested image. The images are saved without an extension, with the name defined by the current value of uniqid();. That way, new uploaded files will automatically have an unique id. |
| w(123) or w(10%) |
The wanted width of the returned image, either in absolute pixels or relative to the original size. |
| h(123) or h(10%) |
Same as above, but this is for the height of the image. When just one of the size-commands are given, the other size is automatically calculated, so that de aspect ratio of the image stays the same. |
| x(123) |
Defines the maximum width of the returned image. Resizing only takes place if the original width exceeds the given maximum. Only absolute values are accepted. |
| y(123) |
Same but for height. |
| t(png|jpg) |
Defines the requested file type of the image. If no type is given, the image is returned as the original saved type. |
| q(100) |
Only for JPEG. Asks for a specific quality of the returned image. Quality can vary from 0 to 100. |