Scripts
Function: image_resize()
Aangezien het parse_image script de mogelijkheid miste om een plaatje echt te resizen (en dus bandbreedte te besparen) heb ik er eventjes 1tje geschreven graag commentaar :)
function-imageresize
<?php
function image_resize($image,$width,$height,$color=array(0,0,0)){
if(@$size = getimagesize($image)){
$newsize=array();
if($size[0]==$size[1]){
$newsize['width']=$width;
$newsize['height']=$height;
}elseif($size[0]<$size[1]){
$ratio=$height/$size[1];
$newsize['width']=$ratio*$size[0];
$newsize['height']=$ratio*$size[1];
}else{
$ratio=$width/$size[0];
$newsize['width']=$ratio*$size[0];
$newsize['height']=$ratio*$size[1];
}
if($newsize['width']>$size[0]){
$newsize['width']=$size[0];
}
if($newsize['height']>$size[1]){
$newsize['height']=$size[1];
}
switch($size[2]){
case 1:
$input=imagecreatefromgif($image);
break;
case 2:
$input=imagecreatefromjpeg($image);
break;
case 3:
$input=imagecreatefrompng(image);
break;
default:
$input=imagecreate($width,$height);
imagecolorallocate($input,125,125,125);
imagestring($input,2,2,2,'Wrong image type',imagecolorallocate($input,230,230,230));
}
$output=imagecreatetruecolor($width,$height);
imagecolorallocate($output,$color[0],$color[1],$color[2]);
imagecopyresampled($output,$input,0,0,0,0,$newsize['width'],$newsize['height'],$size[0],$size[1]);
}else{
$output=imagecreate($width,$height);
imagecolorallocate($output,125,125,125);
imagestring($output,2,2,2,'The image does not exists',imagecolorallocate($output,230,230,230));
imagestring($output,2,2,15,'or is not reachable',imagecolorallocate($output,230,230,230));
}
imagepng($output);
}
image_resize('http://website.com/pic.gif',100,200,array(255,0,0));
/* de eerste waarde is de url naar het plaatje
de 2e de breedte
de 3e is de hoogte
en de 4e de kleuren (in RGB) */
?>
Reacties
0