Jump to content

'Image Overlay' script?


goghost

Recommended Posts

I've seen this in a few different places already, so I know it can be done, but not only am I confused as to HOW to do it, but I can't even think of what to call it to search for it :/

 

Basic idea - PHP places one image (defined by the user using a dropbox) in a 'field'. Based on more input by the user, it will place another image over it, creating a third image. It can layer endless amounts of images.

 

For example: Let's say this is one image: (x's are black pixels and -'s are white ones)

x---x-xx

--x-xx-x

-x-x-x-x

 

And that this is another one:

-xxx-x--

xx-x--x-

x-x-x-x-

 

And when you 'overlay' them you get:

 

xxxxxxxx

xxxxxxxx

xxxxxxxx

 

Or

--------

--------

--------

 

Depending on what you specify.

 

 

If you want working examples, ask me, I can get you some. If you know what I mean, though, please help!

 

Thanks,

Ghost //

Link to comment
https://forums.phpfreaks.com/topic/185709-image-overlay-script/
Share on other sites

If you want preserve transparent than you will have to read pixel by pixel if using the GD library. If you are using ImageMagick then that wouldn't be needed. Here is a quick GD example. It should be self explanatory. Example images also attached.

 

<?php

/* return image resource from a existing image file */

// $e = extension type ( 'gif', 'jpg', 'jpeg', 'png' );

// $p = path to image file ( 'x:/path/image.png' );

// return = resource

function imageFile ( $e, $p )
{
switch ( $e )
{
	case 'gif' :
	return imagecreatefromgif ( $p );
	break;
	case 'jpg' :
	case 'jpeg' :
	return imagecreatefromjpeg ( $p );
	break;
	case 'png' :
	return imagecreatefrompng ( $p );
	break;
}
}

/* return a (new) image resource */

// $s = size array returned from imageSize ();

// $t = true color image, default = true

// return = resource

function imageResource ( $s, $t = true )
{
switch ( $t )
{
	case false :
	return imagecreate ( $s['w'], $s['h'] );
	break;
	case true :
	return imagecreatetruecolor ( $s['w'], $s['h'] );
	break;
}
}

/* return a image resource size */

// $r = image resource

// return = size array ( 'w' => INT, 'h' => INT );

function imageSize ( $r )
{
return array ( 'w' => imagesx ( $r ), 'h' => imagesy ( $r ) );
}

/* save a image resource to a file */

function imageSave ( $e, $f, $r )
{
switch ( $e )
{
	case 'gif' :
	imagegif ( $r, $f );
	break;
	case 'jpg' :
	case 'jpeg' :
	imagejpeg ( $r, $f, 100 );
	break;
	case 'png' :
	imagepng ( $r, $f );
	break;
}
}

$background = true; // false would make overlay transparent

$extension  = 'jpg'; // out image file type

$bgc = array ( 0, 0, 64 ); // dark blue

// original image

$oi = 'f:/www/docs/0.png';

// mask image

$mi = 'f:/www/docs/1.png';

// final image

$fi = 'f:/www/docs/2.jpg';

// original image resource

$oir = imageFile ( 'png', $oi );

// mask image resource

$mir = imageFile ( 'png', $mi );

// original image size

$ois = imageSize ( $oir );

// mask image size

$mis = imageSize ( $mir );

// final image resource

$fir = imageResource ( $ois );

imagealphablending ( $fir, false );

imagesavealpha ( $fir, true );

imagecopyresampled ( $fir, $mir, 0, 0, 0, 0, $ois['w'], $ois['h'], $mis['w'], $mis['h'] );

imagedestroy ( $mir );

for ( $i = 0; $i < $ois['w']; $i++ )
{
for ( $j = 0; $j < $ois['h']; $j++ )
{
	if ( ( $alpha = ( imagecolorat ( $fir, $i, $j ) & 0x7F000000 ) >> 24 ) != 127 )
	{
		$b = imagecolorat ( $oir, $i, $j );

		imagesetpixel  ( $fir, $i, $j, imagecolorallocatealpha ( $fir, ( $b >> 16 ) & 0xFF, ( $b >> 8 ) & 0xFF, $b & 0xFF, $alpha ) );
	}
}
}

imagedestroy ( $oir );

if ( $background === true || $extension == 'jpg' )
{
$tir = imageResource ( $ois );

imagefilledrectangle ( $tir, 0, 0, $ois['w'], $ois['h'], imagecolorallocatealpha ( $tir, $bgc[0], $bgc[1], $bgc[2], 0 ) );

imagecopy ( $tir, $fir, 0, 0, 0, 0, $ois['w'], $ois['h'] );

imagedestroy ( $fir );

imageSave ( $extension, $fi, $tir );

imagedestroy ( $tir );
}
else
{
imageSave ( $extension, $fi, $fir );

imagedestroy ( $fir );
}

?>

 

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/185709-image-overlay-script/#findComment-980632
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.