Her er en funktion, som man kan bruge for at resize billeder.
Den er lavet af XyborX, og det anbefales kraftigt, at man ofte besøger han side, www.xyborx.dk og kigger efter opdateringer (direkte link:
http://www.xyborx.dk/image.php). Jeg har oprettet denne tråd, da hans server for tiden er nede.
Der kan dog være et par funktioner, der ikke virker – det kommer an på sin version af det installerede GD-Lib. Det drejer sig om (du kan evt. bruge denne funktion til at checke det med:
http://www.php.net/manual/en/function.function-exists.php): imagecreatetruecolor (brug i stedet imagecreate – men kun, hvis der er problemer)
imagecopyresampled (brug i stedet imagecopyresized – men kun, hvis der er problemer)
/*************************************************************\\
* Image resizer, version 2.0 - Mar9,2003 *
* Returns true on success, false if there are errors *
* If destination file is specified, resized picture will be *
* saved with the specified name. Else, it is shown to browser *
* Version 2: Now scales both up and down, still preserving *
* aspect ratio, and it accepts not only jpeg, but *
* also png and possibly bmp *
}*************************************************************{
* Created by XyborX (www.xyborx.dk) *
* Feel free to use, modify and distribute, without profit. *
* I cannot be held liable for any damage this script might do *
\\*************************************************************/
function imageresize($sourcefile,$maxwidth,$maxheight,$destinationfile=false)
{
$types=Array();
$types[1]='gif';
$types[2]='jpeg';
$types[3]='png';
$types[6]='wbmp';
list($width,$height,$type)=getimagesize($sourcefile);
$type_name=$types[$type];
$imagecreatefromtype='imagecreatefrom'.$type_name;
$imagetype='image'.$type_name;
if(!function_exists($imagecreatefromtype) || !function_exists($imagetype))
{
return false;
}
if($img_src=$imagecreatefromtype($sourcefile))
{
$ratio_height=$maxheight/$height;
$ratio_width=$maxwidth/$width;
$ratio=min($ratio_width,$ratio_height);
$newwidth=$width*$ratio;
$newheight=$height*$ratio;
$img_dst=imagecreatetruecolor($newwidth,$newheight);
// $img_dst=imagecreate($newwidth,$newheight);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// imagecopyresized($img_dst, $img_src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
if($destinationfile==false)
{
//header("Content-type: image/jpeg");
$imagetype($img_dst);
}
else
$imagetype($img_dst,$destinationfile);
imagedestroy($img_dst);
return true;
}
else
{
return false;
}
}
mikl | mikl@mikl.dk | MSN: mikl@mikl.dk