image and databases problems

This is a discussion on image and databases problems within the alt.comp.lang.php forums, part of the PHP Programming Forums category; OK, sorry for those who answered my first post, but i have decided to store images in databases instead of ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-22-2003
Jon
 
Posts: n/a
Default image and databases problems

OK, sorry for those who answered my first post, but i have decided to store
images in databases instead of as files on the server.
The problem i have is when i do a <img src="HTML/image.php?id=<?php echo
$row['id']; ?>">
, it does not show an image, just a small empty area,
CODE>> adding to a database
$imgFull =
imagecopyresized($newImg,$img,0,0,0,0,$fullWidth,$ fullHeight,$currentWidth,$currentHeight)
;
$imgSQL = "INSERT INTO images (id,image,width,height,image_type) VALUES
('$id' ,'{$newImg}', '$fullWidth' , '$fullHeight'
,'".$_SESSION["IMAGE_TYPE"]."') ";


CODE>> retrieving from database
$sql = "Select * from images where (id = '$id')";
$results = mysql_query($sql, $conn);
$row = mysql_fetch_array($results);
$image_type = $row["image_type"];
$image = $row["image"];
Header ("Content-type: ".$image_type);
echo $image;
mysql_close($conn);


if i just request the url (not embedded in a table and other HTML,) and
comment out the header function i get "Resource id #4" instead of all the
binary data i expected. I read that this was because this was a reference,
not the image

Thanks in advance
if you need any extra info, just ask

Jon
Reply With Quote
  #2 (permalink)  
Old 07-22-2003
Andy Hassall
 
Posts: n/a
Default Re: image and databases problems

On Tue, 22 Jul 2003 19:56:48 +0100, Jon <jonchalk@hotmail.spam> wrote:

>OK, sorry for those who answered my first post, but i have decided to store
>images in databases instead of as files on the server.
>The problem i have is when i do a <img src="HTML/image.php?id=<?php echo
>$row['id']; ?>">
>, it does not show an image, just a small empty area,
> CODE>> adding to a database
>$imgFull =
>imagecopyresized($newImg,$img,0,0,0,0,$fullWidth, $fullHeight,$currentWidth,$currentHeight);


I'm assuming that all your variables are correctly defined, since there's no
information on them in this post.

>$imgSQL = "INSERT INTO images (id,image,width,height,image_type) VALUES
>('$id' ,'{$newImg}', '$fullWidth' , '$fullHeight'
>,'".$_SESSION["IMAGE_TYPE"]."') ";


That's not going to work... you've stored the image resource handle, not the
data representing the image.

And even if you had, you _absolutely need_ to run it through an escaping
function, e.g. mysql_escape_string.

http://uk.php.net/mysql_escape_string

There's no information here as to how you execute the query (if at all), or
whether you check for errors.

>CODE>> retrieving from database
>$sql = "Select * from images where (id = '$id')";


The brackets are superfluous.

>$results = mysql_query($sql, $conn);


You must check for errors. Do not ignore whether the return value is FALSE.

>$row = mysql_fetch_array($results);
>$image_type = $row["image_type"];
>$image = $row["image"];
>Header ("Content-type: ".$image_type);
>echo $image;
>mysql_close($conn);
>
>if i just request the url (not embedded in a table and other HTML,) and
>comment out the header function i get "Resource id #4" instead of all the
>binary data i expected. I read that this was because this was a reference,
>not the image


Yes - this is because you originally stored the image resource handle, NOT the
image data that it refers to.

Use imagepng, imagejpeg or whatever, and either write to a temporary file and
read it back in again for the data, or capture the output with the output
control functions.

Then escape that data, and store that in the database.

--
Andy Hassall (andy@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:16 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0