PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/vendors/adapters/ffmpeg-php/ffmpeg_animated_gif.php

https://github.com/mariuz/firetube
PHP | 237 lines | 96 code | 7 blank | 134 comment | 12 complexity | dd80e6c37c7346a601c2de02ef11df43 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * This is a pure php emulation of the PHP module FFmpeg-PHP.
  4. * There is one extra function here. ffmpeg_animated_gif::saveNow();
  5. * @author Oliver Lillie (aka buggedcom) <publicmail@buggedcom.co.uk>
  6. * @package PHPVideoToolkit
  7. * @license BSD
  8. * @copyright Copyright (c) 2008 Oliver Lillie <http://www.buggedcom.co.uk>
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
  10. * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
  11. * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
  12. * is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  16. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  18. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. * @uses GifEncoder
  20. * - @link http://www.phpclasses.org/browse/package/3163.html
  21. * - @link http://phpclasses.gifs.hu/show.php?src=GIFEncoder.class.php
  22. * - @author Lรกszlรณ Zsidi
  23. * - @license Freeware.
  24. * @see ffmpeg-php,
  25. * - @link http://ffmpeg-php.sourceforge.net/
  26. * - @author Todd Kirby.
  27. * - all phpdoc documentation is lifted directly from the ffmpeg-php docs
  28. */
  29. class ffmpeg_animated_gif
  30. {
  31. private $_frames = array();
  32. private $_width = null;
  33. private $_height = null;
  34. private $_frame_rate = null;
  35. private $_loop_count = null;
  36. private $_output_file_path = null;
  37. private $_toolkit_class = null;
  38. private $_unlink_files = array();
  39. private $_saved = false;
  40. /**
  41. * Class Constructor
  42. * Create a new PHPVideoToolkit_animated_gif object.
  43. * @param resource $output_file_path Location in the filesystem where the animated gif will be written.
  44. * @param resource $width Width of the animated gif.
  45. * @param resource $height Height of the animated gif.
  46. * @param resource $frame_rate Frame rate of the animated gif in frames per second.
  47. * @param resource $loop_count Number of times to loop the animation. Put a zero here to loop forever or omit this parameter to disable looping.
  48. */
  49. function __construct($output_file_path, $width, $height, $frame_rate, $loop_count=false)
  50. {
  51. $this->_output_file_path = $output_file_path;
  52. $this->_width = $width;
  53. $this->_height = $height;
  54. $this->_frame_rate = $frame_rate;
  55. $this->_loop_count = $loop_count;
  56. }
  57. /**
  58. * Class Destructor
  59. * saves the file
  60. */
  61. function __destruct()
  62. {
  63. // save the output
  64. $this->saveNow();
  65. // destroy all image resources
  66. if(count($this->_frames))
  67. {
  68. foreach($this->_frames as $key=>$frame)
  69. {
  70. imagedestroy($frame->toGDImage());
  71. }
  72. }
  73. // loop through the temp files to remove
  74. if(!empty($this->_unlink_files))
  75. {
  76. foreach ($this->_unlink_files as $key=>$file)
  77. {
  78. @unlink($file);
  79. }
  80. $this->_unlink_files = array();
  81. }
  82. }
  83. /**
  84. * This function IS NOT PROVIDED IN FFMPEG-PHP as it creates the gif as frames are added. to save memory in
  85. * php and practicality purposes this isn't really possible. It will overwrite any file.
  86. * @access public
  87. * @uses GifEncoder
  88. * - @link http://www.phpclasses.org/browse/package/3163.html
  89. * - @link http://phpclasses.gifs.hu/show.php?src=GIFEncoder.class.php
  90. * - @author Lรกszlรณ Zsidi
  91. * - @license Freeware.
  92. * @param string $tmp_directory The temp directory to work with. (remember the trailing slash)
  93. * @return boolean
  94. */
  95. public function saveNow($tmp_directory='/tmp/')
  96. {
  97. if($this->_saved === false)
  98. {
  99. $this->_saved = true;
  100. // check there are frames to make a gif
  101. if(!count($this->_frames))
  102. {
  103. return false;
  104. }
  105. if(!class_exists('GIFEncoder'))
  106. {
  107. require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'gifencoder'.DIRECTORY_SEPARATOR.'GIFEncoder.class.phpvideotoolkit.php';
  108. }
  109. // save all the images from the ffmpeg_frames
  110. $files = array();
  111. $delays = array();
  112. $delay = (1/$this->_frame_rate)*100;
  113. foreach($this->_frames as $key=>$frame)
  114. {
  115. $file = $tmp_directory.'fag-'.uniqid(time().'-').'.gif';
  116. if(!imagegif($frame->toGDImage(), $file))
  117. {
  118. return false;
  119. }
  120. // add file to array so it out deletes on close
  121. array_push($this->_unlink_files, $file);
  122. array_push($files, $file);
  123. array_push($delays, $delay);
  124. }
  125. // convert the images
  126. $gif = new GIFEncoder($files, $delays, $this->_loop_count, 2, 0, 0, 0, 0, 'url');
  127. $gif_data = $gif->GetAnimation();
  128. if(!$gif_data)
  129. {
  130. return false;
  131. }
  132. // write the gif
  133. if (!$handle = fopen($this->_output_file_path, 'w'))
  134. {
  135. return false;
  136. }
  137. if (fwrite($handle, $gif_data) === false)
  138. {
  139. return false;
  140. }
  141. fclose($handle);
  142. return true;
  143. }
  144. return false;
  145. }
  146. // NOTE this provides a way to do it through pure PHPVideoToolkit class, however when ffmpeg creates animated gifs it uses a
  147. // limited colour palette for some stupid reason so the gifs created look rubbish.
  148. // public function saveNow($tmp_directory='/tmp/')
  149. // {
  150. // if($this->_saved === false)
  151. // {
  152. // $this->_saved = true;
  153. // // check there are frames to make a gif
  154. // if(!count($this->_frames))
  155. // {
  156. // return false;
  157. // }
  158. // if($this->_toolkit !== null)
  159. // {
  160. // $this->_toolkit->reset();
  161. // }
  162. // else
  163. // {
  164. // // get the ffmpeg class
  165. // require_once dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR.'phpvideotoolkit.php5.php';
  166. // $this->_toolkit = new PHPVideoToolkit($tmp_directory);
  167. // }
  168. // // save all the images from the ffmpeg_frames
  169. // $files = array();
  170. // foreach($this->_frames as $key=>$frame)
  171. // {
  172. // $file = $tmp_directory.'fag-'.$this->_toolkit->unique().'.jpg';
  173. // if(!imagejpeg($frame->toGDImage(), $file, 80))
  174. // {
  175. // return false;
  176. // }
  177. // // add file to array so it out deletes on close
  178. // array_push($this->_unlink_files, $file);
  179. // array_push($files, $file);
  180. // }
  181. // // print_r($files);
  182. // // prepare these images for conversion into a movie/gif
  183. // $result = $this->_toolkit->prepareImagesForConversionToVideo($files);
  184. // if(!$result)
  185. // {
  186. // return false;
  187. // }
  188. // // set the width and height
  189. // $this->_toolkit->setVideoOutputDimensions($this->_width, $this->_height);
  190. // // set the frame rate
  191. // $this->_toolkit->addCommand('-inputr', $this->_frame_rate);
  192. // // set the looping
  193. // $this->_toolkit->setGifLoops($this->_loop_count);
  194. // // set the output parameters
  195. // $output_path_info = pathinfo($this->_output_file_path);
  196. // $result = $this->_toolkit->setOutput($output_path_info['dirname'].'/', $output_path_info['basename'], PHPVideoToolkit::OVERWRITE_EXISTING);
  197. // if(!$result)
  198. // {
  199. // return false;
  200. // }
  201. // // execute the ffmpeg command
  202. // $result = $this->_toolkit->execute();
  203. // print_r($this->_toolkit->getLastCommand());
  204. // if(!$result)
  205. // {
  206. // return false;
  207. // }
  208. // return true;
  209. // }
  210. // return false;
  211. // }
  212. /**
  213. * Add a frame to the end of the animated gif.
  214. * @access public
  215. * @param resource $output_file_path The ffmpeg_frame object to add to the end of the animated gif.
  216. * @param boolean $save If true the gif will save after the frame has been added.
  217. * NOTE: this param does not feature in the actuall ffmpeg-php module.
  218. * @return boolean
  219. */
  220. public function addFrame($frame_to_add, $save=false)
  221. {
  222. if(get_class($frame_to_add) == 'ffmpeg_frame' && $frame_to_add->hasValidResource())
  223. {
  224. array_push($this->_frames, $frame_to_add);
  225. $this->_saved = false;
  226. return $save === true ? $this->saveNow() : true;
  227. }
  228. return false;
  229. }
  230. }