PHPverse 2025

Voting

: six plus three?
(Example: nine)

The Note You're Voting On

jbr at ya-right dot com
19 years ago
This is a niffty function that you can use to make transparent ellipse/round type cutouts of any PNG or GIF image. The hard part is finding what color to use for the cutout layer and then the transparent layer, because you don't want to set the transparent index to a color being used in the image. After that it's as simple as layering the two images together.

what you need for the example...

a image of the size you want the cutout to be, gif or png (true color /256) can be used!

<?

$original_image = './image.png';
$output_image = './new.png';
$temp_image = './temp'; // path and name (don't include the extension)
$is_true_color = true;

$ext = substr ( $original_image, strrpos ( $original_image, '.' ) );
$temp_image .= $ext;
$new = image_get ( $ext, $original_image );
$width = imagesx ( $new );
$height = imagesy ( $new );

// we need to create temp reduced image so we can get the colors
// in a high bit true color image (png 16,24 bit only)

if ( $is_true_color )
{
imagetruecolortopalette ( $new, false, 256 );
image_make ( $new, $ext, $temp_image );
imagedestroy ( $new );
$colors = get_rgb ( $temp_image, $ext );
@unlink ( $temp_image );
$new = image_get ( $ext, $original_image );
}
else
{
$colors = get_rgb ( $original_image, $ext );
}

// this creates the cutout layer (2 colors, both will become transparent)

$old = imagecreate ( $width, $height );
imageantialias( $old, true );
imagecolorallocate ( $old, $colors[0]['red'], $colors[0]['green'], $colors[0]['blue'] );
$bg = imagecolorallocate ( $old, $colors[1]['red'], $colors[1]['green'], $colors[1]['blue'] );
imagefilledellipse ( $old, floor ( $width / 2 ), floor ( $height / 2 ), $width, $height, $bg );
imagecolortransparent ( $old, $bg );
imagecopy ( $new, $old, 0, 0, 0, 0, $width, $height );
image_make ( $new, $ext, $output_image );
imagedestroy ( $old );
imagedestroy ( $new );

// this layers both images together, making a nice ellipse/round transparent image cutout

$old = imagecreate ( $width, $height );
$new = image_get ( $ext, $output_image );
$tbg = imagecolorallocate ( $old, $colors[0]['red'], $colors[0]['green'], $colors[0]['blue'] );
imagecopy ( $old, $new, 0, 0, 0, 0, $width, $height );
imagecolortransparent ( $old, $tbg );
image_make ( $old, $ext, $output_image );
imagedestroy ( $old );
imagedestroy ( $new );

/*
* shortcut functions (1,2)
*/

// returns the called image resource

function image_get ( $ext, $name )
{
switch ( $ext )
{
case '.gif' :
return ( imagecreatefromgif ( $name ) );
break;
case '.png' :
return ( imagecreatefrompng ( $name ) );
break;
}
}

// outputs the image named passed to it

function image_make ( $io, $ext, $name )
{
switch ( $ext )
{
case '.gif' :
imagegif ( $io, $name );
break;
case '.png' :
imagepng ( $io, $name );
break;
}
}

// get (2) colors not found in the current image

function get_rgb ( $image, $ext )
{
$x = 0;
$colors = array ();
$img = image_get ( $ext, $image );

for ( $color = 10; $color <= 250; $color++ )
{
if ( imagecolorexact ( $img, $color, $color, $color ) == -1 )
{
$colors[] = array ( 'red' => $color, 'green' => $color, 'blue' => $color );

if ( $x == 1 )
{
imagedestroy ( $img );
return ( $colors );
}

$x++;
}
}

imagedestory ( $img );
return ( $colors );
}

?>

You can try a demo here (SGML) capture a web page, then make multi cutouts example!

http://www.ya-right.com/

<< Back to user notes page

To Top