Forum Moderators: coopster

Message Too Old, No Replies

Questions about GD and images

         

omoutop

6:53 am on Jul 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hello to everybody!
I have a question regarding GD and images which I need to clarify with your help of course....

I have a system where admins can upload photos and images....the original photo as well as the thumbnail...At first sight, I had 2 fields, one for the image and one for the thumbnail...however, I was thinking about having only one field while the image should be proccessed by the "submit" script afterwards in order to create the thumbnail aytomatically in the selected folder (/pictures/thumbs/) while storing the original image in the /pictures/ folder...
In the first system I created (2 fields), both images were uploaded in a selected folder while their extensions(names) were stored in the Database...however...if I use GD library to create the new system the whole thing changes...Images are not uploaded but stored directly in the database as strings and called afterwards as images...for instance, the original photo is stored in the database while the automatically created thumbnail proccessed by GD, (opened as text...croped...etc etc.) and also stored in DB as text...Am I right?

lobo235

2:51 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



You don't have to save the image in your DB, in fact, I would not recommend it. The way I do most of my scripts like this is to set specific sizes for the larger image and the thumbnail. So for example, I would set the large limit to be 640x640 and the thumbnail to be 240x240 or something like that. When the image is uploaded I first check to see if the dimensions are smaller than 640x640, if they are i just save that image to a directory someplace and make the thumbnail for it. If it's bigger than 640x640 I run it through a resize function to get it down to 640x640 and then i create the thumbnail again.

Typically I just store the path to the image in the DB since it's faster to pull out the path to the image than 60k or more of image data.

The imagejpeg() [php.net] function will let you save image data to a file.

omoutop

1:44 pm on Jul 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thank you for ur tips lobo 235...my current version requires user to upload both thumb and Image...the only thinks that are checked are the size of photo, type, and if the photo is loaded or not....i tried to use some functions in order to resize it or even create the thumbnail automatically but it never worked...if u could plz indicate me some examples from the net or from ur own work I would be gratefull...thx in advance

mincklerstraat

1:53 pm on Jul 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



have a look at [php.net...] , and then look at using a second parameter for imagejpeg(), which allows you to save it instead of outputting it (you also should scrap that header() line). You'll find some examples there as well. The basic thing is to create image object handlers - first one with imagecreatefromjpeg made with the original image, then one with imagecreatetruecolor - and then you can use imagecopyresized.

lobo235

3:20 pm on Jul 14, 2005 (gmt 0)

10+ Year Member



Here is the function that I use.
function resampimagejpg($forcedwidth, $forcedheight, $sourcefile, $destfile) { 
$g_imgcomp=$imgcomp;
$g_srcfile="temp/".$sourcefile;
$g_dstfile=$destfile;
$g_fw=$forcedwidth;
$g_fh=$forcedheight;
$g_is=getimagesize($g_srcfile);
if ($g_is[0] >= $g_is[1]) {
$orientation = 0;
} else {
$orientation = 1;
$g_fw = $forcedheight;
$g_fh = $forcedwidth;
}
if ($g_is[0] > $g_fw ¦¦ $g_is[1] > $g_fh) {
if(($g_is[0]-$g_fw)>=($g_is[1]-$g_fh)) {
$g_iw=$g_fw;
$g_ih=($g_fw/$g_is[0])*$g_is[1];
} else {
$g_ih=$g_fh;
$g_iw=($g_ih/$g_is[1])*$g_is[0];
}
$t = 1;
} else {
$g_iw = $g_is[0];
$g_ih = $g_is[1];
$t = 2;
}
if ($t == 1) {
$img_src=imagecreatefromjpeg($g_srcfile);
$img_dst=imagecreatetruecolor($g_iw,$g_ih);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $g_iw, $g_ih, $g_is[0], $g_is[1]);
if(!imagejpeg($img_dst, $destfile, 90))
{
exit();
}
} else if ($t == 2) {
copy( $g_srcfile, $g_dstfile );
}
}

omoutop

6:21 am on Jul 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thx both of you for your tips!I will try to update my script to work like this..thx againa, i am gratefull