GD help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

GD help

Post by ecaandrew »

Can anyone please help me with GD, I am trying to allow users to upload files, and I would like it to resize the photo, I don't wnat the photo to be an exact width and height everytime, I just dont want the width bigger than 130

If you check out my current GD function I am using, it makes the thumb 130x98 every time, plus its horrible quality

Code: Select all

function resampimagejpg($forcedwidth, $forcedheight, $sourcefile, $destfile, $imgcomp)
{
$g_imgcomp=100-$imgcomp;
$g_srcfile=$sourcefile;
$g_dstfile=$destfile;
$g_fw=$forcedwidth;
$g_fh=$forcedheight;

if(file_exists($g_srcfile))
{
$g_is=getimagesize($g_srcfile);
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];   
}
$img_src=imagecreatefromjpeg($g_srcfile);
$img_dst=imagecreate($g_iw,$g_ih);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $g_iw, $g_ih, $g_is[0], $g_is[1]);
imagejpeg($img_dst, $g_dstfile, $g_imgcomp);
imagedestroy($img_dst);
return true;
}
else
return false;
}
You can see this in action at http://www.xeofriends.com

just signup and select "Upload/Change Photos"

thanks any help would be awesome!


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

check to see if your GD has this function:

ImageCreateTruecolor()

if it does, use this function instead of imagecreate() to create your new image. I use it, and my thumbnails looks great.

Also, if you specify a specific width and height, your thumbnail looks funny - images get distorted. :)

check this function at
http://us2.php.net/imagecreatetruecolor
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

Post by ecaandrew »

how would i get that ImageCreateTruecolor() into my GD function, and if i dont specify a width and height, how do i make sure the thumbnail is no larger tahn 130px width?
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

Post by ecaandrew »

i changed it to imagecreatetruecolor, works awesome, but how do i make it so i dont have to specify a width and height, and the thumbnail will never be bigger than 130px, thanks!

Code: Select all

function resampimagejpg($forcedwidth, $forcedheight, $sourcefile, $destfile, $imgcomp)
{
$g_imgcomp=100-$imgcomp;
$g_srcfile=$sourcefile;
$g_dstfile=$destfile;
$g_fw=$forcedwidth;
$g_fh=$forcedheight;

if(file_exists($g_srcfile))
{
$g_is=getimagesize($g_srcfile);
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];   
}
$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]);
imagejpeg($img_dst, $g_dstfile, $g_imgcomp);
imagedestroy($img_dst);
return true;
}
else
return false;
}

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

start using [syntax=php][/syntax]
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

Post by ecaandrew »

ook sorry, anyone know how, thx :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you must specify the dimensions. You could however, just specify 1 dimension: 130.. and the use simple math and comparisons to determine which dimension of the image is larger, and scale the smaller dimension accordingly.
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

Post by ecaandrew »

i got it thanks
here is what i came up with

Code: Select all

function resampimagejpg($size, $sourcefile, $destfile, $imgcomp)
{
$g_imgcomp=100-$imgcomp;
$g_srcfile=$sourcefile;
$g_dstfile=$destfile;

list ( $width, $height, $type, $attr ) = getimagesize ( $sourcefile );

if ( $height == $width )
{
$g_fh = $size;
$g_fw = $size;
}
elseif ( $height > $width )
{
$g_fh = $size;
$g_fw = round ( $width / round ( $height / $size ) );
}
elseif ( $height < $width )
{
$g_fh = round ( $height / round ( $width / $size ) );
$g_fw = $size;
}
else
{
$g_fh = $height;
$g_fw = $width;
}

if(file_exists($g_srcfile))
{
$g_is=getimagesize($g_srcfile);
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];   
}
$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]);
imagejpeg($img_dst, $g_dstfile, $g_imgcomp);
imagedestroy($img_dst);
return true;
}
else
return false;
}
Post Reply