Using GD/Image Functions to Transform Images Stored in a Database

This is a discussion on Using GD/Image Functions to Transform Images Stored in a Database within the PHP Language forums, part of the PHP Programming Forums category; I'm new to the GD/Image functions with PHP, and I'm trying to use them to manipulate jpeg ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-13-2003
Weston C
 
Posts: n/a
Default Using GD/Image Functions to Transform Images Stored in a Database

I'm new to the GD/Image functions with PHP, and I'm trying to use them
to manipulate jpeg images that I've stored in a MySQL database
(specifically, creating thumbnails). The thing I can't tell from
reading the documentation is how to use these image functions to
operate on image data -- it looks like you get a GD image resource in
each case by specifying a file name, not by passing image data
directly. While I can see how that's convenient for the common case, I
can't figure out how to make this work without writing the image out
to a file (and since I'm already taking a bit of a performance hit by
reading the image out a database, that seems like the wrong thing to
do). Can anyone elaborate?
Reply With Quote
  #2 (permalink)  
Old 09-14-2003
Weston C
 
Posts: n/a
Default Re: Using GD/Image Functions to Transform Images Stored in a Database

notsew-reversePreceedingAndRemoveThis@canncentral.org (Weston C) wrote in message news:<df41fe1.0309130905.2f7cccc6@posting.google.c om>...
> I can't tell from
> reading the documentation is how to use these image functions to
> operate on image data


As it turns out, the helpful folks on php-general suggested the
function "imagecreatefromstring", which seems to be the ticket.
However, I'm having another problem.

The end goal is to create a script that will output the image to the
browser -- and will resize it if given a width value via the query
string. Right now, I have a script that works if I just pass the image
through, but it fails if I try to use the gd library to change the
size.

Here's what I've got:

$row = mysql_fetch_row($dbresult);
$imgdata = $row[0];

if(isset($width) && !empty($width) && is_numeric($width))
/* only do transformation if given a width */
{
$gdimg = imagecreatefromstring($imgdata);
$srcWidth = imagesx($gdimg);
$srcHeight = imagesy($gdimg);
$scaleFactor = $width/$srcWidth;
$targetHeight = $srcHeight * $scaleFactor;

$resizedimg = imagecreatetruecolor($width,$targetHeight);
imagecopyresized($resizedimg,$gdimg,0,0,0,0,$width ,$targetHeight,$srcWidth,$srcHeight);
}

header("Content-Type: image/jpeg");
header("Content-Transfer-Encoding: binary\n");
//send file contents
if(!empty($resizedimg))
imagejpg($resizedimg);
else
{
header("Content-length: " . strlen($imgdata) . "\n");
print($imgdata);
}


Does anyone see what I'm doing wrong? Again, the script actually works
if I
don't try to do the transform. See:

http://www.fsboutah.net/jpg.php?tabl...field=Picture1

and

http://www.fsboutah.net/jpg.php?tabl...ure1&width=100

for contrasts.


-W
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB 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 06:24 AM.


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