PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/GifCreator.php

https://gitlab.com/billyprice1/mc-skintools
PHP | 349 lines | 172 code | 88 blank | 89 comment | 44 complexity | 3a6bdd840d15277b4f867c656d1a99c4 MD5 | raw file
  1. <?php
  2. namespace GifCreator;
  3. /**
  4. * Create an animated GIF from multiple images
  5. * @version 1.0
  6. * @link https://github.com/Sybio/GifCreator
  7. * @author Sybio (Clément Guillemain / @Sybio01)
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @copyright Clément Guillemain
  10. * @modified by Max Korlaar
  11. */
  12. class GifCreator {
  13. /**
  14. * @var string The gif string source (old: this->GIF)
  15. */
  16. private $gif;
  17. /**
  18. * @var string Encoder version (old: this->VER)
  19. */
  20. private $version;
  21. /**
  22. * @var boolean Check the image is build or not (old: this->IMG)
  23. */
  24. private $imgBuilt;
  25. /**
  26. * @var array Frames string sources (old: this->BUF)
  27. */
  28. private $frameSources;
  29. /**
  30. * @var integer Gif loop (old: this->LOP)
  31. */
  32. private $loop;
  33. /**
  34. * @var integer Gif dis (old: this->DIS)
  35. */
  36. private $dis;
  37. /**
  38. * @var integer Gif color (old: this->COL)
  39. */
  40. private $colour;
  41. /**
  42. * @var array (old: this->ERR)
  43. */
  44. private $errors;
  45. // Methods
  46. // ===================================================================================
  47. /**
  48. * Constructor
  49. */
  50. public function __construct() {
  51. $this->reset();
  52. // Static data
  53. $this->version = 'GifCreator: Under development';
  54. $this->errors = array(
  55. 'ERR00' => 'Does not supported function for only one image.',
  56. 'ERR01' => 'Source is not a GIF image.',
  57. 'ERR02' => 'You have to give resource image variables, image URL or image binary sources in $frames array.',
  58. 'ERR03' => 'Does not make animation from animated GIF source.',
  59. );
  60. }
  61. /**
  62. * Create the GIF string (old: GIFEncoder)
  63. *
  64. * @param array $frames An array of frame: can be file paths, resource image variables, binary sources or image URLs
  65. * @param array $durations An array containing the duration of each frame
  66. * @param integer $loop Number of GIF loops before stopping animation (Set 0 to get an infinite loop)
  67. *
  68. * @throws \Exception
  69. * @return string The GIF string source
  70. */
  71. public function create($frames = array(), $durations = array(), $loop = 0) {
  72. if (!is_array($frames) && !is_array($durations)) {
  73. throw new \Exception($this->version . ': ' . $this->errors['ERR00']);
  74. }
  75. $this->loop = ($loop > -1) ? $loop : 0;
  76. $this->dis = 2;
  77. for ($i = 0; $i < count($frames); $i++) {
  78. if (is_resource($frames[$i])) { // Resource var
  79. $resourceImg = $frames[$i];
  80. ob_start();
  81. imagegif($frames[$i]);
  82. $this->frameSources[] = ob_get_contents();
  83. ob_end_clean();
  84. } elseif (is_string($frames[$i])) { // File path or URL or Binary source code
  85. if (file_exists($frames[$i]) || filter_var($frames[$i], FILTER_VALIDATE_URL)) { // File path
  86. $frames[$i] = file_get_contents($frames[$i]);
  87. }
  88. $resourceImg = imagecreatefromstring($frames[$i]);
  89. ob_start();
  90. imagegif($resourceImg);
  91. $this->frameSources[] = ob_get_contents();
  92. ob_end_clean();
  93. } else { // Fail
  94. throw new \Exception($this->version . ': ' . $this->errors['ERR02']);
  95. }
  96. if ($i == 0) {
  97. $colour = imagecolortransparent($resourceImg);
  98. }
  99. if (substr($this->frameSources[$i], 0, 6) != 'GIF87a' && substr($this->frameSources[$i], 0, 6) != 'GIF89a') {
  100. throw new \Exception($this->version . ': ' . $i . ' ' . $this->errors['ERR01']);
  101. }
  102. for ($j = (13 + 3 * (2 << (ord($this->frameSources[$i]{10}) & 0x07))), $k = true; $k; $j++) {
  103. switch ($this->frameSources[$i]{$j}) {
  104. case '!':
  105. if ((substr($this->frameSources[$i], ($j + 3), 8)) == 'NETSCAPE') {
  106. throw new \Exception($this->version . ': ' . $this->errors['ERR03'] . ' (' . ($i + 1) . ' source).');
  107. }
  108. break;
  109. case ';':
  110. $k = false;
  111. break;
  112. }
  113. }
  114. unset($resourceImg);
  115. }
  116. if (isset($colour)) {
  117. $this->colour = $colour;
  118. } else {
  119. $red = $green = $blue = 0;
  120. $this->colour = ($red > -1 && $green > -1 && $blue > -1) ? ($red | ($green << 8) | ($blue << 16)) : -1;
  121. }
  122. $this->gifAddHeader();
  123. for ($i = 0; $i < count($this->frameSources); $i++) {
  124. $this->addGifFrames($i, $durations[$i]);
  125. }
  126. $this->gifAddFooter();
  127. return $this->gif;
  128. }
  129. // Internals
  130. // ===================================================================================
  131. /**
  132. * Add the header gif string in its source (old: GIFAddHeader)
  133. */
  134. public function gifAddHeader() {
  135. $cmap = 0;
  136. if (ord($this->frameSources[0]{10}) & 0x80) {
  137. $cmap = 3 * (2 << (ord($this->frameSources[0]{10}) & 0x07));
  138. $this->gif .= substr($this->frameSources[0], 6, 7);
  139. $this->gif .= substr($this->frameSources[0], 13, $cmap);
  140. $this->gif .= "!\377\13NETSCAPE2.0\3\1" . $this->encodeAsciiToChar($this->loop) . "\0";
  141. }
  142. }
  143. /**
  144. * Add the frame sources to the GIF string (old: GIFAddFrames)
  145. *
  146. * @param integer $i
  147. * @param integer $d
  148. */
  149. public function addGifFrames($i, $d) {
  150. $Locals_str = 13 + 3 * (2 << (ord($this->frameSources[$i]{10}) & 0x07));
  151. $Locals_end = strlen($this->frameSources[$i]) - $Locals_str - 1;
  152. $Locals_tmp = substr($this->frameSources[$i], $Locals_str, $Locals_end);
  153. $Global_len = 2 << (ord($this->frameSources[0]{10}) & 0x07);
  154. $Locals_len = 2 << (ord($this->frameSources[$i]{10}) & 0x07);
  155. $Global_rgb = substr($this->frameSources[0], 13, 3 * (2 << (ord($this->frameSources[0]{10}) & 0x07)));
  156. $Locals_rgb = substr($this->frameSources[$i], 13, 3 * (2 << (ord($this->frameSources[$i]{10}) & 0x07)));
  157. $Locals_ext = "!\xF9\x04" . chr(($this->dis << 2) + 0) . chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . "\x0\x0";
  158. if ($this->colour > -1 && ord($this->frameSources[$i]{10}) & 0x80) {
  159. for ($j = 0; $j < (2 << (ord($this->frameSources[$i]{10}) & 0x07)); $j++) {
  160. if (ord($Locals_rgb{3 * $j + 0}) == (($this->colour >> 16) & 0xFF) &&
  161. ord($Locals_rgb{3 * $j + 1}) == (($this->colour >> 8) & 0xFF) &&
  162. ord($Locals_rgb{3 * $j + 2}) == (($this->colour >> 0) & 0xFF)
  163. ) {
  164. $Locals_ext = "!\xF9\x04" . chr(($this->dis << 2) + 1) . chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . chr($j) . "\x0";
  165. break;
  166. }
  167. }
  168. }
  169. switch ($Locals_tmp{0}) {
  170. case '!':
  171. $Locals_img = substr($Locals_tmp, 8, 10);
  172. $Locals_tmp = substr($Locals_tmp, 18, strlen($Locals_tmp) - 18);
  173. break;
  174. case ',':
  175. $Locals_img = substr($Locals_tmp, 0, 10);
  176. $Locals_tmp = substr($Locals_tmp, 10, strlen($Locals_tmp) - 10);
  177. break;
  178. }
  179. if (ord($this->frameSources[$i]{10}) & 0x80 && $this->imgBuilt) {
  180. if ($Global_len == $Locals_len) {
  181. if ($this->gifBlockCompare($Global_rgb, $Locals_rgb, $Global_len)) {
  182. $this->gif .= $Locals_ext . $Locals_img . $Locals_tmp;
  183. } else {
  184. $byte = ord($Locals_img{9});
  185. $byte |= 0x80;
  186. $byte &= 0xF8;
  187. $byte |= (ord($this->frameSources[0]{10}) & 0x07);
  188. $Locals_img{9} = chr($byte);
  189. $this->gif .= $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp;
  190. }
  191. } else {
  192. $byte = ord($Locals_img{9});
  193. $byte |= 0x80;
  194. $byte &= 0xF8;
  195. $byte |= (ord($this->frameSources[$i]{10}) & 0x07);
  196. $Locals_img{9} = chr($byte);
  197. $this->gif .= $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp;
  198. }
  199. } else {
  200. $this->gif .= $Locals_ext . $Locals_img . $Locals_tmp;
  201. }
  202. $this->imgBuilt = true;
  203. }
  204. /**
  205. * Add the gif string footer char (old: GIFAddFooter)
  206. */
  207. public function gifAddFooter() {
  208. $this->gif .= ';';
  209. }
  210. /**
  211. * Compare two block and return the version (old: GIFBlockCompare)
  212. *
  213. * @param string $globalBlock
  214. * @param string $localBlock
  215. * @param integer $length
  216. *
  217. * @return integer
  218. */
  219. public function gifBlockCompare($globalBlock, $localBlock, $length) {
  220. for ($i = 0; $i < $length; $i++) {
  221. if ($globalBlock{3 * $i + 0} != $localBlock{3 * $i + 0} ||
  222. $globalBlock{3 * $i + 1} != $localBlock{3 * $i + 1} ||
  223. $globalBlock{3 * $i + 2} != $localBlock{3 * $i + 2}
  224. ) {
  225. return 0;
  226. }
  227. }
  228. return 1;
  229. }
  230. /**
  231. * Encode an ASCII char into a string char (old: GIFWord)
  232. * $param integer $char ASCII char
  233. *
  234. * @param $char
  235. *
  236. * @return string
  237. */
  238. public function encodeAsciiToChar($char) {
  239. return (chr($char & 0xFF) . chr(($char >> 8) & 0xFF));
  240. }
  241. /**
  242. * Reset and clean the current object
  243. */
  244. public function reset() {
  245. $this->frameSources;
  246. $this->gif = 'GIF89a'; // the GIF header
  247. $this->imgBuilt = false;
  248. $this->loop = 0;
  249. $this->dis = 2;
  250. $this->colour = -1;
  251. }
  252. // Getter / Setter
  253. // ===================================================================================
  254. /**
  255. * Get the final GIF image string (old: GetAnimation)
  256. * @return string
  257. */
  258. public function getGif() {
  259. return $this->gif;
  260. }
  261. }