GD image resizing

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
eranj
Forum Newbie
Posts: 5
Joined: Sat Feb 27, 2010 9:48 am

GD image resizing

Post 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?
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: GD image resizing

Post 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;
 
  }
}
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: GD image resizing

Post by requinix »

What's wrong is you didn't tell us why you think there's something wrong.
eranj
Forum Newbie
Posts: 5
Joined: Sat Feb 27, 2010 9:48 am

Re: GD image resizing

Post 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..
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: GD image resizing

Post by yacahuma »

make sure gd is in your php.ini
eranj
Forum Newbie
Posts: 5
Joined: Sat Feb 27, 2010 9:48 am

Re: GD image resizing

Post by eranj »

GD is working for me...
just the resizing dont work :(
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: GD image resizing

Post by pickle »

Do you get anything? How exactly doesn't it work? Can you download the image?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
eranj
Forum Newbie
Posts: 5
Joined: Sat Feb 27, 2010 9:48 am

Re: GD image resizing

Post by eranj »

its just printing on the screen the URL address :( ...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: GD image resizing

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply