Photoshop Like "Offset" Function


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. function image_offset_x($image, $offset){
  2. $width = imagesx( $image );
  3. $height = imagesy( $image );
  4.  
  5. // Values too large need to be modded.
  6. // (this is my first use of %mod that doesn't have to do with alternating table rows.)
  7. if( abs($offset) > $width){
  8. $offset = $offset % $width;
  9. }
  10. // Negative values need to be handled differently.
  11. if($offset < 0){
  12. $offset = $width + $offset;
  13. }
  14. // Do nothing for zero offset values.
  15. if($offset == 0){
  16. return $image;
  17. }
  18.  
  19. $new_image = imagecreatetruecolor($width, $height);
  20.  
  21. $chunk1_w = $offset;
  22. $chunk1_h = $height;
  23. $chunk1_xs = $width - $offset;
  24. $chunk1_ys = 0;
  25. $chunk1_xd = 0;
  26. $chunk1_yd = 0;
  27.  
  28. $chunk2_w = $width - $offset;
  29. $chunk2_h = $height;
  30. $chunk2_xs = 0;
  31. $chunk2_ys = 0;
  32. $chunk2_xd = $offset;
  33. $chunk2_yd = 0;
  34.  
  35. // Copy first chunk
  36. imagecopy($new_image,$image,$chunk1_xd,$chunk1_yd, $chunk1_xs,$chunk1_ys,$chunk1_w,$chunk1_h);
  37.  
  38. // Copy second chunk
  39. imagecopy($new_image,$image,$chunk2_xd,$chunk2_yd, $chunk2_xs,$chunk2_ys,$chunk2_w,$chunk2_h);
  40.  
  41. return $new_image;
  42. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.