
December 23rd, 2005, 03:15 PM
| | | Manipulating image data loaded from SQL database.
in brief:
Can I perform operations like imagesx() or getimagesize() on raw image
data as retrieved into a variable from a database, without first
saving it as a file? If so, how? If not, what is the best way to
create a temporary file such that it is unlikely to conflict with
other sessions that are performing the same actions?
in more detail:
I have a database that stores an image in a BLOB field, and I want to
display that image on my page either 'as is' or as a thumbnail created
on the fly to a width passed to the <img...> tag by means of a $_GET
variable - so either
<img src="getimage.php?imageid=0123 />
in the first instance, or
<img src="getimage.php?imageid=0123&action=thumb&width= 200 />
in the second.
I'm getting the image from the database like this:
$photoFetchSource = $_GET["imageid"];
$photoFetchQuery = "SELECT * FROM mytable WHERE
photoid=".$photoFetchSource;
$photoFetchResult = mysql_query($photoFetchQuery) or die("blah...");
$photoFetchArray = mysql_fetch_assoc($photoFetchResult;
$photo = $photoFetchArray["photo"];
and I can send the unchanged photo to the page by
header ("Content-type: image/jpg");
echo $photo;
So far, so good...
Unfortunately, I can't do things like
list($width, $height, $type, $attr) =
getimagesize($photo)
because '$photo' isn't a file.
I'd like to avoid any unnecessary file operations if I can. Any
suggestions?
--
Charlie | 
December 23rd, 2005, 03:45 PM
| | | Re: Manipulating image data loaded from SQL database.
Not sure if this will work.
Put
//Do the get blob stuff
header ("Content-type: image/jpg");
echo $photo;
In a separate page called something like image.php. You should pass id
values to the page to display different images, so lets say the image
is id value 1, e.g. image.php?id=1.
Then try:
list($width, $height, $type, $attr) = getimagesize('image.php?id=1') ;
A better way to do it is not store the images in the database at all,
but store references to the images, and keep the image files on the
file system. By the simple fact you are storing images in the database
you are creating for yourself lots of unnecessary problems. | 
December 23rd, 2005, 10:05 PM
| | | Re: Manipulating image data loaded from SQL database.
On 23 Dec 2005 11:55:47 -0800, in
<1135367747.909658.106790@g47g2000cwa.googlegroups .com>
(comp.lang.php) "Chung Leong" <chernyshevsky@hotmail.com> wrote:
[color=blue]
> Use the VariableStream class given as an example here:
>
> http://fi.php.net/manual/en/function...r-register.php[/color]
Thanks, to you and Sean, for your ideas - I'll try them both out: the
more strings to my bow the better! :)
As it happens, shortly after posting, I found the tool that I needed
for the script I had already built: imagecreatefromstring();
Following on from the example in my first post, now I can do
$im = imagecreatefromstring($photo);
which lets me
$photoWidth = imagesx($im);
$photoHeight = imagesy($im);
list($width, $height, $type, $attr) =getimagesize($im);
etc.,
Although Sean's point about using the filesystem to store images, and
the database to reference them, seems like a good one given the 2048k
limit I have on LONGBOOLs in MySQL...
Cheers all
--
Charlie | 
December 23rd, 2005, 10:15 PM
| | | Re: Manipulating image data loaded from SQL database.
Charlie King wrote:[color=blue]
>
> Can I perform operations like imagesx() or getimagesize()
> on raw image data as retrieved into a variable from a database,
> without first saving it as a file? If so, how?[/color]
You can use getimagesize() only on image files. You can, however, use
imagesx() and imagesy() on any image. What you need to do is to pull
data from the database, create an image using imagecreatefromstring()
and feed the resulting image resource to imagesx() and imagesy().
[color=blue]
> I'm getting the image from the database like this:
> $photoFetchSource = $_GET["imageid"];
> $photoFetchQuery = "SELECT * FROM mytable WHERE
> photoid=".$photoFetchSource;
> $photoFetchResult = mysql_query($photoFetchQuery) or die("blah...");
> $photoFetchArray = mysql_fetch_assoc($photoFetchResult;
> $photo = $photoFetchArray["photo"];
>
> and I can send the unchanged photo to the page by
>
> header ("Content-type: image/jpg");
> echo $photo;
>
> So far, so good...
>
> Unfortunately, I can't do things like
> list($width, $height, $type, $attr) =
> getimagesize($photo)
> because '$photo' isn't a file.
>
> I'd like to avoid any unnecessary file operations if I can. Any
> suggestions?[/color]
Two. First, you can do this:
$img = imagecreatefromstring($photo);
$x = imagesx($img);
$y = imagesy($img);
header ("Content-type: image/jpg");
echo $photo; // or imagejpeg($img);
Second, if you store images in a database, you may consider storing
image attributes (width, height, and type) in the database as well and
not worry about obtaining them at execution.
Cheers,
NC | 
December 24th, 2005, 07:55 AM
| | | Re: Manipulating image data loaded from SQL database.
On 23 Dec 2005 14:08:16 -0800, in
<1135375696.256198.259250@g43g2000cwa.googlegroups .com>
(comp.lang.php) "NC" <nc@iname.com> wrote:
[color=blue]
> Charlie King wrote:[color=green]
> >
> > Can I perform operations like imagesx() or getimagesize()
> > on raw image data as retrieved into a variable from a database,
> > without first saving it as a file? If so, how?[/color]
>
> You can use getimagesize() only on image files. You can, however, use
> imagesx() and imagesy() on any image. What you need to do is to pull
> data from the database, create an image using imagecreatefromstring()
> and feed the resulting image resource to imagesx() and imagesy().[/color]
Thanks for that - as it turns out, that's exactly where I ended up :)
[color=blue][color=green]
> > I'd like to avoid any unnecessary file operations if I can. Any
> > suggestions?[/color]
>
> Two. First, you can do this:
>
> $img = imagecreatefromstring($photo);
> $x = imagesx($img);
> $y = imagesy($img);
> header ("Content-type: image/jpg");
> echo $photo; // or imagejpeg($img);[/color]
Spookily similar to the code I have here! lol
[color=blue]
> Second, if you store images in a database, you may consider storing
> image attributes (width, height, and type) in the database as well and
> not worry about obtaining them at execution.[/color]
I'd considered that, but there are two issues - a) those dimensions
have to be calculated at some time, and it's as well to do so on the
way out as the way in (although, arguably they'll be extracted more
frequently than they're input...), and b) there is a possibility that
my customer will end up squirting data into the database from
somewhere other than my web front end, and I'd rather be in control
than their supplying good data.
[color=blue]
> Cheers,
> NC[/color]
Thanks :)
--
Charlie |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|