Billedskalerings klasse

index.php

<?
include("image.class.php");

$obj_img = new Image();

switch (
$_GET["scale"]) {
case 
"height":
    
$obj_img->scaleByHeight("image_to_scale.jpg"""200); // Output to browser
    
break;
    
case 
"width":
    
$obj_img->scaleByWidth("image_to_scale.jpg""scaled_image"300); // Save to file
    
break;

default:
    
$obj_img->scaleCrop("image_to_scale.jpg"""300200); // Output to browser
}    

?>

image.class.php

<?

/** 
 * This class is using the GD library. Find out more at php.net/gd 
 * Supports: GIF, JPEG and PNG
 * Outputs/saves images in the same type as source image
 **/

class Image {
    
    
/**
     * Scale image by height
     **/
    
function scaleByHeight($srcFile$dstName=""$dstHeight=100) {
    
        if (!
file_exists($srcFile)) {
            exit(
"The file \"".$srcFile."\" does not exists.");
        }
    
        
// Get new dimensions
        
$srcData getimagesize($srcFile);
        
$srcWidth $srcData[0];
        
$srcHeight $srcData[1];
        
        
$dstWidth = (int) (($dstHeight $srcHeight) * $srcWidth);
    
        
// Resample
        
$image_p imagecreatetruecolor($dstWidth$dstHeight);
        
$image imagecreatefromjpeg($srcFile);
        
imagecopyresampled($image_p$image0000$dstWidth$dstHeight$srcWidth$srcHeight);
    
        
// Output
        
if ($dstName == "") {
            
header("Content-Type: ".$srcData['mime']);
            
            if (
strpos($srcData['mime'], "png") !== false) {
                
imagepng($image_p);
            } else if (
strpos($srcData['mime'], "jpeg") !== false) {
                
imagejpeg($image_p);
            } else if (
strpos($srcData['mime'], "gif") !== false) {
                
imagegif($image_p);
            } else {
                return 
false;
            }
        } else {
            if (
strpos($srcData['mime'], "png") !== false) {
                
imagepng($image_p$dstName);
            } else if (
strpos($srcData['mime'], "jpeg") !== false) {
                
imagejpeg($image_p$dstName100); // 100% quality
            
} else if (strpos($srcData['mime'], "gif") !== false) {
                
imagegif($image_p$dstName);
            } else {
                return 
false;
            }
        }
        
        @
imagedestroy($image_p);
        return 
true;
    }
    
    
/**
     * Scale image by width
     **/
    
function scaleByWidth($srcFile$dstName=""$dstWidth=100) {
    
        if (!
file_exists($srcFile)) {
            exit(
"The file \"".$srcFile."\" does not exists.");
        }
    
        
// Get new dimensions
        
$srcData getimagesize($srcFile);
        
$srcWidth $srcData[0];
        
$srcHeight $srcData[1];
        
        
$dstHeight = (int) (($dstWidth $srcWidth) * $srcHeight);
    
        
// Resample
        
$image_p imagecreatetruecolor($dstWidth$dstHeight);
        
$image imagecreatefromjpeg($srcFile);
        
imagecopyresampled($image_p$image0000$dstWidth$dstHeight$srcWidth$srcHeight);
    
        
// Output
        
if ($dstName == "") {
            
header("Content-Type: ".$srcData['mime']);
            
            if (
strpos($srcData['mime'], "png") !== false) {
                
imagepng($image_p);
            } else if (
strpos($srcData['mime'], "jpeg") !== false) {
                
imagejpeg($image_p);
            } else if (
strpos($srcData['mime'], "gif") !== false) {
                
imagegif($image_p);
            } else {
                return 
false;
            }
        } else {
            if (
strpos($srcData['mime'], "png") !== false) {
                
imagepng($image_p$dstName);
            } else if (
strpos($srcData['mime'], "jpeg") !== false) {
                
imagejpeg($image_p$dstName100); // 100% quality
            
} else if (strpos($srcData['mime'], "gif") !== false) {
                
imagegif($image_p$dstName);
            } else {
                return 
false;
            }
        }
        
        @
imagedestroy($image_p);
        return 
true;
    }
    
    
    
/**
     * Scale and crop image
     **/
    
function scaleCrop($srcFile$dstName=""$dstWidth=100$dstHeight=100) {
    
        if (!
file_exists($srcFile)) {
            exit(
"The file \"".$srcFile."\" does not exists.");
        }
    
        
$srcData getimagesize($srcFile);
    
        if (
strpos($srcData["mime"], "jpeg") !== false) {
            
$handle = @imagecreatefromjpeg($srcFile);
        } else if (
strpos($srcData["mime"], "png") !== false) {
            
$handle = @imagecreatefrompng($srcFile);
        } else if (
strpos($srcData["mime"], "gif") !== false) {
            
$handle = @imagecreatefromgif($srcFile);
        } else {
            return 
false;
        }
        
        if (!
$handle) {
            return 
false;
        }
        
        
$srcWidth  = @imagesx($handle);
        
$srcHeight = @imagesy($handle);
    
    
        
$newHandle = @imagecreatetruecolor($dstWidth$dstHeight);
        if (!
$newHandle) {
            return 
false;
        }
        if(
$srcHeight $srcWidth) {
            
$ratio = (double)($srcHeight $dstHeight);
        
            
$cpyWidth round($dstWidth $ratio);
            if (
$cpyWidth $srcWidth) {
                
$ratio = (double)($srcWidth $dstWidth);
                
$cpyWidth $srcWidth;
                
$cpyHeight round($dstHeight $ratio);
                
$xOffset 0;
                
$yOffset round(($srcHeight $cpyHeight) / 2);
            } else {
                
$cpyHeight $srcHeight;
                
$xOffset round(($srcWidth $cpyWidth) / 2);
                
$yOffset 0;
            }
        
        } else {
            
$ratio = (double)($srcWidth $dstWidth);
        
            
$cpyHeight round($dstHeight $ratio);
            if (
$cpyHeight $srcHeight) {
                
$ratio = (double)($srcHeight $dstHeight);
                
$cpyHeight $srcHeight;
                
$cpyWidth round($dstWidth $ratio);
                
$xOffset round(($srcWidth $cpyWidth) / 2);
                
$yOffset 0;
            } else {
                
$cpyWidth $srcWidth;
                
$xOffset 0;
                
$yOffset round(($srcHeight $cpyHeight) / 2);
            }
        }
        
        if (!@
imagecopyresampled($newHandle$handle00$xOffset$yOffset$dstWidth$dstHeight$cpyWidth$cpyHeight)) {
            return 
false;
        }
        @
imagedestroy($handle);
        
        if (
$dstName == "") {
            
header("Content-Type: ".$srcData["mime"]);
            
            if (
strpos($srcData["mime"], "png") !== false) {
                @
imagepng($newHandle);
            } else if (
strpos($srcData["mime"], "jpeg") !== false) {
                @
imagejpeg($newHandle);
            } else if (
strpos($srcData["mime"], "gif") !== false) {
                @
imagegif($newHandle);
            } else {
                return 
false;
            }
        } else {
            if (
strpos($srcData["mime"], "png") !== false) {
                @
imagepng($newHandle$dstName);
            } else if (
strpos($srcData["mime"], "jpeg") !== false) {
                @
imagejpeg($newHandle$dstName100); // 100% quality
            
} else if (strpos($srcData["mime"], "gif") !== false) {
                @
imagegif($newHandle$dstName);
            } else {
                return 
false;
            }
        }
        
        @
imagedestroy($newHandle);
        return 
true;
    }
}
?>