Results 1 to 3 of 3

Thread: What is the best way to resize an image?

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default What is the best way to resize an image?

    How can I resize an image to be smaller and retain the highest possible image quality? For example I could use the following:
    PHP Code:
    <?php
    $img    
    'http://www.mysite/myimage.jpg';
    $width  300px;
    $height '';

    // Content type
    header('Content-type: image/jpeg');

    // Get new dimensions
    list($width1$height1) = getimagesize($img);
    if (
    $width =='') {$j=$height/$height1$width  $width1  $j;}
    if (
    $height=='') {$j=$width/$width1;   $height $height1 $j;}
    $new_width $width;
    $new_height $height;

    // Resample
    $image_p imagecreatetruecolor($width$height);
    $image imagecreatefromjpeg($img);
    imagecopyresampled($image_p$image0000$new_width$new_height$width1$height1);

    // Output
    imagejpeg($image_pnull100);
    $img=preg_replace('/h.*\//','',$img);
    $img=strtolower($img);
    $dest='../images/thumbs/$img';
    imagejpeg($image_p,$dest);
    ?>
    but the image quality seems somewhat decreased compared to
    Code:
    <img src='/images/fullsize/myimage.jpg' width=300 alt=''>
    The above code was hastily copied from my site. I got this code mostly from php.net
    Last edited by james438; 04-25-2009 at 12:54 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #2
    Join Date
    Dec 2008
    Posts
    28
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default

    PHP Code:
    imagejpeg($image_pnull100);

    imagejpeg($image_p,$dest); 
    why do you have both? :O

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    imagecopyresampled() is the correct function. Obviously you'll want to input high quality and output high quality (PNG is "lossless", JPG will lose some quality, but not much at a very high setting), and beyond that it's just setup stuff. The actual dimensions are just math and imagecopyresampled() will handle the "quality" during the resizing, so there's not much to worry about there.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •