Class: image_factory
Source Location: /image_factory.php
Class image_factory
Class Overview
|
Dynamic, blog/rss image generator. Takes a feed from a wordpress blog and creates an image based on the configuration properties of the object.
Located in /image_factory.php [line 21]
Author(s):
Information Tags:
|
Properties
|
Methods
|
Property Summary
| string |
$background |
Sets the background to be used for the image. This can either be a relative path (from the script) to an image on the server or a hexidecimal color $fontcolor re hex. White will be used if image does not exist or hex value is not valid. |
| array |
$bgdetails |
An array of the data returned by getimagesize |
| string |
$cache_directory |
The directory (relative from the script (if set through the method)) of where cached images will be stored. |
| string |
$content_after |
Text content to place after the dynamic content from the feed. If NULL or false, it will not be used |
| string |
$content_before |
Text content to place before the dynamic content from the feed. If NULL or false, it will not be used |
| int |
$content_x |
The x coordinate where the main content will be placed in the output image. Default will be 0 (snug to left) No check will be mad eto ensure text fits within image. |
| int |
$content_y |
The y coordinate where the main content will be placed in the output image. Default will be 0 ( snug to top) |
| array |
$errors |
An array of errors that have accumulated throughout the script. |
| array |
$excluded |
An array of the properties that SHOULD NOT be configurable via the $_GET string. This will stop someone dodgy messing stuff up. You may also want to exclude some properties like the feed, the width, height. |
| array |
$extra_tags |
The additional XML tags to retrieve from the RSS feed entry. TITLE is mandatory. These will use $font and $fontcolor |
| mixed |
$font |
Relative path (from script) the font file to use in image generation or an integer |
| string |
$fontcolor |
A six character hexidecimal color string RRGGBB eg, FF00CC |
| int |
$height |
The height of the image to output |
| array |
$mime_types |
An array of mime types that a background image can be. |
| array |
$parsed_xml |
XML in $xml will be parsed using xml_parse_into_struct, this is an associative array of pointers and data |
| int |
$resample_width |
Due to gdf fonts only having one size, you may get your image looking just right only to find |
| string |
$site_feed |
The url of the RSS feed that will be the source of the dynamic text in the created image. |
| int |
$width |
The width of the image to output |
| string |
$xml |
Contains the full, unparsed xml of the RSS feed |
Method Summary
| image_factory |
__construct() |
The contructor function takes an array of data to build the image from. |
| string |
check_font() |
Check to see if the given font has a file on the server or that it is a number from 1-5. |
| string |
check_hex() |
Checks to ensure the given value is a valid RGB hexidecimal color |
| string |
clean_path() |
Takes the strings sent through as paths and cleans them remove preceding forward slashes. |
| void |
clear_cache() |
Clears out the cache when the dynamic content changes. (when a new blog post occurs), only deletes .jpg images |
| void |
dump_errors() |
For debugging purposes, this dumps the stored $errors within some html pre tags |
| string |
generate_filename() |
Generates a unique filename based on the current object setup. |
| string |
generate_image() |
generates the image that will be returned based on member properties. |
| string |
get_latest_entry() |
Gets the title of the latest blog entry. Currently this is hardcoded in based on a WordPress RSS feed. |
| bool |
is_hex() |
Checks if a string is hex |
| void |
set_background() |
Checks a given background to see whether it is a file on the server or |
| void |
write_to_cache() |
Takes the image resource in $img and writes it to cache/filename.jpg. |
Properties
Sets the background to be used for the image. This can either be a relative path (from the script) to an image on the server or a hexidecimal color $fontcolor re hex. White will be used if image does not exist or hex value is not valid.
API Tags:
An array of the data returned by getimagesize
API Tags:
The directory (relative from the script (if set through the method)) of where cached images will be stored.
API Tags:
Text content to place after the dynamic content from the feed. If NULL or false, it will not be used
API Tags:
The x coordinate where the $content_after will be placed. Default will be 0 (snug to left)
API Tags:
The y coordinate where the $content_after will be placed. Default will be directly underneath $content;
API Tags:
Text content to place before the dynamic content from the feed. If NULL or false, it will not be used
API Tags:
The x coordinate where the $content_before will be placed. Default will be 0 (snug to left)
API Tags:
The y coordinate where the $content_before will be placed. Default will be 0 (snug to top);
API Tags:
The x coordinate where the main content will be placed in the output image. Default will be 0 (snug to left) No check will be mad eto ensure text fits within image.
API Tags:
The y coordinate where the main content will be placed in the output image. Default will be 0 ( snug to top)
Or if $content_before is set the text will be placed directly underneath $content_before No check will be made to ensure text fits within image API Tags:
An array of errors that have accumulated throughout the script.
API Tags:
An array of the properties that SHOULD NOT be configurable via the $_GET string. This will stop someone dodgy messing stuff up. You may also want to exclude some properties like the feed, the width, height.
Note that site feed is in here initially. Think carefully before taking it out as it adds the potential for people to feed from your server to promote their own sites. API Tags:
The additional XML tags to retrieve from the RSS feed entry. TITLE is mandatory. These will use $font and $fontcolor
API Tags:
Relative path (from script) the font file to use in image generation or an integer
1-5 that defines a GD module default font to use API Tags:
A six character hexidecimal color string RRGGBB eg, FF00CC
This will be validated prior to use, if given value is not hex, black (000000) will be used API Tags:
The height of the image to output
API Tags:
Sets the output jpeg quality (1-100)
API Tags:
An array of mime types that a background image can be.
API Tags:
XML in $xml will be parsed using xml_parse_into_struct, this is an associative array of pointers and data
see http://php.net/xml_parse_into_struct for details of this function API Tags:
The HTTPRequest
API Tags:
Due to gdf fonts only having one size, you may get your image looking just right only to find
that it's too big or small. Use this variable to resample from the initial width to a different size same for height API Tags:
The url of the RSS feed that will be the source of the dynamic text in the created image.
API Tags:
The width of the image to output
API Tags:
Contains the full, unparsed xml of the RSS feed
API Tags:
Methods
static image_factory generate_from_request(
$request
$request, [$sep
$defaults = array()], [$hyp
$sep = 'qp789qp789'], [$defaults
$hyp = '56ty65']
)
|
|
Takes $_SERVER['REQUEST_URI'] and parses it into an associative array for use with __construct()
The URL should be in the format /key-value/key-value -- any slash should be replaced with $sep
{
$vars = array();
foreach ($request as $var)
{
if (!empty($bits[1]))
}
{
$defaults = array();
}
}
Parameters:
|
$request |
$request: |
string a url string |
|
$sep |
$defaults: |
string the separator string for slashes in values |
|
$hyp |
$sep: |
string the separator string for hyphens in values |
|
$defaults |
$hyp: |
If you want to pass through some default values, pass it as an associative array |
API Tags:
| Return: | returns an instance of image_factory |
image_factory __construct(
[$data
$data = false]
)
|
|
The contructor function takes an array of data to build the image from.
Loops through class properties, checks if they are in $excluded and if they aren't checks to see if $data[propertyname] is set, if it is, it assigns the value to the property via the setter function for that property. This allows the data to be cleaned, only setting the proprty if it is the expected value.
{
foreach ($this as $prop => $default_value)
{
continue;
if (!empty($data[$prop]))
{
$method_to_run = 'set_' . $prop;
// Check to ensure there is a setter function
{
// try and set the property, else catch it in the errors array
try {
$this->$method_to_run($data[$prop]);
} catch (Exception $e) {
$this->errors[$prop . '_value_error'] = $e->getMessage();
}
}
}
}
}
try {
} catch (Exception $e) {
$this->errors['XML_parse_error'] = $e->getMessage();
}
}
Parameters:
API Tags:
Clean up a bit
{
}
API Tags:
bool check_feed_validity(
[
$value = false]
)
|
|
Checks $site_feed to ensure it is a valid rss feed
{
// get the headers to ensure we have xml
return false;
//var_dump($request);
if (!preg_match('/^(text\/xml)|(application\/xml);/', $request['Content-Type']))
return false;
return true;
}
Parameters:
API Tags:
| Return: | true on success, false on failure |
| Access: | public |
string check_font(
$val
$val
)
|
|
Check to see if the given font has a file on the server or that it is a number from 1-5.
{
{
if ($val < 0 || $val > 5)
{
throw new Exception('Font must be a number 0 - 5');
}
$font = $val;
} else {
throw new Exception('Font must be a .gdf file');
throw new Exception('Font ' . $val . ' does not exist on server');
throw new Exception('Could not load font ' . $val);
}
return $font;
}
Parameters:
|
$val |
$val: |
mixed either a relative string or integer |
API Tags:
| Return: | returns given svalue if validates as valid font |
| Access: | private |
string check_hex(
$val
$val
)
|
|
Checks to ensure the given value is a valid RGB hexidecimal color
{
{
throw new Exception('Given color is not valid. Correct format is AAEE00 (RRGGBB)');
}
return $val;
}
Parameters:
API Tags:
| Return: | returns given value if validates as a hex string |
| Access: | private |
string clean_path(
$val
$val
)
|
|
Takes the strings sent through as paths and cleans them remove preceding forward slashes.
Also checks to ensure that it's not trying to use another protocol such as http, ftp or file. Returns the string with offending starting characters removed.
{
return preg_replace('/^([\/\\\\]+)|(.{,8}tp:\/\/)/', '', $val);
}
Parameters:
|
$val |
$val: |
string The string to be checked and cleaned. |
API Tags:
| Return: | Returns the cleaned string |
| Access: | private |
Clears out the cache when the dynamic content changes. (when a new blog post occurs), only deletes .jpg images
{
while (false !== ($file = $dir->read()))
{
}
}
API Tags:
For debugging purposes, this dumps the stored $errors within some html pre tags
{
}
API Tags:
string generate_filename(
)
|
|
Generates a unique filename based on the current object setup.
This generation includes member properties and xml data to allow consistency
{
// loop through properties and create a string of the property values that
// are allowed to be changed
$filename = '';
foreach ($this as $name => $prop)
{
continue;
$filename .= $this->$name;
}
// now throw the dynamic content into it
// Now hash it and return md5 should be unique enough for this purpose
return md5($filename) . '.jpg';
}
API Tags:
| Return: | returns a string filename. |
| Access: | private |
string generate_image(
[$check_cache
$check_cache = true], [$add_cache
$add_cache = true]
)
|
|
generates the image that will be returned based on member properties.
{
// If we're checking from the cache, we'll use that image first
return $img;
// Create the image foundation
// set the background of the image, either from another image or with a color
{
} else {
// We are using a background image on the server
// load the appropriate function
// now copy the backgroun dto our new image
// we can get rid of the background image now
}
// Now it's time to load the text into the file image
{
// load up the color
}
// Now we'll add the content
//Check to see if 1. content_y is set, if not check if content_before is set and if it is 3. check if content_before_y is set
// This will determine the y location of the content text
else
$y=0;
// Now finally, add the content after if it is set
{
// Either place where it was told to go or place after the main content
}
// Now finally check to see if we need to resample the width and/or height
{
if ($add_cache)
}
if ($add_cache)
}
Parameters:
|
$check_cache |
$check_cache: |
boolean If true, cache will be checked to see if an image already exists with same parameters. If one is found, this will be returned. |
|
$add_cache |
$add_cache: |
boolean If true and an image is not returned after checking the cache, this image will be written to the cache |
API Tags:
| Return: | binary string of an image |
| Access: | public |
resource generate_image_color(
resource
$img, string
$color
)
|
|
Allocates a color to an image for use in the output.
{
return $color;
}
Parameters:
|
resource |
$img: |
an image to allocate a colour to |
|
string |
$color: |
a hex string |
API Tags:
| Return: | an allocated image color |
| Access: | private |
Returns the data from a file in the cache
{
// Returns image data from cache
else
return false;
}
API Tags:
string get_latest_entry(
)
|
|
Gets the title of the latest blog entry. Currently this is hardcoded in based on a WordPress RSS feed.
{
}
API Tags:
| Return: | The title of the latest blog post |
| Access: | public |
Information Tags:
| Todo: | Make the data retrieved from the RSS feed configurable. There should be the ability to choose what XML NODE is returned |
Checks if a string is hex
{
try {
return true;
} catch (Exception $e) {
return false;
}
}
Parameters:
API Tags:
| Return: | true for valid hex, false otherwise |
| Access: | private |
Parses the xml returned by $site_feed into $data and $pointers
This is stored for use in get_latest_entry
{
throw new Exception('XML document could not be parsed');
}
API Tags:
void set_background(
$val
$val
)
|
|
Checks a given background to see whether it is a file on the server or
a hexidecimal color value
{
try {
} catch (Exception $e) {
if (!preg_match('/(.jpg|.jpeg|.gif|.png)$/i', $val))
{
throw new Exception($e->getMessage() . ' and is not a valid image');
}
{
throw new Exception($val . ' does not exist on the server');
}
{
throw new Exception($val . ' is not a valid image, valid images are ' . implode( ', ', $this->mime_types));
}
}
}
Parameters:
|
$val |
$val: |
string relative file on server or hex crgb color string |
API Tags:
void set_cache_directory(
$val
)
|
|
..
{
}
Parameters:
API Tags:
void set_content_after(
$val
)
|
|
..
{
throw new Exception('content_after must be a string');
else
}
Parameters:
API Tags:
void set_content_after_font(
$val
)
|
|
..
{
}
Parameters:
API Tags:
void set_content_after_fontcolor(
$val
)
|
|
..
{
}
Parameters:
API Tags:
void set_content_after_x(
$val
)
|
|
..
{
throw new Exception('content_after_x must be a number');
else
}
Parameters:
API Tags:
void set_content_after_y(
$val
)
|
|
..
{
throw new Exception('content_after_y must be a number');
else
}
Parameters:
API Tags:
void set_content_before(
$val
)
|
|
..
{
throw new Exception('content_before must be a string');
else
}
Parameters:
API Tags:
void set_content_before_font(
$val
)
|
|
..
{
}
Parameters:
API Tags:
void set_content_before_fontcolor(
$val
)
|
|
..
{
}
Parameters:
API Tags:
void set_content_before_x(
$val
)
|
|
..
{
throw new Exception('content_before_x must be a number');
else
}
Parameters:
API Tags:
void set_content_before_y(
$val
)
|
|
..
{
throw new Exception('content_before_y must be a number');
else
}
Parameters:
API Tags:
void set_content_x(
$val
)
|
|
..
{
throw new Exception('content_x must be a number');
else
}
Parameters:
API Tags:
void set_content_y(
$val
)
|
|
..
{
throw new Exception('content_y must be a number');
else
}
Parameters:
API Tags:
void set_dynamic_content(
)
|
|
API Tags:
..
{
}
Parameters:
API Tags:
void set_fontcolor(
$val
)
|
|
..
{
}
Parameters:
API Tags:
..
{
throw new Exception('Height is not a valid number');
else
}
Parameters:
API Tags:
void set_image_quality(
$val
$val
)
|
|
Sets the value of $image_quality... uses default value if not an int 1-100
Parameters:
|
$val |
$val: |
int output quality of the image |
API Tags:
void set_resample_height(
$val
)
|
|
..
{
throw new Exception('Resample Height must be a number');
else
}
Parameters:
API Tags:
void set_resample_width(
$val
)
|
|
..
{
throw new Exception('Resample Width must be a number');
else
}
Parameters:
API Tags:
void set_site_feed(
$url
)
|
|
..
{
throw new Exception('Site feed is either not a valid URL or does not address a valid XML file');
else
}
Parameters:
API Tags:
..
{
throw new Exception('Width is not a valid number');
else
}
Parameters:
API Tags:
Takes the image resource in $img and writes it to cache/filename.jpg.
Runs clear_cache() if dynamic content has changed
{
// Check if image has been written yet
{
}
// Throw an exception if we still don't have an image
{
throw new Exception('Could not write to cache as image could not be generated');
}
// Check to see if the content has changed at all
$write_file = true;
{
else
$write_file = false;
}
if ($write_file) {
}
}
API Tags:
|
|