Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 17th, 2003, 12:03 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default using images from database output

Hmmmm,
This board feels like a big empty room! I'll throw my question out there anyway.

I have some images which are resized and outputted from a database, which is no problem, I have the system down for creating the image and outputting it to a browser. What I want to know is this: can I call on an image through GD functions without first outputting it through another script.

Example of the kinda thing I mean:

I have an image stored here in the public_profiles table that I want to resize down to a thumbnail. So far I have had to call on an external script to output that image with image headers before I can run it through the resize functions. Seems to me that I am doing more work than I have to, if I can just pull the image from the table here and use it directly it would be more efficient.

//snip snip

/* query file_type for headers */

$image_query = mysql_query("SELECT file_type FROM public_profiles WHERE user_id = '{$_REQUEST["user"]}'");
$image_data = mysql_fetch_array($image_query);

/* call on external script to output the complete image for resize functions from the same database table */

$source_file = "http://www.smilingsouls.net/images/profile_image.php?user={$_REQUEST["user"]}";

$size = getimagesize($source_file);

$maximum_width = 75;
$maximum_height = 75;

$width = $size[0];
$height = $size[1];

$x_ratio = $maximum_width / $width;
$y_ratio = $maximum_height / $height;

if (($width <= $maximum_width) && ($height <= $maximum_height)) {

$picture_width = $width;
$picture_height = $height;

} else if (($x_ratio * $height) < $maximum_height) {

$picture_height = ceil($x_ratio * $height);
$picture_width = $maximum_width;

} else {

$picture_width = ceil($y_ratio * $width);
$picture_height = $maximum_height;
}

/* find the file_type of the image and resize accordingly */

if ($image_data["file_type"] == "image/jpeg" || $image_data["file_type"] == "image/pjpeg" || $image_data["file_type"] == "image/jpg") {

/* make another freaking jpeg! */

$source_file = imagecreatefromjpeg($source_file);

/* make empty jpeg with resized dimensions in ratio to the old */

$destination_file = imagecreatetruecolor($picture_width, $picture_height);

/* merge the two */

imagecopyresized($destination_file, $source_file, 0, 0, 0, 0, $picture_width, $picture_height, $width,$height);

header("Content-type: image/pjpeg");

/* output the resized image to be called on by img src */

imagejpeg($destination_file, null, -1);

/* clean up the cache */

imagedestroy($source_file);
imagedestroy($destination_file);

}

So if I could find a way to make the source file not refer to the external url I think I'd have this thing licked! Maybe a function that I have not yet found?

Sorry about all the code, I hope it isn't too messy! I can't really think of a good way to format it in this little textarea box.

Thanks!
: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old July 17th, 2003, 12:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Maybe this is oversimplifying the problem... but why not write all the relevant code in your profile_image.php script in a user-defined function that RETURNS the image data? profile_image.php would then only need to include the script that defines the function, output the proper headers, and echo the return value of that function.

Your other page can simply include the script that defines the function and deal with its return value.


<?php // user_image.php

function user_image($user)
{
   // bla bla bla
   return $image_data;
}
?>

<?php // profile_image.php
require_once('user_image.php');
header('...');
echo user_image($_GET['user']);
?>


You can even make things simpler by adding some optional params to user_image() to handle creating an image of a specific size. If the parameters are not passed, no (re)sizing is done.


Take care,

Nik
http://www.bigaction.org/
 
Old July 17th, 2003, 01:54 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hi Nik,
Thanks for your suggestions!

Quote:
quote:Maybe this is oversimplifying the problem... but why not write all the relevant code in your profile_image.php script in a user-defined function that RETURNS the image data?
I'm not sure that I understand your solution. Are you saying that I could have my own function output the image data with headers?

OK, This is how I originally approached the project: I set up a script and queried for the image data, and file type information both and tried to pass the binary image data itself as a parameter to whatever image handling function. Obviously this didn't work because it wanted a source file location to read from and I was passing the binary data.

I was hoping that there was a function that I have not yet found that would take the raw image binary data as it is stored in the database and make it in such a way that it could be passed as a parameter. Hmmmm, I'm not sure that one exists though, because either way it would need a file name to read from, right?

I do have some user-defined functions that deal with images I just haven't integrated them yet. Reason being is I am only just getting my feet wet with the GD functionality, the code I posted was mostly adapted from a textbook example. But I fully intend to take the project in that direction.

BTW: On an non-related topic I happened to run across your register_globals FAQ... very helpful... answered alot of questions. I had always wondered why there were security issues with it... now I know : ). Not to get off topic, but why are there issues with the session_register() function? (I noticed that you didn't care much for it!) I have used this to register session variables, but I access them through the $_SESSION superglobal... is that still secure?

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old July 21st, 2003, 02:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by quesadilla5
I'm not sure that I understand your solution. Are you saying that I could have my own function output the image data with headers?
No, the function wouldn't output ANYTHING. It would just retrieve the image data from your database.

I've long been opposed to storing binary data in the database -- I think files should be stored in the filesystem. Store the path and filename of the image in the database. Database operations (insert, select, delete, etc) will be much faster with the smaller row and table sizes.

The GD functions open a file and create or manipulate the image in that file. In your setup, it seems you'll have to create or overwrite a file each time you extract the data and need to perform some operations on it.

The function I described would just return the binary contents of the data which, as far as GD is concerned, is not much help -- the binary image data should be in a file.

Quote:
quote:Originally posted by quesadilla5
... Hmmmm, I'm not sure that one exists though, because either way it would need a file name to read from, right?
Right.

Again, the best solution is usually the simplest and most efficient one. Don't store an image in the database, store a path to an image file, since you're working with files elsewhere in the application. Don't perform GD manipulations on images each time they're requested -- that's overkill. It's really easy to set up a simple caching system to create the thumbnails (or whatever image variations you want) just once, and only recreate them when the original image changes. Image manipulation take a while, relatively speaking, so you want to minimize the amount of redundant processing.

A simple caching scheme for generating thumbnails (for example) is as follows:
  Check if the thumbnail exists. If not, create it.
  If the thumbnail does exist, compare the last modified date of the original file to the thumbnail file. If the thumbnail is older, recreate it.
  Otherwise, use the thumbnail that exists.

In more PHP-ish pseudocode:

Code:
$orig  = "{$user}_orig.jpg";
$thumb = "{$user}_thumb.jpg";

if(!file_exists($thumb) ||
   (filemtime($thumb) < filemtime($orig)))
{
   // perform your image manipulations here,
   // saving the new thumbnail image as $thumb.
}

// at this point in the script, $thumb is up-to-date.
Take care,

Nik
http://www.bigaction.org/
 
Old July 21st, 2003, 11:42 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Thank you Nik for your suggestions and enlightenment!

I feel that database storage is ideal for this type of project, its secure, its easy to use and I don't have to fool with setting file permissions. Anyhow why would the database include this functionality if it weren't practical to use??? And otherwise my site is small in terms of following, so this shouldn't hurt the resources too badly : ).

As far as your other suggestions go, as always, you make a great deal of sense and I will certainly be using them to get things streamlined!

: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::





Similar Threads
Thread Thread Starter Forum Replies Last Post
Load Images from and Save Images to a Database cyndie VB.NET 2 August 17th, 2008 06:42 AM
Formatting Database Output Gobbles Classic ASP Basics 3 September 25th, 2007 05:08 AM
changing the way a database stored value is output red_fiesta Classic ASP Professional 1 November 15th, 2006 05:46 AM
sorting xml (database output) Kabe XML 2 September 21st, 2005 03:17 AM
images and database Lucian Ion Classic ASP Databases 0 July 1st, 2005 11:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.