relevance_pic()
Syntax:
string relevance_pic ( relevance [, min_color [, max_color ]] )
Arguments:
| unsigned int | relevance | specified relevance; linearly proportional to generated image width
| | string | min_color | color associated with the minimum relevance; must be in the form of hexidecimal RGB string;
optional argument
| | string | max_color | color associated with the maximum relevance; must be in the form of hexidecimal RGB string;
optional argument
|
Description:
Useful for dynamically generating 'relevance' indicator images common to search engines. This function will generate a .jpg image based on the specified parameters and will return an 'img' tag with reference to the generated image. The function will create its own folder 'images'
if it does not exist. The images are stored according to their parameters; the function will only generate the image if it does not find an existing file name matching the current parameters. No garbage collection is performed on the images as the function automatically reuses previously generated images and there are only 100 possible files per color permutation: negligible disk space assuming a small set of color permutations. On the server side, this is certainly not the simplest approach to this kind of functionality but compared to its html equivalent it is a solution more independent of the browser's parser. Your php installation must have GD support enabled.
Source:
<?php
/* builds relevance indicator bar image */
function relevance_pic(
$relevance,
$min_color='#9999cc',
$max_color='#ffaa33'
){
$pics_folder = "images";
/* strip pound sign from hex string */
$max_color=str_replace("#","",$max_color);
$min_color=str_replace("#","",$min_color);
/* create image folder if not exist */
if(!is_dir("$pics_folder")){
mkdir("$pics_folder",0777);
}
/* limit image size */
$max_rel = 100;
if($relevance>$max_rel){
$relevance=$max_rel;
}
/* create image if not exist */
if(!is_file("$pics_folder/pic".$relevance."_"
.$min_color.$max_color.".jpg")){
/* convert hex to RGB decimal equivalents */
$r_hot = hexdec(substr($max_color, 0, 2));
$g_hot = hexdec(substr($max_color, 2, 2));
$b_hot = hexdec(substr($max_color, 4, 2));
$r_cold = hexdec(substr($min_color, 0, 2));
$g_cold = hexdec(substr($min_color, 2, 2));
$b_cold = hexdec(substr($min_color, 4, 2));
/* image size boundaries */
$max_width = 30;
$min_width = 5;
$height = 8;
/* initialize image of max 30px width */
$pic=ImageCreate(
$relevance*$max_width/$max_rel+$min_width,
$height);
/* determine image color based on boundary colors */
$col2=ImageColorAllocate(
$pic,
$r_cold+($r_hot-$r_cold)*$relevance/$max_rel,
$g_cold+($g_hot-$g_cold)*$relevance/$max_rel,
$b_cold+($b_hot-$b_cold)*$relevance/$max_rel);
/* clean up vars */
unset($r_hot);
unset($g_hot);
unset($b_hot);
unset($r_cold);
unset($g_cold);
unset($b_cold);
/* fill image */
ImageFilledRectangle(
$pic,
0,0,
$relevance*$max_width/$max_rel+$min_width,$height,
$col2);
/* create image file */
ImageJPEG(
$pic,
"$pics_folder/pic"
.$relevance."_".$min_color
.$max_color.".jpg");
/* destroy pic in php */
ImageDestroy($pic);
}
/* return html src tag with reference to correct image */
return "<img src=\"$pics_folder/pic".$relevance."_"
.$min_color.$max_color.".jpg\" border=0>";
}
/*
* Copyright 2001 David Altherr;
* altherda@email.uc.edu
* www.davidaltherr.net
*
* Free for use under MIT open source public license
*/
?>
|
|
|