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.

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

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.