How to use draw functions with BLOB image from database?
Posted: Mon Aug 09, 2010 11:12 am
Hello forum,
Say I want to use a draw function such as imagearc. Instead of creating an image and setting it to variable $img, can I use existing BLOB data (in this case image/jpeg) from a mysql database as the target image?
Getting and displaying the images from the db I have no problem with, but this seems more difficult to a php novice such as myself.
Here's what I have so far...
image.php
so on some other page, I write an image tag as such, "<img src='image.php?id=1' alt='' />"
Thanks!
tk
Say I want to use a draw function such as imagearc. Instead of creating an image and setting it to variable $img, can I use existing BLOB data (in this case image/jpeg) from a mysql database as the target image?
Getting and displaying the images from the db I have no problem with, but this seems more difficult to a php novice such as myself.
Here's what I have so far...
image.php
Code: Select all
<?php
// set connection variables
$dbhost = "localhost";
$dbuser = "someuser";
$dbpass = "somepassword";
$dbname = "somedb";
$sqlimgdata = "SELECT data FROM table WHERE id = " . (int)$_GET['id'] . ";";
// image to be displayed for invalid ID query string
$noimgurl = "../images/invalid.jpg";
// error reporting
ini_set('display_errors',1);
error_reporting(E_ALL & ~E_NOTICE);
// attempt database connection
$conn = mysql_connect($dbhost, $dbuser, $dbpass)
or die('Error connecting to MySQL.');
// attempt to select the database
mysql_select_db($dbname)
or die('Error selecting the database.');
$result = mysql_query($sqlimgdata)
or die('Could not run query: ' . mysql_error());
if (!isset($_GET['id']) || mysql_num_rows($result) == 0) {
$image = file_get_contents($noimgurl);
} else {
$row = mysql_fetch_row($result);
$image = $row[0];
}
header("Content-type:image/jpeg");
print $image;
exit;
mysql_close($conn);
?>
Thanks!
tk