Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

image into variable?

Status
Not open for further replies.

ryan010101

Technical User
Jan 24, 2001
83
US
I purchased a shopping cart solution called XCart which uses Smarty templates and MySQL. The way it handles images of products is to store the actual image in the database. This doesn't work for me so I created a field in the products table called "imageName" and I want to just reference that field name when it displays the image (so it builds the img tag). The problem is the template uses a file called "image.php" to display all images. This file is what is actually displaying the image. This is the code:

require "./smarty.php";
require "./config.php";

$result=db_query("select image,image_type from thumbnails where productid='$productid'");
if(db_num_rows($result)) list($image,$image_type)=db_fetch_row($result);
db_free_result($result);

if(!empty($image)) {
header("Content-type: $image_type");
echo $image;
}
else
{
$fi=fopen($default_image,"rb");
$image=fread($fi,filesize($default_image));
header("Content-type: image/gif");
echo $image;
}

So what I want to do is load the image from a directory instead of loading it from a database and then return it. Is that possible?
 
There's no point in storing that image in memory unless you're planning on using GD library functions to manipulate it on the fly.

I'd recommend just printing an &quot;<IMG...&quot; HTML tag to the browser, filling in the &quot;src&quot; attribute of the tag from the database. Then the browser and server can deal with fetching the image rather than your having to write code to do it.

If you find that you must send the image via PHP, I recomment you open the file, read a buffer of, say, 1024 bytes, and immediately output that 1024 bytes, looping until you can no longer read from the file. ______________________________________________________________________
TANSTAAFL!
 
Your suggestion is exactly what I want to do. I just need to make it worth with this php code.

On the page that it actual displays the code looks like:
<img src=&quot;image.php?x={$productcode}&quot;>

So it seems to me that I can't just return a address to the image, I must return the actual image from the &quot;image.php&quot; page. &quot;image.php&quot; is the code that I included in my first post.

I could make a change in the way it's calling this &quot;image.php&quot; page, but there are a ton of files to go through. Any suggestions would be greatly appriciated.
 
If you were running Apache, I one possibility would be to use mod_rewrite to change the URL internally from invoking the image.php script to directly retrieving the image. I don't know of a way to perform the equivalent on IIS.

Short of that, if you are going to fundamentally change the way the app runs (and I wholeheartedly recommend that you do so in this case), you're just going to have to bite the bullet and fix all the files -- it sucks, but it's the best thing to do.

If you have a text editor that can do regular expression search and replace across multiple files, the editor can do most of the work. But I'd back up before I do this. ______________________________________________________________________
TANSTAAFL!
 
It is running on Apache, can't say as that I know anything about mod_rewrite. I'm new to PHP (used ASP/Cold Fusion mostly). I think I might just change all the code and be done with it. Might be worth it in the long run.

How would you recommend changing it? I would think using an include file would be the best way? With these Smarty templates each file has like no code in them. They all just refer to other files.
 
I would definately make the effort to change the files. You're right -- it will be worth it in the long run.

You wouldn't happen to be lucky enough to have a single included file which determines the behavior of the building of all those &quot;<IMG...&quot; tags would you?

I can't advise you on how to change the app -- I'm not familiar with Smarty templates. An include file does give you the ability to manage your code more efficiently, though. ______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top