Scripts

function: imagereverse, imagenegative

Met deze functie kun je een plaatje dynamisch spiegelen, en we op deze manier:

function-imagereverse-imagenegative
<?php
function imagereverse( $path )
{
	# Read in image from $path
	if( strtolower( substr( $path, -3 ) ) == 'jpg' )
		$image = imagecreatefromjpeg( $path );
	elseif( strtolower( substr( $path, -3 ) ) == 'png' )
		$image = imagecreatefrompng( $path );
	else
		die( 'A non valid path has been given!' );
	
	# Get image dimensions
	$x = imagesx( $image );
	$y = imagesy( $image );
	
	# Create a new image
	$ip = imagecreatetruecolor( $x, $y );
	
	# Loop threw the whole height of the image
	for( $i = 0; $i < $y; $i++ )
	{
		# Loop threw the whole width of the image
		for( $j = 0; $j < $x; $j++ )
		{
			# Get the pixel color ( $x - 1 needs to be there cos of the imagestart @ 0,0 ;) )
			$c = imagecolorat( $image, ( $x - 1 ) - $j, $i );
			
			# Get the rgb values from color $c
			$r = ($c >> 16) & 0xFF;
			$g = ($c >> 8) & 0xFF;
			$b = $c & 0xFF;
			
			# Set the color
			$clr = imagecolorallocate( $ip, $r, $g, $b );
			
			# Set the pixel color in the new image
			imagesetpixel( $ip, $j, $i, $clr );
		}
	}
	
	# Output the reversed image
	header( 'Content-type: image/png' );
	imagepng( $ip );
	imagedestroy( $ip );
        imagedestroy( $image );
}

function imagenegative( $path )
{
	# Read in image from $path
	if( strtolower( substr( $path, -3 ) ) == 'jpg' )
		$image = imagecreatefromjpeg( $path );
	elseif( strtolower( substr( $path, -3 ) ) == 'png' )
		$image = imagecreatefrompng( $path );
	else
		die( 'A non valid path has been given!' );
	
	# Get image dimensions
	$x = imagesx( $image );
	$y = imagesy( $image );
	
	# Create a new image
	$ip = imagecreatetruecolor( $x, $y );
	
	# Loop threw the whole height of the image
	for( $i = 0; $i < $y; $i++ )
	{
		# Loop threw the whole width of the image
		for( $j = 0; $j < $x; $j++ )
		{
			# Get the pixel color
			$c = imagecolorat( $image, $j, $i );
			
			# Get the rgb values from color $c
			$r = ($c >> 16) & 0xFF;
			$g = ($c >> 8) & 0xFF;
			$b = $c & 0xFF;
			
			# Subtract 255 to get the opposite color
			$r = abs( $r - 255 );
			$g = abs( $g - 255 );
			$b = abs( $b - 255 );
			
			# Set the color
			$clr = imagecolorallocate( $ip, $r, $g, $b );
			
			# Set the pixel color in the new image
			imagesetpixel( $ip, $j, $i, $clr );
		}
	}
	
	# Output the reversed image
	header( 'Content-type: image/png' );
	imagepng( $ip );
	imagedestroy( $ip );
	imagedestroy( $image );
}
?>

Reacties

0
Nog geen reacties.