<?PHP
/*****************************************************************
 *  IRRCOS
 *  Image Resize with Rounded Corners and Optional Squarification
 *  By Andrew Rowls
 *  http://techknack.net/rounded-corners-at-the-src
 *****************************************************************
 *  Usage: [All options other than "src" are optional]
 *
 *    irrcos.php?src=source_img[&option=value]...
 *
 *    Option        Value            Purpose
 *    src        path to image file    Specifies the image
 *                        to work with
 *
 *    square[d]    true|false        Specifies whether to
 *                        square or not
 *                        Default: true
 *
 *    height          number            Specify the maximum
 *                        image height
 *                        Default: source's height
 *
 *    width           number            Specify the maximum
 *                        image width
 *                        Default: source's width
 *
 *    rad|radius    number            Specify the curvature
 *                        of the corners.  "0" is
 *                        90 degree corners.
 *                        Default: 20 * $ratio (see below)
 *
 *    fmt|format    gif|png            Specify the output format.
 *                        PNG is recommended.  GIF is
 *                        for IE6 support.
 *                        Default: png
 *****************************************************************
 * Rounded Corners adapted from http://www.web-max.ca/PHP/misc_10.php
 *****************************************************************/

// get square val.  two possible params = "square" and "squared"
$square=isset($_GET['square'])&&$_GET['square']!="false"?true:(isset($_GET['squared'])&&$_GET['squared']!="false"?true:false);
// get the source.  no source?  Pfft, you think I can read your mind?  Exit or provide a default
$source=isset($_GET['src'])?$_GET['src']:exit;

list(
$width,$height,$type) = @getimagesize($source);
// eval: this way, we can dynamically get jpeg, png, and gif.
//    Presumably, won't work for .BMP files, as the function is "imagecreatefromwbmp"

eval("\$src = @imagecreatefrom".image_type_to_extension($typefalse)."(\$source);");
//$src = imagecreatefromjpeg($source);
if ($src === null || !isset($src) || $src === false) exit(1);

// maximum height and width
$maxheight = isset($_GET['height'])&&$_GET['height']!=""?$_GET['height']:$height;
$maxwidth = isset($_GET['width'])&&$_GET['width']!=""?$_GET['width']:$width;
// constrain maxes to actual size
$maxheight $maxheight>$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,$src0,00,0imagesx($img2),imagesy($img2), imagesx($src),imagesy($src));
imagedestroy($src);

// copy base
imagecopy($img,$img20,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($img255,0,0,127);
$magenta imagecolorallocate($img255,0,255);

// set alpha reqs on the images
imagealphablending($tmpfalse);
imagesavealpha($tmptrue);
imagealphablending($imgtrue);
// 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$tmp0,00,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($imgNULL);
}
else {
    
header("Content-Type: image/png");
    
imagepng($imgNULL9);
}

imagedestroy($img);

?>