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 2007
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Image manipulation practices

    Hi I have an image upload script that works but I need it to also resize images. Right now it takes the users image and puts it straight in a folder on the server. I would like to have it check the image to make sure the width and height are not over a specific pixel value, say 800. If it does exceed this value I need to resize the image to be less than 800 * 800 while keeping the proportions correct. That is the fullsize image. I also need it to create a medium (under 300 * 300) and a thumbnail and upload those to different folders.

    What is the best way to do these manipulations? What is the best way to do these manipulations keeping in mind server load.

    Thanks,
    Marc

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Use getimagesize() to get the image dimensions, and a function such as the one below to get the new sizes if the image is larger than your maximum dimensions. Then use imagecreatetruecolor() to make image pointers for the new images; imagecopyresampled() to resize & copy the new images to the newly created image pointers; and finally save them with imagejpeg() or imagepng().

    PHP Code:
    /**
     * ratioSizes - Calculates Proportionately Accurate Image Resizes
     * @param integer Image Width
     * @param integer Image Height
     * @param integer Maximum Width
     * @param integer Maximum Height
     * @return array
     **/

    function ratioSizes(integer $widthinteger $height$maxWidth 250$maxHeight 200)
    {
        if(
    $width $maxWidth || $height $maxHeight)
        {
            
    $diff 0;
            
    $aspectRatio $width $height;
            if(
    $width $height)
            {
                
    $newHeight round($maxWidth $aspectRatio);
                if(
    $newHeight $maxHeight)
                {
                    
    $diff $newHeight $maxHeight;
                }
                return array(
    'width' => round($maxWidth - ($diff $aspectRatio)), 'height' => $newHeight $diff);
            }
            else
            {
                
    $newWidth round($maxHeight $aspectRatio);
                if(
    $newWidth $maxWidth)
                {
                    
    $diff $newWidth $maxWidth;
                }
                return array(
    'width' => $newWidth $diff'height' => round($maxHeight - ($diff $aspectRatio)));
            }

        }
        return array(
    'width' => $width'height' => $height);



  •  

    Posting Permissions

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