Page 1 of 1
GD image resizing
Posted: Sat Feb 27, 2010 9:53 am
by eranj
I'm trying to resize an image by using GD, here is my code:
Code: Select all
header('Content-Type: image/jpeg');
$image = imageCreateFromJpeg("img.jpg");
imagecolortransparent($image,imagecolorat($image,0,0));
$image_c = imagecreatetruecolor($newW, newH);
imagecopyresampled($image_c, $image, 0, 0, 0, 0, $newW, $newH, imagesx($image), imagesy($image));
imagejpeg($image_c);
what's wrong?
Re: GD image resizing
Posted: Sun Feb 28, 2010 7:04 pm
by yacahuma
Code: Select all
class STCThumb
{
//Thumb::resize('ffff.jpg','fff_.jpg',320,240,65);
function resize($origfile,$dstfile,$max_width,$max_height,$quality)
{
//echo "STCThumb::resize ORIG=[$origfile],DEST=[$dstfile],MAX_WIDTH=[$max_width],MAX_HEIGHT=[$max_height],QUALITY=[$quality]";
// Path to your jpeg
$size = GetImageSize($origfile); // Read the size
$width = $size[0];
$height = $size[1];
if($width == $max_width || $height == $max_height)
{
//dont do anything
copy($origfile, $dstfile);
}
// Proportionally resize the image to the
// max sizes specified above
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height) )
{
$tn_width = $width;
$tn_height = $height;
}
elseif (($x_ratio * $height) < $max_height)
{
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}
else
{
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
// Increase memory limit to support larger files
ini_set('memory_limit', '32M');
// Create the new image!
$src = ImageCreateFromJpeg($origfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
// ImageJpeg($dst);
//save new file
$itworked = imagejpeg($dst,$dstfile,$quality);
// Destroy the images
ImageDestroy($src);
ImageDestroy($dst);
return $itworked;
}
function create($name,$filename,$new_w,$new_h,$quality=75)
{
$src_img=imagecreatefromjpeg($name);
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
$dst_img=ImageCreateTrueColor($new_w,$new_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,$old_x,$old_y);
$itworked = imagejpeg($dst_img,$filename,$quality);
imagedestroy($dst_img);
imagedestroy($src_img);
return $itworked;
}
}
Re: GD image resizing
Posted: Sun Feb 28, 2010 9:55 pm
by requinix
What's wrong is you didn't tell us why you think there's something wrong.
Re: GD image resizing
Posted: Mon Mar 01, 2010 3:23 am
by eranj
My code is just like ur create function :O
I tried my code on my computer and on another server and at the both of them there is no error, its just writing the URL address..
Re: GD image resizing
Posted: Mon Mar 01, 2010 5:33 am
by yacahuma
make sure gd is in your php.ini
Re: GD image resizing
Posted: Mon Mar 01, 2010 5:49 am
by eranj
GD is working for me...
just the resizing dont work

Re: GD image resizing
Posted: Mon Mar 01, 2010 9:52 am
by pickle
Do you get anything? How exactly doesn't it work? Can you download the image?
Re: GD image resizing
Posted: Mon Mar 01, 2010 1:16 pm
by eranj
its just printing on the screen the URL address

...
Re: GD image resizing
Posted: Mon Mar 08, 2010 9:53 am
by pickle
In my experience, that's an indication that the proper headers weren't set - the browser isn't recognizing the file as an image.