Element
06-01-2011, 04:03 AM
I'm working on little app to automatically make award images for users. But first, I need to get the basic template down.
This is alter of "Apply Round Corner PHP-GD" and adds a cropping feature (as it needs to be a thumbnail of the original)
But I want the crop to be the same resolution as the image. Unfortunately when I use the $source_width, and $source_height, the image is extremely blown up. So I divided that by half, and got what appears to be the resolution, but with cose inspection, it's still a bit big, causing blurring.
The second, MAIN problem I just found, is if the image height is only lets say 142... The cropped thumb has a blank black space at the bottom where it isn't spanning to, and I can't for the life of me make the image move down, just make the blank space bigger.
Please, help.
<?php
//----------------------------------------------------------------
// Apply Round Corner PHP-GD
// Revision 2 [2009-07-01]
// Corrected inconsistent corner problem caused by PHP bug 42685
//----------------------------------------------------------------
//
// source: path or url of a gif/jpeg/png image -- php fopen url wrapper must be enabled if using url
// radius: corner radius in pixels -- default value is 10
// colour: corner colour in rgb hex format -- default value is FFFFFF
//
$source = $_GET['src'];
$radius = isset( $_GET[ "radius" ] ) ? $_GET[ "radius" ] : 10;
$colour = isset( $_GET[ "colour" ] ) ? $_GET[ "colour" ] : "FFFFFF";
$cropWidth = 191;
$cropHeight = 81;
//
// 1) open source image and calculate properties
//
list( $source_width, $source_height, $source_type ) = getimagesize( $source );
switch ( $source_type )
{
case IMAGETYPE_GIF:
$source_image = imagecreatefromgif( $source );
break;
case IMAGETYPE_JPEG:
$source_image = imagecreatefromjpeg( $source );
break;
case IMAGETYPE_PNG:
$source_image = imagecreatefrompng( $source );
break;
}
//getting the top left coordinate
$c1 = array( "x" => round( ( $source_width - $cropWidth ) / 2 - 20 ), "y" => round( ( $source_height - $cropHeight ) / 2 ) );
//echo "<p>" . $c1['x'] . "x " . $c1['x'] . "y</p>" .
//"<p>" . $source_width . "x " . $source_height . "y</p>";
$thumb = imagecreatetruecolor( $cropWidth, $cropHeight );
imagecopyresampled( $thumb, $source_image, 0, 0, $c1['x'], $c1['y'], $source_width / 2, $source_height / 2, $cropWidth, $cropHeight );
$source_width = $cropWidth;
$source_height = $cropHeight;
//
// 2) create mask for top-left corner in memory
//
$corner_image = imagecreatetruecolor( $radius, $radius );
$clear_colour = imagecolorallocate( $corner_image, 0, 0, 0 );
$solid_colour = imagecolorallocate( $corner_image, hexdec( substr( $colour, 0, 2 ) ), hexdec( substr( $colour, 2, 2 ) ),hexdec( substr( $colour, 4, 2 ) ) );
imagecolortransparent( $corner_image, $clear_colour );
imagefill( $corner_image, 0, 0, $solid_colour );
imagefilledellipse( $corner_image, $radius, $radius, ( $radius * 2 ), ( $radius * 2 ), $clear_colour );
//
// 3) render the top-left, bottom-left, bottom-right, top-right corners by rotating and copying the mask
//
imagecopymerge( $thumb, $corner_image, 0, 0, 0, 0, $radius, $radius, 100 );
$corner_image = imagerotate( $corner_image, 90, 0 );
imagecopymerge( $thumb, $corner_image, 0, ( $source_height - $radius ), 0, 0, $radius, $radius, 100 );
$corner_image = imagerotate( $corner_image, 90, 0 );
imagecopymerge( $thumb, $corner_image, ( $source_width - $radius ), ( $source_height - $radius ), 0, 0, $radius, $radius, 100 );
$corner_image = imagerotate( $corner_image, 90, 0 );
imagecopymerge( $thumb, $corner_image, ( $source_width - $radius ), 0, 0, 0, $radius, $radius, 100 );
//
// 4) output the image -- revise this step according to your needs
//
header( "Content-type: image/png" );
imagepng( $thumb );
?>
The code in question is this block:
//getting the top left coordinate
$c1 = array( "x" => round( ( $source_width - $cropWidth ) / 2 - 20 ), "y" => round( ( $source_height - $cropHeight ) / 2 ) );
//echo "<p>" . $c1['x'] . "x " . $c1['x'] . "y</p>" .
//"<p>" . $source_width . "x " . $source_height . "y</p>";
$thumb = imagecreatetruecolor( $cropWidth, $cropHeight );
imagecopyresampled( $thumb, $source_image, 0, 0, $c1['x'], $c1['y'], $source_width / 2, $source_height / 2, $cropWidth, $cropHeight );
$source_width = $cropWidth;
$source_height = $cropHeight;
This is alter of "Apply Round Corner PHP-GD" and adds a cropping feature (as it needs to be a thumbnail of the original)
But I want the crop to be the same resolution as the image. Unfortunately when I use the $source_width, and $source_height, the image is extremely blown up. So I divided that by half, and got what appears to be the resolution, but with cose inspection, it's still a bit big, causing blurring.
The second, MAIN problem I just found, is if the image height is only lets say 142... The cropped thumb has a blank black space at the bottom where it isn't spanning to, and I can't for the life of me make the image move down, just make the blank space bigger.
Please, help.
<?php
//----------------------------------------------------------------
// Apply Round Corner PHP-GD
// Revision 2 [2009-07-01]
// Corrected inconsistent corner problem caused by PHP bug 42685
//----------------------------------------------------------------
//
// source: path or url of a gif/jpeg/png image -- php fopen url wrapper must be enabled if using url
// radius: corner radius in pixels -- default value is 10
// colour: corner colour in rgb hex format -- default value is FFFFFF
//
$source = $_GET['src'];
$radius = isset( $_GET[ "radius" ] ) ? $_GET[ "radius" ] : 10;
$colour = isset( $_GET[ "colour" ] ) ? $_GET[ "colour" ] : "FFFFFF";
$cropWidth = 191;
$cropHeight = 81;
//
// 1) open source image and calculate properties
//
list( $source_width, $source_height, $source_type ) = getimagesize( $source );
switch ( $source_type )
{
case IMAGETYPE_GIF:
$source_image = imagecreatefromgif( $source );
break;
case IMAGETYPE_JPEG:
$source_image = imagecreatefromjpeg( $source );
break;
case IMAGETYPE_PNG:
$source_image = imagecreatefrompng( $source );
break;
}
//getting the top left coordinate
$c1 = array( "x" => round( ( $source_width - $cropWidth ) / 2 - 20 ), "y" => round( ( $source_height - $cropHeight ) / 2 ) );
//echo "<p>" . $c1['x'] . "x " . $c1['x'] . "y</p>" .
//"<p>" . $source_width . "x " . $source_height . "y</p>";
$thumb = imagecreatetruecolor( $cropWidth, $cropHeight );
imagecopyresampled( $thumb, $source_image, 0, 0, $c1['x'], $c1['y'], $source_width / 2, $source_height / 2, $cropWidth, $cropHeight );
$source_width = $cropWidth;
$source_height = $cropHeight;
//
// 2) create mask for top-left corner in memory
//
$corner_image = imagecreatetruecolor( $radius, $radius );
$clear_colour = imagecolorallocate( $corner_image, 0, 0, 0 );
$solid_colour = imagecolorallocate( $corner_image, hexdec( substr( $colour, 0, 2 ) ), hexdec( substr( $colour, 2, 2 ) ),hexdec( substr( $colour, 4, 2 ) ) );
imagecolortransparent( $corner_image, $clear_colour );
imagefill( $corner_image, 0, 0, $solid_colour );
imagefilledellipse( $corner_image, $radius, $radius, ( $radius * 2 ), ( $radius * 2 ), $clear_colour );
//
// 3) render the top-left, bottom-left, bottom-right, top-right corners by rotating and copying the mask
//
imagecopymerge( $thumb, $corner_image, 0, 0, 0, 0, $radius, $radius, 100 );
$corner_image = imagerotate( $corner_image, 90, 0 );
imagecopymerge( $thumb, $corner_image, 0, ( $source_height - $radius ), 0, 0, $radius, $radius, 100 );
$corner_image = imagerotate( $corner_image, 90, 0 );
imagecopymerge( $thumb, $corner_image, ( $source_width - $radius ), ( $source_height - $radius ), 0, 0, $radius, $radius, 100 );
$corner_image = imagerotate( $corner_image, 90, 0 );
imagecopymerge( $thumb, $corner_image, ( $source_width - $radius ), 0, 0, 0, $radius, $radius, 100 );
//
// 4) output the image -- revise this step according to your needs
//
header( "Content-type: image/png" );
imagepng( $thumb );
?>
The code in question is this block:
//getting the top left coordinate
$c1 = array( "x" => round( ( $source_width - $cropWidth ) / 2 - 20 ), "y" => round( ( $source_height - $cropHeight ) / 2 ) );
//echo "<p>" . $c1['x'] . "x " . $c1['x'] . "y</p>" .
//"<p>" . $source_width . "x " . $source_height . "y</p>";
$thumb = imagecreatetruecolor( $cropWidth, $cropHeight );
imagecopyresampled( $thumb, $source_image, 0, 0, $c1['x'], $c1['y'], $source_width / 2, $source_height / 2, $cropWidth, $cropHeight );
$source_width = $cropWidth;
$source_height = $cropHeight;