Return to Snippet

Revision: 37373
at December 9, 2010 02:37 by redoc


Initial Code
function image($file_temporary, $file_destination, $width, $height, $type = "jpg") {
        $type 		= strtolower($type);
        $image_size = getimagesize($file_temporary);
 
        if($image_size[0] > $image_size[1]) {
            $new_width 	= $width;
            $new_height = $image_size[1] * ($new_width / $image_size[0]);
        }
        else {
            $new_height = $height;
            $new_width 	= $image_size[0] * ($new_height / $image_size[1]);
        }
 
        switch(image_type_to_mime_type($image_size[2])) {
            case "image/jpeg":
                $image = imagecreatefromjpeg($file_temporary);
                break;
            case "image/gif":
                $image = imagecreatefromgif($file_temporary);
                break;
            case "image/png":
                $image = imagecreatefrompng($file_temporary);
                break;
            default:
                $t = image_type_to_mime_type($image_size[2]);
                echo "The file type {$t} is not supported, please use either jpeg, gif, or png";
                break;
        }
 
        $thumb = imagecreatetruecolor($new_width, $new_height);
        imagecopyresampled($thumb, $image, 0, 0, 0, 0, $new_width, $new_height, $image_size[0], $image_size[1]);
 
        switch($type) {
            case "jpg":
            case "jpeg":
                //header("Content-type: image/jpeg");
                imagejpeg($thumb, $file_destination);
                break;
            case "gif":
                //header("Content-type: image/gif");
                imagegif($thumb, $file_destination);
                break;
            case "png":
                //header("Content-type: image/png");
                imagepng($thumb, $file_destination);
                break;
            default:
                echo "The image type {$type} is not supported, please choose another.";
                break;
        }
 
        imagedestroy($image);
        imagedestroy($thumb);
		
		return true;
}

Initial URL

                                

Initial Description
I edit this from tylerhall on 11/30/-1

Initial Title
All Image to JPG/JPeG Converter and Resizer

Initial Tags
php, resize, convert

Initial Language
PHP