How to use draw functions with BLOB image from database?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tjk925
Forum Newbie
Posts: 1
Joined: Mon Aug 09, 2010 10:56 am

How to use draw functions with BLOB image from database?

Post by tjk925 »

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

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);
?>
so on some other page, I write an image tag as such, "<img src='image.php?id=1' alt='' />"

Thanks!
tk
Post Reply