Sad lige i går og lavede en Image-Resizer function, og synes lige jeg ville dele den med jer
Den understøtter kun jpg-billeder, men kan relativt nemt omskrives til png. (pga problemer med licenser understøtter GDlib ikke længere gif :/)
<pre><?php
/*************************************************************\\
* Image resizer, version 1.3 alpha *
* 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 *
}*************************************************************{
* 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 imageresizejpeg($sourcefile,$maxwidth,$maxheight,$destinationfile=false)
{
if($img_src=@imagecreatefromjpeg($sourcefile))
{
$info=@getimagesize($sourcefile);
if($info[1]>$maxheight || $info[0]>$maxwidth)
{
$ratio_height=$maxheight/$info[1];
$ratio_width=$maxwidth/$info[0];
$ratio=min($ratio_width,$ratio_height);
$newwidth=intval($info[0]*$ratio);
$newheight=intval($info[1]*$ratio);
$img_dst=@imagecreatetruecolor($newwidth,$newheight);
@imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $newwidth, $newheight, $info[0], $info[1]);
if($destinationfile==false)
{
header("Content-type: image/jpeg");
@imagejpeg($img_dst);
}
else
@imagejpeg($img_dst,$destinationfile);
@imagedestroy($img_dst);
}
else
{
if($destinationfile==false)
{
header("Content-type: image/jpeg");
@imagejpeg($img_src);
}
else
@imagejpeg($img_src,$destinationfile);
}
@imagedestroy($img_src);
return true;
}
else
{
return false;
}
}
// Code below is only to demonstrate usage
if(!extension_loaded('gd'))
{
echo 'The GD-extension must be loaded for this to work..';
}
else
{
if($_GET['image'])
{
$file='./pics/'.str_replace(array(' ','/','\\\\','.'),'',$_GET['image']).'.jpg';
//$file2='./pics/thumb_'.str_replace(array(' ','/','\\\\','.'),'',$_GET['image']).'.jpg';
$width=intval($_GET['width'])>0?intval($_GET['width']):150;
$height=intval($_GET['height'])>0?intval($_GET['height']):150;
if(!imageresizejpeg($file,$width,$height))
echo 'Errors opening '.$file.' ... hmm.. :/';
}
else
{
echo '<FORM action="'.$_SERVER['PHP_SELF'].'">'
.'Name: /pics/<SELECT name="image">'
.'<OPTION value="biglogo">biglogo</OPTION>'
.'<OPTION value="allbg">allbg</OPTION>'
.'<OPTION value="blogoinv">blogoinv</OPTION>'
.'<OPTION value="wally1">wally1</OPTION>'
.'<OPTION value="wally2">wally2</OPTION>'
.'</SELECT>.jpg<BR>'
.'Max width <INPUT type="text" name="width" value="150"><BR>'
.'Max height <INPUT type="text" name="height" value="150"><BR>'
.'<INPUT type="submit" value="Test!">'
.'</FORM><HR>';
show_source('./'.$_SERVER['PHP_SELF']);
}
}
?></pre>
Eksempel kan ses på
http://www.xyborx.dk/image.php// XyborX \\\<?php $thinkers=Array('Albert Einstein'=>'Imagination is more important than knowledge');?>