Get blob jpeg from mySQL, via a function
Posted: Fri Aug 31, 2007 7:23 am
pickle | Please use
I want to encapsulate the functionality of getBlobImage.php in a function so I can achieve the following:
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
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" />Code: Select all
<?php
include('myBlobImageFunctions.php');
outputImage('users', 'photo', 'id=123');
?>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]