Jump to content

how to resize uploaded images to the certain scale, on the same ratio...


newrehmi

Recommended Posts

I am sorry if this has been questioned for many times... But i still can't find a way to do it how...

 

how to resize the uploaded images to the certain scale, on the same ratio. Because all of the uploaded photo in my site is so big (like 1.5MB) that make it slow to load... I don't want to set the MAX_FILE_SIZE too low because it will be hard to upload...

 

Edit: The extension is not only jpeg.

 

Thanks very much indeed,

and sorry for my broken english.

Link to comment
Share on other sites

function generate_thumbnail($image, $max, $output){
    if(!file_exists($output)){
        $size = getimagesize($image);
        if($size[0] > $size[1]){
            $type = trim(str_replace('image/',' ', image_type_to_mime_type($size[2])));
            $newheight = $max*($size[1]/$size[0]);
            switch($type){
                case "png":
                $oimage = imagecreatefrompng($image);
                break;
                case "jpg":
                case "jpeg":
                $oimage = imagecreatefromjpeg($image);
                break;
                case "gif":
                $oimage = imagecreatefromgif($image);
                break;
                default:
                trigger_error("Invalid Image Type", E_USER_WARNING);
                break;
            }
            $nimage = imagecreatetruecolor( $max , $newheight );
            imagecopyresampled($nimage,$oimage, 0,0, 0, 0, $max , $newheight, $size[0] , $size[1]);
            imagegif($nimage,$output,100);
        }else{
            $type = trim(str_replace('image/',' ', image_type_to_mime_type($size[2])));
            $newwidth = $max*($size[0]/$size[1]);
            switch($type){
                case "png":
                $oimage = imagecreatefrompng($image);
                break;
                case "jpg":
                case "jpeg":
                $oimage = imagecreatefromjpeg($image);
                break;
                case "gif":
                $oimage = imagecreatefromgif($image);
                break;
                default:
                trigger_error("Invalid Image Type", E_USER_WARNING);
                break;
            }
            $nimage = imagecreatetruecolor( $newwidth , $max );
            imagecopyresampled($nimage,$oimage, 0,0, 0, 0, $newwidth , $max , $size[0] , $size[1]);
            imagegif($nimage,$output,100);
        }
    }
}

where the $image is the beginning image and the $output is the new image to be generated

Link to comment
Share on other sites

function generate_thumbnail($image, $max, $output){
    if(!file_exists($output)){
        $size = getimagesize($image);
        if($size[0] > $size[1]){
            $type = trim(str_replace('image/',' ', image_type_to_mime_type($size[2])));
            $newheight = $max*($size[1]/$size[0]);
            switch($type){
                case "png":
                $oimage = imagecreatefrompng($image);
                break;
                case "jpg":
                case "jpeg":
                $oimage = imagecreatefromjpeg($image);
                break;
                case "gif":
                $oimage = imagecreatefromgif($image);
                break;
                default:
                trigger_error("Invalid Image Type", E_USER_WARNING);
                break;
            }
            $nimage = imagecreatetruecolor( $max , $newheight );
            imagecopyresampled($nimage,$oimage, 0,0, 0, 0, $max , $newheight, $size[0] , $size[1]);
            imagegif($nimage,$output,100);
        }else{
            $type = trim(str_replace('image/',' ', image_type_to_mime_type($size[2])));
            $newwidth = $max*($size[0]/$size[1]);
            switch($type){
                case "png":
                $oimage = imagecreatefrompng($image);
                break;
                case "jpg":
                case "jpeg":
                $oimage = imagecreatefromjpeg($image);
                break;
                case "gif":
                $oimage = imagecreatefromgif($image);
                break;
                default:
                trigger_error("Invalid Image Type", E_USER_WARNING);
                break;
            }
            $nimage = imagecreatetruecolor( $newwidth , $max );
            imagecopyresampled($nimage,$oimage, 0,0, 0, 0, $newwidth , $max , $size[0] , $size[1]);
            imagegif($nimage,$output,100);
        }
    }
}

where the $image is the beginning image and the $output is the new image to be generated

 

sorry for the hijacking, well i too want to know the same like on site some users try to upload images which size are like 5mb, 10mb etc etc. and height and width also differs as some images are huge, so is there a specific code which will actually automatically reduce the 10mb pic size to some default like 500kb and some fixed height and width during the upload process?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.