LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-11-2004, 01:12 PM   #1
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Rep: Reputation: 30
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
 
Old 09-11-2004, 01:26 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
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 ?
 
Old 09-11-2004, 02:22 PM   #3
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Original Poster
Rep: Reputation: 30
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);

?>
 
Old 09-11-2004, 02:45 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
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.
 
Old 09-12-2004, 02:02 AM   #5
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Original Poster
Rep: Reputation: 30
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.";
}
?>
 
Old 09-12-2004, 03:34 AM   #6
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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.
 
Old 09-12-2004, 04:40 AM   #7
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Original Poster
Rep: Reputation: 30
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...!
 
Old 09-12-2004, 05:04 AM   #8
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
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
 
Old 09-12-2004, 05:09 AM   #9
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
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"

Last edited by Cedrik; 09-12-2004 at 05:11 AM.
 
Old 09-12-2004, 05:11 AM   #10
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Original Poster
Rep: Reputation: 30
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.";
}
?>
 
Old 09-12-2004, 05:17 AM   #11
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
[ Edit ] Ok I see your $image variable ($itemContents)

Are you sure it is data from a JPEG file ?

Last edited by Cedrik; 09-12-2004 at 05:22 AM.
 
Old 09-12-2004, 05:42 AM   #12
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Original Poster
Rep: Reputation: 30
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...
 
Old 09-12-2004, 06:41 AM   #13
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Original Poster
Rep: Reputation: 30
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

Last edited by davee; 09-12-2004 at 07:32 AM.
 
Old 09-12-2004, 12:08 PM   #14
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Original Poster
Rep: Reputation: 30
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.";
}
?>
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
php, else function not working. Erhnam Programming 4 06-01-2005 07:48 AM
passing php string to javascript function djgerbavore Programming 2 03-01-2005 11:34 AM
PHP Variable = function? wh33t Programming 2 01-22-2005 06:28 AM
php HTTP_RAW_POST_DATA na value in function dinges Linux - Software 1 12-26-2004 04:11 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM


All times are GMT -5. The time now is 10:50 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration