Can I detect animated gifs using php and gd?
There is a brief snippet of code in the PHP manual page of the imagecreatefromgif()
function that should be what you need:
imagecreatefromgif
comment #59787 by ZeBadger
Display animated GIF from outside public directory (GD?)
If you're not manipulating the image at all, I would just return the contents of the file with the correct header.
For example:
<?php
$file = 'whatever.gif';
// Do whatever checks you want on permissions etc
header('Content-type: image/gif');
readfile($file);
?>
Readfile outputs the contents of the file, and the header makes sure the browser sees it as a gif and shows it. Because you're not using GD it will still be animated.
The catch with this method is you'll need to know the content type for each file to server it correctly.
http://php.net/manual/en/function.readfile.php
How do I programmatically check whether a GIF image is animated?
With Python and PIL:
from PIL import Image
gif = Image.open('path.gif')
try:
gif.seek(1)
except EOFError:
isanimated = False
else:
isanimated = True
Exploding Animated GIF and Manipulating Frames (GD Library)
A GIF does not contain just separate images appended after each other. A frame in a GIF may change just a part of the image - it does not have to cover the whole frame. It can also contain a local palette, but otherwise it relies on the global palette of the image - which is stored for the file itself and not just the frame.
I.e. - you can't just explode the file and decode each segment separately and except to get useful images from GD.
You'll at least have to add the gif header to each set of image data, but I strongly recommend using the PHP ImageMagick interface instead if possible - it has far better support for iterating through frames in an image.
Another option is using a pure PHP implementation that does what you want, such as GifFrameExtractor.
The relevant code is located at line 137 of the source file:
$img = imagecreatefromstring(
$this->fileHeader["gifheader"] .
$this->frameSources[$i]["graphicsextension"] .
$this->frameSources[$i]["imagedata"] .
chr(0x3b)
);
As you can see, there is far more data necessary (the header, the extension (87a vs 89) and a terminating character) to make it valid GIF data.
Create animated gif using the GD library
Well searching on Google revealed GIFEncoder.class.php found at http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html
This link requires registration.
So i searched a little and it is included in phpvideotoolkit on code.google.com and can be downloaded at:
http://phpvideotoolkit.googlecode.com/svn-history/r6/trunk/phpvideotoolkit/adapters/ffmpeg-php/gifencoder/GIFEncoder.class.php
there is also a bugfixed version just change the file name to GIFEncoder.class.phpvideotoolkit.php in the above link.
I haven't tried it myself but maybe it can help you.
in the parent directory of the php file on code.google.com is also an example
best of luck
Detecting animated GIF on the fly using PHP/CodeIgniter
Check out this comment to the documentation for function imagecreatefromgif (on php.net). You still need to first download the image file even if you are just going to link to it later.
How do edit animated GIF image with GD?
If you can use imagick:
$gif = new Imagick('full/path/to/your/image.gif');
$draw = new ImagickDraw();
$draw->setFont('full/path/to/your/font.ttf');
$draw->setFontSize(30);
$draw->setFillColor('white');
// put text on each frame
foreach($gif as $frame){
$gif->annotateImage($draw, $x = 10, $y = 45, $angle = 0, 'Your text');
}
header('Content-Type: image/gif');
print $gif->getImagesBlob();
PHP GD Image - output animated GIF
If you want It to be an actual watermark - present in the whole image when downloaded - then you'll have to convert the whole thing to a gif and overlay the background image (repeated every frame) with the watermark frames.
Do you have to have the animated watermark? Could you have a static watermark with the animated watermark positioned over the top of it?
At the end of the day, you can't animate a jpg, and having all your content saved as gifs is something I'd try and avoid
Related Topics
Checking Browser's Language by PHP
Pcre Regular Expression Overlapping Matches
Simplexml Soap Response Namespace Issues
Destroy Session on Window Close
Upload Doesn't Work Right When the File Is Too Big
How to Create Clean Url Using .Htaccess
How to Stop PHP Sleep() Affecting My Whole PHP Code
How to Get Wordpress Post Featured Image Url
Any Way to Break If Statement in PHP
Display an Array in a Readable/Hierarchical Format
Xdebug for Remote Server Not Connecting
Laravel Password Validation Rule