|
Posted by NC on 10/17/96 11:35
Charlie King wrote:
>
> 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?
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().
> 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?
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
Navigation:
[Reply to this message]
|