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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-11-2004, 01:12 PM
|
#1
|
Member
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263
Rep:
|
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
|
|
|
09-11-2004, 01:26 PM
|
#2
|
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140
|
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 ?
|
|
|
09-11-2004, 02:22 PM
|
#3
|
Member
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263
Original Poster
Rep:
|
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);
?>
|
|
|
09-11-2004, 02:45 PM
|
#4
|
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140
|
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.
|
|
|
09-12-2004, 02:02 AM
|
#5
|
Member
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263
Original Poster
Rep:
|
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.";
}
?>
|
|
|
09-12-2004, 03:34 AM
|
#6
|
Senior Member
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794
Rep: 
|
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 " iirc.
|
|
|
09-12-2004, 04:40 AM
|
#7
|
Member
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263
Original Poster
Rep:
|
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...!
|
|
|
09-12-2004, 05:04 AM
|
#8
|
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140
|
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
|
|
|
09-12-2004, 05:09 AM
|
#9
|
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140
|
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($result, 0, "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, $src, 0, 0, 0, 0,
$tn_width,$tn_height,$width,$height);
header('Content-type: image/jpeg');
ImageJpeg($dst, null, -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.
|
|
|
09-12-2004, 05:11 AM
|
#10
|
Member
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263
Original Poster
Rep:
|
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.";
}
?>
|
|
|
09-12-2004, 05:17 AM
|
#11
|
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140
|
[ 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.
|
|
|
09-12-2004, 05:42 AM
|
#12
|
Member
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263
Original Poster
Rep:
|
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...
|
|
|
09-12-2004, 06:41 AM
|
#13
|
Member
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263
Original Poster
Rep:
|
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.
|
|
|
09-12-2004, 12:08 PM
|
#14
|
Member
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263
Original Poster
Rep:
|
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 03:34 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|