$height?$height:$maxheight; $maxwidth = $maxwidth>$width?$width:$maxwidth; // ratios for scaling $x_ratio = $maxwidth/$width; $y_ratio = $maxheight/$height; // ratio for square img $ratio=min($y_ratio, $x_ratio); // get new sizes via ratio $newwidth=round($ratio*$width); $newheight=round($ratio*$height); // get the maximum edge size if (isset($_GET['height'])&&$_GET['height']!="" && isset($_GET['width'])&&$_GET['width']!="") $max=min($_GET['height'], $_GET['width']); else if (isset($_GET['height'])&&$_GET['height']!="") $max=$_GET['height']; else if (isset($_GET['width'])&&$_GET['width']!="") $max=$_GET['width']; else $max=min($width, $height); // if we want if ($square) { if ($width>$height) $ratio=$max/$height; else $ratio=$max/$width; $newheight=$newwidth=$max; } // default radius; 20 times the min ratio seems to work for all sizes. $rad = (20*$ratio); // override the radius with user-spec'd value $rad = isset($_GET['rad'])&&$_GET['rad']!=""?$_GET['rad']:(isset($_GET['radius'])&&$_GET['radius']!=""?$_GET['radius']:$rad); // beyond $max/2, the radius tends to lose the desired effect $rad = min($rad, $max/2); // create the base image $img=imagecreatetruecolor($newwidth,$newheight); // create properly scaled copy of image $img2=imagecreatetruecolor(ceil(imagesx($src)*$ratio),ceil(imagesy($src)*$ratio)); imagecopyresampled($img2,$src, 0,0, 0,0, imagesx($img2),imagesy($img2), imagesx($src),imagesy($src)); imagedestroy($src); // copy base imagecopy($img,$img2, 0,0, -((imagesx($img)-imagesx($img2))/2),-((imagesy($img)-imagesy($img2))/2), imagesx($img2),imagesy($img2)); imagedestroy($img2); /* Rounded corners method: Create a template image, fill with one color (magenta) create a rounded-corner rect in another color (red) that is allocated as transparent. Save the alpha channels of the template, creating a transparent rounded rect cutout from a full-sized magenta rect. Copy the template to the base image (the scaled image will show through the rounded corner rect) and specify the first color (magenta) as a transparent color on the base img. Final result is an image with rounded corners, where the cutoff area of the corners are transparent. */ $tmp = imagecreatetruecolor($newwidth,$newheight); $x = $y = 0; $cx = $newwidth; $cy = $newheight; $dia = $rad*2; $trans = imagecolorallocatealpha($img, 255,0,0,127); $magenta = imagecolorallocate($img, 255,0,255); // set alpha reqs on the images imagealphablending($tmp, false); imagesavealpha($tmp, true); imagealphablending($img, true); // transparent bg imagefilledrectangle($tmp,$x,$y,$cx,$cy,$magenta); // rounded corners - two rects, four circles // left to right rect imagefilledrectangle($tmp,$x,$y+$rad,$cx,$cy-$rad,$trans); // top to bottom rect imagefilledrectangle($tmp,$x+$rad,$y,$cx-$rad,$cy,$trans); // top left corner imagefilledellipse($tmp, $x+$rad, $y+$rad, $rad*2, $dia, $trans); // top right corner imagefilledellipse($tmp, $cx-$rad, $y+$rad, $rad*2, $dia, $trans); // bottom left corner imagefilledellipse($tmp, $x+$rad, $cy-$rad, $rad*2, $dia, $trans); // bottom right corner imagefilledellipse($tmp, $cx-$rad, $cy-$rad, $rad*2, $dia, $trans); // use $tmp cookie-cutter transparent png to cut rounded corners to $img imagecopy($img, $tmp, 0,0, 0,0, $cx,$cy); imagedestroy($tmp); // set the transparent color on the image imagecolortransparent($img, $magenta); $fmt = isset($_GET['fmt'])&&$_GET['fmt']=='gif'?'gif':isset($_GET['format'])&&$_GET['format']=='gif'?'gif':'png'; // output the image and cleanup if ($fmt == "gif") { header("Content-Type: image/gif"); imagegif($img, NULL); } else { header("Content-Type: image/png"); imagepng($img, NULL, 9); } imagedestroy($img); ?>