Return to Snippet

Revision: 34555
at October 25, 2010 07:17 by Jamie


Initial Code
function image_offset_x($image, $offset){
	$width = imagesx( $image );
	$height = imagesy( $image );
	
	// Values too large need to be modded.
	// (this is my first use of %mod that doesn't have to do with alternating table rows.)
	if( abs($offset) > $width){
		$offset = $offset % $width;
	}
	// Negative values need to be handled differently.
	if($offset < 0){
		$offset = $width + $offset;
	}
	// Do nothing for zero offset values.
	if($offset == 0){
		return $image;
	}
	
	$new_image = imagecreatetruecolor($width, $height);
	
	$chunk1_w = $offset;
	$chunk1_h = $height;
	$chunk1_xs = $width - $offset;
	$chunk1_ys = 0;
	$chunk1_xd = 0;
	$chunk1_yd = 0;
	
	$chunk2_w = $width - $offset;
	$chunk2_h = $height;
	$chunk2_xs = 0;
	$chunk2_ys = 0;
	$chunk2_xd = $offset;
	$chunk2_yd = 0;
	
	// Copy first chunk
	imagecopy($new_image,$image,$chunk1_xd,$chunk1_yd, $chunk1_xs,$chunk1_ys,$chunk1_w,$chunk1_h);
	
	// Copy second chunk
	imagecopy($new_image,$image,$chunk2_xd,$chunk2_yd, $chunk2_xs,$chunk2_ys,$chunk2_w,$chunk2_h);
	
	return $new_image;
}

Initial URL

                                

Initial Description
This code war written for a project I'm working on where a map of the world may need to be offset to center the view on specific areas. I'm also using it as a part of an iterative blur function to prevent seams from the blur operation.

I would like to eventually tweak the function to receive both X & Y values for the offset command, but am a tad lazy right now on this fine Sunday afternoon.

Initial Title
Photoshop Like "Offset" Function

Initial Tags
image, filter

Initial Language
PHP