LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Passing one php function result as a parameter to another php function (http://www.linuxquestions.org/questions/programming-9/passing-one-php-function-result-as-a-parameter-to-another-php-function-229360/)

davee 09-11-2004 01:12 PM

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

Cedrik 09-11-2004 01:26 PM

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 ?

davee 09-11-2004 02:22 PM

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);

?>

Cedrik 09-11-2004 02:45 PM

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.

davee 09-12-2004 02:02 AM

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.";
}
?>

Proud 09-12-2004 03:34 AM

Have you tried something a lot simpler than messing with the actual methods, and just checked the syntax as you first asked?
Quote:

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">
Have you tried
<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 &#34 iirc.

davee 09-12-2004 04:40 AM

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...!

Cedrik 09-12-2004 05:04 AM

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

Cedrik 09-12-2004 05:09 AM

Something like :
PHP Code:

<?php
$itemId 
= (isset($_GET["itemId"])) ? $_GET["itemId"]: FALSE;

if(!
$itemId or !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)
{
    
$image = @mysql_result($result0"itemBlobData");
}
else
{
    die 
"Record doesn't exist.";
}

$max_width 200;
$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$src0000,
$tn_width,$tn_height,$width,$height);
header('Content-type: image/jpeg');
ImageJpeg($dstnull, -1);
ImageDestroy($src);
ImageDestroy($dst);
?>

so if you save it as "images.php", you call it as "http://site.com/images.php?itemId=12"

davee 09-12-2004 05:11 AM

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.";
}
?>

Cedrik 09-12-2004 05:17 AM

[ Edit ] Ok I see your $image variable ($itemContents)

Are you sure it is data from a JPEG file ?

davee 09-12-2004 05:42 AM

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...

davee 09-12-2004 06:41 AM

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

davee 09-12-2004 12:08 PM

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 09:15 AM.