Create 100 different images Containing a Number – The Image Equivalent of Lorem Ipsum

15th March, 2012 – 10:53 am

Often I get asked to design things which require displaying of products and I don’t yet have the images.  I’ve written a script to do just that.  If you’re not a PHP coder and don’t want to tweak the code and run it yourself bug  just want a zip with 100 – 600 random images – here it is.

I needed some filler in a test site and found using the same image for all products doesn’t help prove that the solution will work. What I needed was a 100 (or 600!) meaningful(ish) jpgs.

This script produces just that with random background colours and contains a number.

Here is the output – the colours please me greatly.

100 randomly coloured images with a number

Useful for filler for beta sites.

Remember if you’re using PHP on Windows to check  your php.ini file that extension=php_gd2.dll  is enabled.  I’ve commented about the font used but you will have to amend these lines.    The script actually outputs the last image for testing though in some other use you might just want some confirmation text – remove the header gubbins.

<?php
// Lots of credit to http://www.php-mysql-tutorial.com/user-authentication/image-verification.php
// send several headers to make sure the image is not cached
// taken directly from the PHP Manual

// Date in the past
header(“Expires: Mon, 26 Jul 1997 05:00:00 GMT”);
// always modified
header(“Last-Modified: ” . gmdate(“D, d M Y H:i:s”) . ” GMT”);
// HTTP/1.1
header(“Cache-Control: no-store, no-cache, must-revalidate”);
header(“Cache-Control: post-check=0, pre-check=0”, false);
// HTTP/1.0
header(“Pragma: no-cache”);
// send the content type header so the image is displayed properly
header(‘Content-type: image/jpeg’);
define(‘IMAGE_DIR’, ‘./output/’); // change this.
define(‘FONT_DIR’, ”); // should be your includes folder for the site
define(‘IMAGE_FILENAME’, ‘image’);

// how many images do you want
$noImages = 600;

// can change these but you might need to tweak the positioning below
$width = 125;
$height = 130;

$red = 0;
$green = 0;
$blue = 0;

for ($i = 1; $i <= $noImages; $i++) {
// create an image object using the chosen background
$image = imagecreate($width, $height);

// generate a rand background colour – note no protection to avoid grey backround with grey text.
$red = rand(0,255);
$green = rand(0,255);
$blue = rand(0,255);

$background_color = imagecolorallocate($image, $red, $green, $blue);

//$textColor = imagecolorallocate($image, 0, 0, 0);

//replace with your own font or use a windows standard one (e.g. point it at c:\windows\fonts) – i like the capture_it font.
// www.dafont.com/capture-it.font
$font = FONT_DIR . ‘capture_it.ttf’;
// $font = ‘C:\Windows\Fonts\arial.ttf’;

$x = ($width / 2)- 20;
$y = ($height /2)+ 20;

//Create font colour
$grey = imagecolorallocate($image, 128, 128, 128);

// Add some shadow to the text
imagettftext($image, 40, 0, $x, $y, $grey, $font, $i);

// save the image
imagejpeg($image, IMAGE_DIR . IMAGE_FILENAME . $i.’.jpg’);

// send the last image to the browser – now I’ve added the loop this is not a good idea
if ($i == $noImages) {
imagejpeg($image);
}
// destroy the image to free up the memory
imagedestroy($image);
}
?>

Post a Comment