PageRenderTime 53ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/phpvideotoolkit/adapters/ffmpeg-php/ffmpeg_animated_gif.php

http://phpvideotoolkit.googlecode.com/
PHP | 243 lines | 100 code | 9 blank | 134 comment | 13 complexity | d3c5e76d0b9564ad3143ca5d2b6957c3 MD5 | raw file
Possible License(s): GPL-2.0
  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. if(!defined('DS'))
  30. {
  31. define('DS', DIRECTORY_SEPARATOR);
  32. }
  33. class ffmpeg_animated_gif
  34. {
  35. private $_frames = array();
  36. private $_width = null;
  37. private $_height = null;
  38. private $_frame_rate = null;
  39. private $_loop_count = null;
  40. private $_output_file_path = null;
  41. private $_toolkit_class = null;
  42. private $_unlink_files = array();
  43. private $_saved = false;
  44. /**
  45. * Class Constructor
  46. * Create a new ffmpeg_animated_gif object.
  47. * @param resource $output_file_path Location in the filesystem where the animated gif will be written.
  48. * @param resource $width Width of the animated gif.
  49. * @param resource $height Height of the animated gif.
  50. * @param resource $frame_rate Frame rate of the animated gif in frames per second.
  51. * @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.
  52. */
  53. function __construct($output_file_path, $width, $height, $frame_rate, $loop_count=false)
  54. {
  55. $this->_output_file_path = $output_file_path;
  56. $this->_width = $width;
  57. $this->_height = $height;
  58. $this->_frame_rate = $frame_rate;
  59. $this->_loop_count = $loop_count;
  60. }
  61. /**
  62. * Class Destructor
  63. * saves the file
  64. */
  65. function __destruct()
  66. {
  67. // save the output
  68. $this->saveNow();
  69. // destroy all image resources
  70. if(count($this->_frames))
  71. {
  72. foreach($this->_frames as $key=>$frame)
  73. {
  74. imagedestroy($frame->toGDImage());
  75. }
  76. }
  77. // loop through the temp files to remove
  78. if(!empty($this->_unlink_files))
  79. {
  80. foreach ($this->_unlink_files as $key=>$file)
  81. {
  82. @unlink($file);
  83. }
  84. $this->_unlink_files = array();
  85. }
  86. }
  87. /**
  88. * This function IS NOT PROVIDED IN FFMPEG-PHP as it creates the gif as frames are added. to save memory in
  89. * php and practicality purposes this isn't really possible. It will overwrite any file.
  90. * @access public
  91. * @uses GifEncoder
  92. * - @link http://www.phpclasses.org/browse/package/3163.html
  93. * - @link http://phpclasses.gifs.hu/show.php?src=GIFEncoder.class.php
  94. * - @author L?szl? Zsidi
  95. * - @license Freeware.
  96. * @param string $tmp_directory The temp directory to work with. (remember the trailing slash)
  97. * @return boolean
  98. */
  99. public function saveNow($tmp_directory='/tmp/')
  100. {
  101. if($this->_saved === false)
  102. {
  103. $this->_saved = true;
  104. // check there are frames to make a gif
  105. if(!count($this->_frames))
  106. {
  107. return false;
  108. }
  109. if(!class_exists('GIFEncoder'))
  110. {
  111. require_once dirname(__FILE__).DS.'gifencoder'.DS.'GIFEncoder.class.phpvideotoolkit.php';
  112. }
  113. // save all the images from the ffmpeg_frames
  114. $files = array();
  115. $delays = array();
  116. $delay = (1/$this->_frame_rate)*100;
  117. foreach($this->_frames as $key=>$frame)
  118. {
  119. $file = $tmp_directory.'fag-'.uniqid(time().'-').'.gif';
  120. if(!imagegif($frame->toGDImage(), $file))
  121. {
  122. return false;
  123. }
  124. // add file to array so it out deletes on close
  125. array_push($this->_unlink_files, $file);
  126. array_push($files, $file);
  127. array_push($delays, $delay);
  128. }
  129. // convert the images
  130. $gif = new GIFEncoder($files, $delays, $this->_loop_count, 2, 0, 0, 0, 0, 'url');
  131. $gif_data = $gif->GetAnimation();
  132. if(!$gif_data)
  133. {
  134. return false;
  135. }
  136. // write the gif
  137. if (!$handle = fopen($this->_output_file_path, 'w'))
  138. {
  139. return false;
  140. }
  141. if (fwrite($handle, $gif_data) === false)
  142. {
  143. return false;
  144. }
  145. fclose($handle);
  146. return true;
  147. }
  148. return false;
  149. }
  150. // NOTE this provides a way to do it through pure PHPVideoToolkit class, however when ffmpeg creates animated gifs it uses a
  151. // limited colour palette for some stupid reason so the gifs created look rubbish.
  152. // public function saveNow($tmp_directory='/tmp/')
  153. // {
  154. // if($this->_saved === false)
  155. // {
  156. // $this->_saved = true;
  157. // // check there are frames to make a gif
  158. // if(!count($this->_frames))
  159. // {
  160. // return false;
  161. // }
  162. // if($this->_toolkit !== null)
  163. // {
  164. // $this->_toolkit->reset();
  165. // }
  166. // else
  167. // {
  168. // // get the ffmpeg class
  169. // require_once dirname(dirname(dirname(__FILE__))).DS.'phpvideotoolkit.php5.php';
  170. // $this->_toolkit = new PHPVideoToolkit($tmp_directory);
  171. // }
  172. // // save all the images from the ffmpeg_frames
  173. // $files = array();
  174. // foreach($this->_frames as $key=>$frame)
  175. // {
  176. // $file = $tmp_directory.'fag-'.$this->_toolkit->unique().'.jpg';
  177. // if(!imagejpeg($frame->toGDImage(), $file, 80))
  178. // {
  179. // return false;
  180. // }
  181. // // add file to array so it out deletes on close
  182. // array_push($this->_unlink_files, $file);
  183. // array_push($files, $file);
  184. // }
  185. // // print_r($files);
  186. // // prepare these images for conversion into a movie/gif
  187. // $result = $this->_toolkit->prepareImagesForConversionToVideo($files);
  188. // if(!$result)
  189. // {
  190. // return false;
  191. // }
  192. // // set the width and height
  193. // $this->_toolkit->setVideoOutputDimensions($this->_width, $this->_height);
  194. // // set the frame rate
  195. // $this->_toolkit->addCommand('-inputr', $this->_frame_rate);
  196. // // set the looping
  197. // $this->_toolkit->setGifLoops($this->_loop_count);
  198. // // set the output parameters
  199. // $output_path_info = pathinfo($this->_output_file_path);
  200. // $result = $this->_toolkit->setOutput($output_path_info['dirname'].'/', $output_path_info['basename'], PHPVideoToolkit::OVERWRITE_EXISTING);
  201. // if(!$result)
  202. // {
  203. // return false;
  204. // }
  205. // // execute the ffmpeg command
  206. // $result = $this->_toolkit->execute();
  207. // print_r($this->_toolkit->getLastCommand());
  208. // if(!$result)
  209. // {
  210. // return false;
  211. // }
  212. // return true;
  213. // }
  214. // return false;
  215. // }
  216. /**
  217. * Add a frame to the end of the animated gif.
  218. * @access public
  219. * @param resource $output_file_path The ffmpeg_frame object to add to the end of the animated gif.
  220. * @param boolean $save If true the gif will save after the frame has been added.
  221. * NOTE: this param does not feature in the actuall ffmpeg-php module.
  222. * @return boolean
  223. */
  224. public function addFrame($frame_to_add, $save=false)
  225. {
  226. if(get_class($frame_to_add) == 'ffmpeg_frame' && $frame_to_add->hasValidResource())
  227. {
  228. array_push($this->_frames, $frame_to_add);
  229. $this->_saved = false;
  230. return $save === true ? $this->saveNow() : true;
  231. }
  232. return false;
  233. }
  234. }