1

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++;
}
Community
  • 1
  • 1
  • Check for errors. Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Sep 24 '15 at 17:52
  • You didn't select the `value` column, just "r,g,b". Seeing that you are checking for errors, that should've been said. – Funk Forty Niner Sep 24 '15 at 17:53
  • 1
    **WARNING**: If you're just learning PHP, please, do not learn the obsolete `mysql_query` interface. It's awful and is being removed in future versions of PHP. A modern replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/). A guide like [PHP The Right Way](http://www.phptherightway.com/) can help explain best practices. Always be absolutely **sure** your user parameters are [properly escaped](http://bobby-tables.com/php) or you will have severe [SQL injection bugs](http://bobby-tables.com/). – tadman Sep 24 '15 at 17:54
  • If you don't have a column called "value", then you'll need to use an alias in your query. `$row['value']` that does nothing right now. I have a 5 minute attention span today, btw. – Funk Forty Niner Sep 24 '15 at 17:57
  • Added error reporting to top of page and no errors were displayed in the page. I see the place where "value" is in the code and what you said makes sense. I have no field in my database named "value" I am going to try and change that to a field named "hexValue" which holds my hex string. – John DeFalco Sep 24 '15 at 18:06
  • it's the `or die(mysql_error())` to `mysql_query()` that should've thrown you an error. Try something like `SELECT r, g, b as value` type of thing if you're going to use `$row['value']`. But you may need to select the row that contains the image name as the alias. You'll need to post your DB schema. Or use one of the columns `$row['r']` – Funk Forty Niner Sep 24 '15 at 18:09
  • Changed my select to $sql = mysql_query("SELECT r, g, b as value FROM thex") or die(mysql_error()); Still get a blank page with no image created. – John DeFalco Sep 24 '15 at 18:12
  • My database table tHex has the following fields. hexValue(tinyText), r(varChar), 50, g(varChar), 50.b(varChar), 50. colors(varChar), 50., pkId(int), 11. Thanks – John DeFalco Sep 24 '15 at 18:30

0 Answers0