Get blob jpeg from mySQL, via a function

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
User avatar
josamoto
Forum Commoner
Posts: 41
Joined: Fri Aug 24, 2007 6:57 am
Location: South Africa
Contact:

Get blob jpeg from mySQL, via a function

Post 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]
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

That's pretty simple. Just write outputImage so it accepts 3 parameters, then echoes them inside the wanted string.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
josamoto
Forum Commoner
Posts: 41
Joined: Fri Aug 24, 2007 6:57 am
Location: South Africa
Contact:

Post 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.
User avatar
josamoto
Forum Commoner
Posts: 41
Joined: Fri Aug 24, 2007 6:57 am
Location: South Africa
Contact:

Post 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›¦
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
josa
Forum Commoner
Posts: 75
Joined: Mon Jun 24, 2002 4:58 am
Location: Sweden

Post 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
Post Reply