Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help in resizing image

    hi

    i am trying to resize an image to thumbnail size. the code below is working. but the problem with this is when my image is resizes to thumbnail size then its quality gets bad. so how can i maintain the quality of the image after resizing it?

    Code:
    <?php
    function imagetothumb($imagename, $extension) {
    $save = "thumb/".$imagename;
    $filename = $imagename;//'image.jpg';
    $percent = 0.05;
    //$width = 50;
    //$height = 50;
    
    header('Content-type: image/'.$extension);
    
    list($width, $height) = getimagesize($filename);
    $newwidth = $width * $percent;
    $newheight = $height * $percent;
    
    $thumb = imagecreatetruecolor($newwidth, $newheight);
    $source = imagecreatefromjpeg($filename);
    
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    
    imagejpeg($thumb,$save,100);
    }
    
    
    function getExtension($str) {
        $i = strrpos($str,".");
        if (!$i) { return ""; }
        $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);
        return $ext;
    }
    $imagename = "image3.jpg";
    $ext = getExtension($imagename);
    $ext = strtolower($ext);
    
    //imageresizer($imagename,$ext);
    
    imagetothumb($imagename,$ext);
    
    ?>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    You'll want to use "imagecopyresampled" instead of "imagecopyresized".

    So use this:
    imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    Instead of this:
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);


    If I didn't get the properties right in my example, look it up in the PHP manual:
    http://php.net/manual/en/function.im...yresampled.php


  •  

    Posting Permissions

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