Dynamic images (as I call them) are images that change depending on a condition.
A regular image URL is something like:
#1
<img src="http://www.papaface.com/wp-content/themes/papaface/images/beer.png" />
However a Dynamic Image's URL could be something like:
#2
<img src="http://www.papaface.com/somephpfile.php" />
Notice the difference?
Example 1's URL is actually an image file, however example 2's URL is actually a PHP file producing a dynamic image in this case.
These dynamic images are useful if you want to change the image produced at any point.
Lets say we are providing a link checking service and I want to offer the visitors the ability to put an image on their site once they've used the link checking service. The image will change to green when the link is valid, and red when it is not.
<img src="http://www.papaface.com/resources/dynamicimage/example.php?id=22451" />
Now every time the image is loaded the script can actually look for link id "22451" in a database. It can then check the image for validity and produce a green or red image like so:
This is the code that would allow you to do this:
<?php
$linkid = $_GET['id']; //this is the link id that the script needs to look for in the database
//you would open a mysql connection here
$get = @mysql_query("SELECT `link` FROM `tbllinks` WHERE `linkid`='".mysql_real_escape_string($linkid)."'"); //@ suppresses the error message (this is usually a bad coding practice, but in this case we do not want anything other than an image displayed to the user.
list($url) = mysql_fetch_array($get);
/*The code below checks the link. If it is a valid and working link it will return a 200 code.*/
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($info == "200")
$colour = "green"; //link is valid
else
$colour = "red"; //link is NOT valid
}
//code above decided if it should be green or red and produced $colour
header("Content-type: image/jpg");
$im = imagecreatefromjpeg($colour.".jpg"); //tells the script what image to source locally (green.jpg/red.jpg are actual images in the directory)
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
?>
And that is a way of making a dynamic image. I have provided a relatively basic example of how to use dynamic images, I hope it was interesting 🙂
Feel free to ask me any questions!
That’s pretty cool. I might make use of that sometime.
Pingback: coach バッグ
Pingback: gucci 財布