Page 1 of 1

Get blob jpeg from mySQL, via a function

Posted: Fri Aug 31, 2007 7:23 am
by josamoto
pickle | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi all

I have a file, getBlobImage.php which I use to retrieve images from mySQL blobs. It is used as follows:

Code: Select all

<img src="getBlobImage.php?table=users&field=photo&idfieldname=id&id=123" />
I want to encapsulate the functionality of getBlobImage.php in a function so I can achieve the following:

Code: Select all

<?php
     include('myBlobImageFunctions.php');
     outputImage('users', 'photo', 'id=123');
?>
How do I go about writing this function. The function must basically echo an image tag for me.

Here's the code for getBlobImage.php

Code: Select all

<?php
	include('myDatabase.php');
	
	// Retrieve image from database
	if($myDB = new myDatabase()){
		$result = $myDB->executeQuery("SELECT " . $_GET['field'] . " FROM " . $_GET['table']
			. " WHERE " . $_GET['idField'] . "=" . $_GET['value']);
			
		if($result && $row = mysql_fetch_assoc($result)){
			$strImage = $row['Photo'];
	
			// Set MIME header
			header("Content-Type: image/jpeg");

			// Ouput and cleanup
			$image  = imagecreatefromstring($strImage);
			
			// Perform resize if so indicated
			if(isset($_GET['width']) && isset($_GET['height'])){
				$desWidth  = abs($_GET['width']);
				$desHeight = abs($_GET['height']);

				$origWidth  = imagesx($image);
				$origHeight = imagesy($image);

				if($desWidth >= $desHeight)
					$desHeight = $origHeight / $origWidth * $desWidth; // Landscape image
				else
					$desWidth = $origWidth / $origHeight * $desHeight; // Portrait image

				$newImage = imagecreate($desWidth, $desHeight);
				imagecopyresampled($newImage, $image, 0, 0, 0, 0, $desWidth, $desHeight, imagesx($image), imagesy($image));
				
				$image = $newImage;
			}
			
			// Output image
			$output = imagejpeg($image);
			echo($output);

			// Perform memory cleanup
			if(isset($image))    imagedestroy($image);
			if(isset($newImage)) imagedestroy($newImage);
		}else{
			echo('Image not found!');
		}
	}
?>

pickle | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Aug 31, 2007 9:51 am
by pickle
That's pretty simple. Just write outputImage so it accepts 3 parameters, then echoes them inside the wanted string.

Posted: Mon Sep 03, 2007 1:21 am
by josamoto
Thanks for the reply, Pickle

I've tried wrapping the functionality inside a function accepting 3 tags, but for some reason, the browser opens a new window and it outputs the binary data of the jpeg. I've made sure to set the MIME type in the header, and I've even tried output buffering with ob_start().

Hope I can find some solution.

PS: Apologies about the code tags, I know how to use them, I guess I was a bit tired and absent minded.

Posted: Mon Sep 03, 2007 2:02 am
by josamoto
When I drop my code from getBlobImage into a function, I call it this way:

Code: Select all

<img src="<?php getBlobImage('Photo','Users','loginNumber','19',128,128); ?>" />
The output is as follows:

Code: Select all

<img class="contactImage" alt="Photo" src="ÿØÿà&#65533;JFIF&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;ÿþ&#65533;>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality
ÿÛ&#65533;C&#65533;
... omitted jpeg binary stuff
g‚d61¹IôªU-º)˘v—cölÉÀÈû c'éøú»èHÜsêyª»^‰&#65533;üøUë(çÔÒ_"<´&#65533;oÃ`ð9éØþU“M›¦

Posted: Mon Sep 03, 2007 4:15 am
by onion2k
You're putting jpeg data in the middle of a page of HTML and you're wondering why it doesn't work? :roll:

In HTML you need to link to an image .. eg <img src="image.jpg">. It's the same if the image is created with PHP. You need to use <img src="image.php"> ... then have a script called image.php that outputs the jpeg data and nothing else.

Posted: Mon Sep 03, 2007 4:30 am
by josa
While I completely agree with onion2k there actually is a way to embed image data directly in the tag. Read http://www.faqs.org/rfcs/rfc2397.html. But I only point this out to show that it's not really an option. It's not supported by Internet Explorer (http://en.wikipedia.org/wiki/Data:_URI_scheme) for example.

/josa