Passing one php function result as a parameter to another php function
I store images on my website as mysql blobs. I'd like to retrieve an image via an existing php function, and resize them via another function, both on the fly.
To retrieve the image, I use; <img src="retrieve_image.php?itemId=1"> To resize a stored jpg, I can use: <img src="resize_image.php?image=image.jpg"> How can I combine the two? I would like to have something like: <img src="resize_image.php?image=retrieve_image.php?itemId=1"> Is there correct syntax for this? Dave |
I think that you already know that "resize_image.php?image=retrieve_image.php?itemId=1" will not work.
Could you post your resize_image.php ? maybe not all the file, just the relevant lines where you get the itemId and assign an image to it. Also, what do you call resize, is it using GD library or just change the HTML width and height of the image ? |
Here's the resize_image.php file I'm using - it's almost verbatum from the (wonderful) PHP and MySql programming book, which I think is a New Riders offering (it's at work or I would confirm)...
I presume the bit you're most interested in is the final stanza... <?php $image = $HTTP_GET_VARS['image']; //set dimensions desired for webpage viewing if (!$max_width) $max_width = 200; if (!$max_height) $max_height = 150; $size = GetImageSize($image); $width = $size[0]; $height = $size[1]; $x_ratio = $max_width / $width; $y_ratio = $max_height / $height; if ( ($width <= $max_width) && ($height <= $max_height) ) { $tn_width = $width; $tn_height = $height; } else if (($x_ratio * $height) < $max_height) { $tn_height = ceil($x_ratio * $height); $tn_width = $max_width; } else { $tn_width = ceil($y_ratio * $width); $tn_height = $max_height; } $src = ImageCreateFromJpeg($image); $dst = Imagecreatetruecolor($tn_width,$tn_height); ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width,$tn_height,$width,$height); header('Content-type: image/jpeg'); ImageJpeg($dst, null, -1); ImageDestroy($src); ImageDestroy($dst); ?> |
Well, now I know where the 'image' variable go but not for the 'itemId', I need this information to help you to combine them and make the url from that.
|
Here's the script I use to display the blob images, complete with my itemId variable...
<?php global $itemId; if(!is_numeric($itemId)) die("Invalid itemId specified"); include 'details.php'; $sConn = mysql_connect($dbServer, $dbUser, $dbPass) or die("Couldn't connect to database server"); $dConn = mysql_select_db($dbDatabase, $sConn) or die("Couldn't connect to database $dbDatabase"); $dbQuery = "SELECT itemBlobType, itemBlobData "; $dbQuery .= "FROM stockSetup "; $dbQuery .= "WHERE itemId = $itemId"; $result = mysql_query($dbQuery) or die("Couldn't get file list"); if(mysql_num_rows($result) == 1) { $fileType = @mysql_result($result, 0, "itemBlobType"); $fileContent = @mysql_result($result, 0, "itemBlobData"); header("Content-type: $fileType"); echo $fileContent; } else { echo "Record doesn't exist."; } ?> |
Have you tried something a lot simpler than messing with the actual methods, and just checked the syntax as you first asked?
Quote:
<img src="resize_image.php?image='retrieve_image.php?itemId=1'"> or <img src="resize_image.php?image=\"retrieve_image.php?itemId=1\""> Or instead of escaping the inner quotes use the " iirc. |
Tried that (tried all variations again this morning just to be sure!).
I think the problem may lie around the ImageCreateFromJpeg function - does anyone know if ImageCreateFromString will directly decypher a blob image? It's a bit heavy thinking for my wee mind this early on a Sunday morning...! |
You can try combine retrieve_image.php and resize_image.php in one script file, and modify it so when it get 'itemId' from database, if it knows it is an image, then it resize it.
With one script for all, you just indicate 'itemId' in the url |
Something like :
PHP Code:
|
Just what I've been doing; it doesn't work yet, but I'll keep at it. (error - The image “http://qwerty/full_resize.php?itemId=1” cannot be displayed, because it contains errors. - meaningful!)
<?php global $itemId; if(!is_numeric($itemId)) die("Invalid itemId specified"); #My connection details for my mySql db include 'details.php'; $sConn = mysql_connect($dbServer, $dbUser, $dbPass) or die("Couldn't connect to database server"); $dConn = mysql_select_db($dbDatabase, $sConn) or die("Couldn't connect to database $dbDatabase"); $dbQuery = "SELECT itemBlobType, itemBlobData "; $dbQuery .= "FROM stockSetup "; $dbQuery .= "WHERE itemId = $itemId"; $result = mysql_query($dbQuery) or die("Couldn't get file list"); //set dimensions desired for webpage viewing if (!$max_width) $max_width = 200; if (!$max_height) $max_height = 150; #$size = GetImageSize($image); #$width = $size[0]; #$height = $size[1]; #$x_ratio = $max_width / $width; #$y_ratio = $max_height / $height; #manually specify height and width for now till I get something displayed... (800x600 source image) $x_ratio = $max_width / 800; $y_ratio = $max_height / 600; if ( ($width <= $max_width) && ($height <= $max_height) ) { $tn_width = $width; $tn_height = $height; } else if (($x_ratio * $height) < $max_height) { $tn_height = ceil($x_ratio * $height); $tn_width = $max_width; } else { $tn_width = ceil($y_ratio * $width); $tn_height = $max_height; } if(mysql_num_rows($result) == 1) { $fileType = @mysql_result($result, 0, "itemBlobType"); $fileContent = @mysql_result($result, 0, "itemBlobData"); header("Content-type: $fileType"); $src = ImageCreateFromJpeg($fileContent); $dst = Imagecreatetruecolor($tn_width,$tn_height); ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width,$tn_height,$width,$height); ImageJpeg($dst, null, -1); ImageDestroy($src); ImageDestroy($dst); } else { echo "Record doesn't exist."; } ?> |
[ Edit ] Ok I see your $image variable ($itemContents)
Are you sure it is data from a JPEG file ? |
Yes - all the images come straight from my digital camera... see http://www.daveedmondston.org
I've tried both ImageCreateFromJpeg and ImageCreateFromString with no success. I was hoping there'd be an ImageCreateFromBlob - no such luck... |
Update: -
Got something working; I'll refine it (take out the constants etc) and post my solution, after a bit of lunch! Thanks to everyone who responded, especially cedrik, for putting me on the right track... Dave |
Here's the (almost) complete script - I've cheated slightly, by calling my original retrieve script (download_stockblob.php) purely to get it's size. It works, though! The main thing is, I can now create thumbnails, etc, on the fly...
<?php global $itemId; if(!is_numeric($itemId)) die("Invalid itemId specified"); include 'details.php'; $sConn = mysql_connect($dbServer, $dbUser, $dbPass) or die("Couldn't connect to database server"); $dConn = mysql_select_db($dbDatabase, $sConn) or die("Couldn't connect to database $dbDatabase"); $dbQuery = "SELECT itemBlobType, itemBlobData "; $dbQuery .= "FROM stockSetup "; $dbQuery .= "WHERE itemId = $itemId"; $result = mysql_query($dbQuery) or die("Couldn't get file list"); //set dimensions desired for webpage viewing $max_width = 100; $max_height = 80; $size = GetImageSize("http://qwerty/download_stockblob.php?itemId=$itemId"); $width = $size[0]; $height = $size[1]; $x_ratio = $max_width / $width; $y_ratio = $max_height / $height; if ( ($width <= $max_width) && ($height <= $max_height) ) { $tn_width = $width; $tn_height = $height; } else if (($x_ratio * $height) < $max_height) { $tn_height = ceil($x_ratio * $height); $tn_width = $max_width; } else { $tn_width = ceil($y_ratio * $width); $tn_height = $max_height; } if(mysql_num_rows($result) == 1) { $fileType = @mysql_result($result, 0, "itemBlobType"); $fileContent = @mysql_result($result, 0, "itemBlobData"); header("Content-type: $fileType"); $src = ImageCreateFromString($fileContent); $dst = Imagecreatetruecolor($tn_width,$tn_height); ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width,$tn_height,$width,$height); ImageJpeg($dst, null, -1); ImageDestroy($src); ImageDestroy($dst); } else { echo "Record doesn't exist."; } ?> |
All times are GMT -5. The time now is 05:13 AM. |