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