Results 1 to 6 of 6

Thread: GD Thumbnailing Probs

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Posts
    3

    GD Thumbnailing Probs

    Hi,
    I am writing an eCommerce site for a client and when showing products i wish to show a thumbnail which is generated on the fly from a picture. It works, but not totally correctly. This is my code (I borrowed it from somewhere and tweaked it slightly)

    PHP Code:

    <?php

        $error 
    False;
        
        
    $system explode("."$image);
        
        
    // check if the 'image' parameter has been given
        
    if (!isset($image)) // if not generate an error
            
    $error True;
            
        
    // check if the 'img_size' parameter has been given
        
    if (!isset($img_size) && ($resize != 'no')) // if not generate an error
            
    $error True;
        
        if(
    $resize != 'no')    {
            
    // ensure that $img_size is an integer 
            
    $img_size = (integer) $img_size;
            
            
    // check that the size asked for is sensible
            
    if ($img_size 20)// if not generate an error
                
    $error True;    
        }
        
        if(!
    $bigimage=@imagecreatefromjpeg($image))
        
    $error true;        

        
        if(
    $error){ // generate an error image
            
    $img ImageCreate(123100);                
            
    $bg ImageColorAllocate($img255255255);
            
    $white ImageColorAllocate($img25500);
            
    ImageString($img53020"Picture"$white);
            
    ImageString($img54540"Not"$white);
            
    ImageString($img52560"Available"$white);
            
    ImagePNG($img);
            
    ImageDestroy($img);
            exit();
        }
        
        
    $x=imageSX($bigimage);
        
    $y=imageSY($bigimage);        
        
        if(
    $resize == 'no' || (($x $y) && ($x $img_size)) || (($y $x) && ($y $img_size))){

            
    imagejpeg($bigimage);
            exit();
        }
        
        
    // find the larger dimension
        
    if ($x>$y) {    // if it is the width then
            
    $dx 0;                    // the left side of the new image
            
    $w $img_size;                // the width of the new image
            
    $h = ($y $x) * $img_size;    // the height of the new image
            
    $dy 0;                    // the top of the new image
        
    } else {    // if the height is larger then
            
    $dy 0;                    // the top of the new image
            
    $h $img_size;                // the height of the new image
            
    $w = ($x $y) * $img_size;    // the width of the new image
            
    $dx 0;    // the left edgeof the new image
        
    }
        
        
    $tnimage ImageCreate($w$h);    

        
    ImageCreateTrueColor($w$h);
        
    ImageCopyResampled($tnimage$bigimage$dx$dy00$w$h$x$y); 
            
        
    // copy the resized version into the thumbnal image
        //ImageCopyResized($tnimage, $bigimage, $dx, $dy, 0, 0, $w, $h, $x, $y);    

        
    imagejpeg($tnimage);    

        
    // release the memory used by the thumbnail image
        
    ImageDestroy($tnimage);
        
        
    // release the memory used by the original big image
        
    ImageDestroy($bigimage);
        

    ?>
    And this is the result:



    This is what it should look like:



    Any ideas where i might be going wrong?
    Thanks for the help

    -Pointybeard

  2. #2
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    are you using the correct version of teh GD library? I think it needs 2.0.1

    also where is img_size coming from?

    edit: after playing around with it is, its the image size. it is too small for what you are doing. I made it 290 and it looked exactly like the image.

    ike I said before, where is the img_size coming from?
    Last edited by phpman; Sep 11th, 2003 at 03:16 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Posts
    3
    Hi,
    I figured it out now. img_size is passed thru the URL. But thats not the prob, what i should have done was instead of this:

    PHP Code:
    $tnimage ImageCreate($w$h);     

    ImageCreateTrueColor($w$h); 
    i should have done this:

    PHP Code:
    $tnimage ImageCreateTrueColor($w$h); 
    It works perfectly now. Thanks anywayz.

    -Pointybeard

  4. #4
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    whatever. that wasn't the problem because I had it working just fine with your code you have now. just supplied a img_size greater than the actual image and it showed up just fine.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Posts
    3
    Hmm, thats interesting. But then i wast actually trying to make the image larger, i was only thumbnailing it.

    -Pointybeard

  6. #6
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    I never had the original size to begin with so what I had works great with your code.

    maybe if I had the original it would be different.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width