I am trying to create a .jpg file with a background fill using RGB or hex values that are stored in my MySQL database. I want to loop through the records and create a .jpg for the 500 values I have and name them with hex value. I have found a post similar to mine but can not get it to work.
My fields in my database are hexValue (sample data;B0171F), r, g, b. which store their respective values.
I am brand new at php and could use some help. Here is the code I am trying to use.
<?php
$username = "john";
$password = "myPassword";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("mycolors",$dbhandle)
or die("Could not select Colors");
//execute the SQL query and return records
$sql = mysql_query("SELECT r, g, b FROM thex")
or die(mysql_error());
$x = 0;
while($row = mysql_fetch_array( $sql ))
{
$imgname = $x.".jpg";
$color = $row['value'];
// Skip the whole lot if the colour is invalid
if (strlen($color) != 6)
continue;
// No need to create an array just to call list()
$r = hexdec($color[0].$color[1]);
$g = hexdec($color[2].$color[3]);
$b = hexdec($color[4].$color[5]);
// There's no need to header() if you're writing to a file
//header("Content-type: image/jpeg");
$image = imagecreate( 720, 576 );
$colour = imagecolorallocate($image, $r, $g, $b);
// You don't actually fill the image with the colour
imagefilledrectangle($image, 0, 0, 719, 575, $colour);
imagejpeg($image, $imgname);
imagedestroy($image);
$x++;
}