<?php
function resize($image,$x,$y=NULL,$wm=NULL,$wml='br'){
if(!file_exists($image)){
return false;
}
$images = array();
if($wm !== '' && $wm !== NULL && file_exists($wm)){
$images['wmimg'] = $wm;
}
$images['img'] = $image;
foreach($images as $key=>$value){
$type = substr($value,strrpos($value,'.'));
if(stristr($type,'i')){
$$key = imagecreatefromgif($value);
}
if(stristr($type,'j')){
$$key = imagecreatefromjpeg($value);
}
if(stristr($type,'n')){
$$key = imagecreatefrompng($value);
}
}
$size = array();
if($y === '' || $y === NULL){
$size['x'] = imageSX($img);
$size['y'] = imageSY($img);
if($size['x'] >= $size['y']){
$size['dest_x'] = $x;
$size['dest_y'] = ceil($size['y'] * ($x / $size['x']));
}else{
$size['dest_y'] = $x;
$size['dest_x'] = ceil($size['x'] * ($x / $size['y']));
}
$dest = imageCreatetruecolor($size['dest_x'],$size['dest_y']);
}else{
$dest = imagecreatetrueColor($x,$y);
$size['x'] = imageSX($img);
$size['y'] = imageSY($img);
$size['dest_x'] = $x;
$size['dest_y'] = $y;
}
imagecopyresized($dest, $img, 0, 0, 0, 0, $size['dest_x'], $size['dest_y'], $size['x'], $size['y']);
if(isset($wmimg)){
$size['wmx'] = imageSX($wmimg);
$size['wmy'] = imageSY($wmimg);
$size['wmh'] = strtolower($wml{0}) === 'b' ? ($size['dest_y'] - $size['wmy'] - 2) : 2;
$size['wmw'] = strtolower($wml{1}) === 'r' ? ($size['dest_x'] - $size['wmx'] - 2) : 2;
imagecopy($dest, $wmimg, $size['wmw'], $size['wmh'], 0, 0, $size['wmx'], $size['wmy']);
imagedestroy($wmimg);
}
imagedestroy($img);
return $dest;
}
?>
<?php
header('Content-type: image/jpeg'); // set your header for jpeg image
imagejpeg(resize('imagefile.png',100));
// outputs imagefile.png as a jpeg constrained to 100 px with its ratio kept the same
header('Content-type: image/png'); // set your header for png image
imagepng(resize('filename.png',100,100));
// outputs filename.png as a png and as a 100X100 image
header('Content-type: image/png'); // set your header for png image
imagepng(resize('imagefile.jpg',100,NULL,'logo.png ','bl'));
// outputs imagefile.jpg as a png constrained to 100 px with its ratio kept the same
// it also adds logo.png on top of the image on the bottom left of the image
?>