Advertisement
Guest User

Untitled

a guest
Feb 28th, 2016
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. /**
  2. * Image stitching function.
  3. *
  4. * Now operates with images of varying heights as well as widths.
  5. *
  6. * @param array $files
  7. * An array of files. Each element is a path to the file in question.
  8. *
  9. * @param int $rows
  10. * The number of rows the end resulting image will have. The images
  11. * will be added to the new image in the order of the array divided
  12. * equally in number to the rows specified here.
  13. *
  14. * @param int $action
  15. * An integer (or static define) of the action to take on the resulting
  16. * image.
  17. * IMAGE_STITCH_DISPLAY - Display the item (default action).
  18. * IMAGE_STITCH_SAVE - Save the image to the file system (path required).
  19. * IMAGE_STITCH_RETURN - Return the resulting file pointer to the calling
  20. * function for processing there.
  21. *
  22. * @param string $path
  23. * The path of where to save the resulting new image.
  24. *
  25. * @return image $image
  26. * The image data that can have whatever done to it.
  27. */
  28. function image_stitch($files, $rows = 2, $action = IMAGE_STITCH_DISPLAY, $path = NULL) {
  29. foreach($files as $file) {
  30. $path = explode('.', $file);
  31. if ($path[count($path)-1] == 'png') {
  32. $images[] = imagecreatefrompng($file);
  33. }
  34. else {
  35. $images[] = imagecreatefromjpeg($file);
  36. }
  37. }
  38. $number_of_images = count($images);
  39. $number_of_columns = ($number_of_images / $rows) - 1;
  40. $total_width = 0;
  41. $max_width = 0;
  42. $total_heights = array();
  43. $widths = array(array());
  44. $grid = array(array());
  45. for ($y = 1; $y <= $rows; $y++) {
  46. $this_height = $this_width = 0;
  47. for ($x = 0; $x <= $number_of_columns; $x++) {
  48. if (empty($files[(( $y + ($x - 1)) + ($number_of_columns * ($y - 1)))])) {
  49. next;
  50. }
  51. $image_size = getimagesize($files[(( $y + ($x - 1)) + ($number_of_columns * ($y - 1)))]);
  52. $grid[$x][$y] = $images[(( $y + ($x - 1)) + ($number_of_columns * ($y - 1)))];
  53. $width = $image_size[0];
  54. $height = $image_size[1];
  55. $widths[$x][$y][] = $width;
  56.  
  57. $this_width += $width;
  58. if ($height > $this_height) {
  59. $this_height = $height;
  60. }
  61.  
  62. if ($x == 0 && $y > 1) {
  63. $total_heights[] = $this_height;
  64. if ($max_width < $this_width) {
  65. $max_width = $this_width;
  66. }
  67. }
  68. }
  69. }
  70.  
  71. $total_heights[] = $this_height;
  72. if ($max_width < $this_width) {
  73. $max_width = $this_width;
  74. }
  75.  
  76. $destination_image = imagecreatetruecolor($max_width, array_sum($total_heights));
  77. $black = imagecolorallocate($destination_image, 0, 0, 0);
  78. imagecolortransparent($destination_image, $black);
  79. imagealphablending($destination_image, FALSE);
  80. imagesavealpha($destination_image, TRUE);
  81.  
  82. // place our images
  83. foreach($grid as $instance_key => $instance) {
  84. $height = $total_heights[$instance_key];
  85. foreach($instance as $reference_key => $reference) {
  86. imagecopyresampled($destination_image, $reference, $instance_key * 180, ($reference_key - 1) * 180, 0, 0, 180, 180, 180, 180);
  87. }
  88. }
  89.  
  90. // Display the image if directed
  91. if ($action = IMAGE_STITCH_DISPLAY) {
  92. header('content-type: image/png');
  93. imagepng($destination_image);
  94. imagedestroy($destination_image);
  95. exit();
  96. }
  97.  
  98. // Return the image if directed.
  99. if ($action == IMAGE_STITCH_RETURN) {
  100. return $destination_image;
  101. }
  102.  
  103. // If we are saving the image, save it with no compression or filters.
  104. if (!$empty($path)) {
  105. imagepng($destination_image, $path, 0, PNG_NO_FILTER);
  106. }
  107.  
  108. return TRUE;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement