SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Guru okrogius's Avatar
    Join Date
    Mar 2002
    Location
    US
    Posts
    622
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    gd image functions

    How would I resize an image file to x pixels height and width on upload?

  2. #2
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Not really what you wanted, but...

    Good question! I'm trying to do exactly the same thing right now! [sodding gd... grumble grumble]

    Don't know if the following might interest you, but it might point you in the right direction. [Or the WRONG direction, knowing me], but this piece of code resizes any image to fit into an 80 x 80 box. (i.e. whatever side is largest gets reduced to 80, and the other side is changed accordingly).

    I'm trying to find out how to change the end of this to WRITE the result back to disk.

    PHP Code:

    <?php

    // We're going to try to create a quarter-sized thumbnail of an image.

    $src ImageCreateFromJPEG($_GET['image']);
    $width ImageSx($src);
    $height ImageSy($src);

    if (
    $height $width) { 
       
    $ratio $height 80
         
    $x round($width $ratio);
         
    $y 80;
    }
    else {
       
    $ratio $width 80
         
    $x 80;
         
    $y round($height ratio);
    }

    $dst ImageCreateTrueColor($x,$y);
    ImageCopyResized($dst,$src,0,0,0,0,$x,$y,$width,$height);

    header('Content-Type: image/png');
    ImagePNG($dst);

    ?>

  3. #3
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you end up finding out how?

  4. #4
    SitePoint Guru okrogius's Avatar
    Join Date
    Mar 2002
    Location
    US
    Posts
    622
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by AtomicPenguin
    Did you end up finding out how?
    No, not yet.

    But thanks for that script, I could use it on my forums for avatars.

  5. #5
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ImageMagick should do a better jon if you have it installed

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  6. #6
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ImageMagick - really? more robust, quicker or what? I must say I'm having a little trouble with gd. It only seems to render two out of three jpg images. I haven't figured out why yet.

  7. #7
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It should produce much higher quality images

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  8. #8
    SitePoint Evangelist -Oz-'s Avatar
    Join Date
    Nov 2001
    Location
    Phoenix, AZ, USA
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is what i use, this only works for JPGS (because GD only works with jpgs now).

    PHP Code:
    $quality '100'//image quality, leave alone
    $src_img imagecreatefromjpeg($DOCUMENT_ROOT "/newspics/" $filename1); //where to get the image from
    $src_width ImageSX($src_img); //source width (set by script)
    $src_height ImageSY($src_img); //source heigth (set by script)
    $dest_width '65'//destination image width
    $dest_height '65'//destination image height
    $dst_img imagecreate($dest_width,$dest_height); //create the new image blank
    ImageCopyResized($dst_img$src_img0000$dest_width,$dest_height$src_width$src_height); //copy the old image to the new image blank and resize
    imagedestroy($src_img); //delete original image
    imagejpeg($dst_img$DOCUMENT_ROOT "/newspics/" $filename1$quality); //save the new image
    imagedestroy($dst_img); //destory the temporary file for the new image 
    In the stuff there is a folder in locations called /newspics/, that is where the image is currently stored, it can be saved.

    This is good as well: http://www.php.net/manual/en/functio...opyresized.php
    Oz
    GamersMark - On Target Gaming
    OzTheory - Programming and Web Solutions
    AmIBlocked - Check if you've been blocked on IM

  9. #9
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pardon my slowness, but how do you call this script? From within a img tag, or separately? I can't seem to get it going on my machine, that's all.

    Man - I'd kill, maim, rape & pillage for a good book on gd right now.

  10. #10
    SitePoint Evangelist -Oz-'s Avatar
    Join Date
    Nov 2001
    Location
    Phoenix, AZ, USA
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mine is ina php file after the image is uploaded, here the whole upload and resize:

    PHP Code:
            if ($picture != 'none') {
            if ( 
    "image/pjpeg" == $HTTP_POST_FILES['picture']['type'] )
              
    $extension1 ".jpg";
            else
              
    $extension1 ".gif";
            
            
    // The complete path/filename
            
    $filename1 time() . $REMOTE_HOST $extension1;
            
            
    // Copy the file
            
    if (copy($HTTP_POST_FILES['picture']['tmp_name'], $DOCUMENT_ROOT "/newspics/" $filename1)) {
            } else {
              echo(
    "<P>Could not save file as $filename1!");
            }}
            if(
    $extension1 == '.jpg'){
                
    $quality '100';
                
    $src_img imagecreatefromjpeg($DOCUMENT_ROOT "/newspics/" $filename1);
                
    $src_width ImageSX($src_img);
                
    $src_height ImageSY($src_img);
                
    $dest_width '65';
                
    $dest_height '65';
                
    $dst_img imagecreate($dest_width,$dest_height);
                
    ImageCopyResized($dst_img$src_img0000$dest_width,
                
    $dest_height$src_width$src_height);
                
    imagedestroy($src_img);
                
    imagejpeg($dst_img$DOCUMENT_ROOT "/newspics/" $filename1$quality);
                
    imagedestroy($dst_img);
            } 
    Oz
    GamersMark - On Target Gaming
    OzTheory - Programming and Web Solutions
    AmIBlocked - Check if you've been blocked on IM

  11. #11
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Los Angeles, California
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for bringing up an old topic.

    Is there a way to make the resized image not suck? The quality is awful. Take a look: http://www.netsplittech.com/images/s_tower.jpg I resized that using the code that -Oz- provided (thanks). I tried editing the $quality but making it higher just made the thumbnail even worse! Any suggestions?
    www.iyeinteractive.com - Website Design and Programming
    www.totalnonsense.net - my site, Under Construction

  12. #12
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Someone posted a function in the user notes under some gd function. From the feedback that was written it seemed to do its job very well.

  13. #13
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  14. #14
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Los Angeles, California
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot! That worked perfectly.
    www.iyeinteractive.com - Website Design and Programming
    www.totalnonsense.net - my site, Under Construction

  15. #15
    SitePoint Zealot
    Join Date
    May 2001
    Location
    UK
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hiya,
    There is a few on that thread and a lot of information but neither of the functions worked for me. They say you have to have ImageResizeBicubic enabled, but it is not listed under my php info (as they say it should be) either as enabled or not and there is no option for it in php.ini. I thought their may be an option in the GD config file, but I can't find that and the manual does not give its location.

    The GD manual does include examples for C and a few other languages (unfortunatly not PHP). But I was wondering do you need to have "include GD" or similar for it to work?

    I am using the latest stable version of GD, v.1.8.4.

    Any advise? Spent quite a while on this.


Bookmarks

Posting Permissions

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