I am using GD to manipulate images in PHP, based on this code
<?php
/**
Función para detectar si una imagen es animación
*/
function is_ani($filename) {
if(!($fh = @fopen($filename, 'rb')))
return false;
$count = 0;
while(!feof($fh) && $count < 2) {
$chunk = fread($fh, 1024 * 100);
$count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
}
fclose($fh);
return $count > 1;
}
?>
I intend to separate the frames inside the GIF (I can even use this library ) and save them in WEBP format .
How can I assemble/create an animated image in WEBP format using GD in PHP?
--EDITED--
PHP version:
PHP 7.2.4 (built: Apr 25 2018 12:05:18) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
gd_info:
#php -r 'print_r(gd_info());'
Array
(
[GD Version] => bundled (2.1.0 compatible)
[FreeType Support] =>
[GIF Read Support] => 1
[GIF Create Support] => 1
[JPEG Support] => 1
[PNG Support] => 1
[WBMP Support] => 1
[XPM Support] =>
[XBM Support] => 1
[WebP Support] => 1
[BMP Support] => 1
[JIS-mapped Japanese Font Support] =>
)
This system is running on Debian 8.
--EDITED 2--
Using the alo-malbarez project as a base and a class to extract the frames of the GIF's, I made a project with a cleaner and clearer extractor class for people who need specific fragments of the GIF. In this project I combine both classes to make a GIF to WEBP converter, it still has bugs but I am working on correcting them.
The issue of how you extract the gif frames would be missing, but for that there is a link at the end
Here is a PoC on how to build an animated webp in php using only GD (with webp support)
PART 1 : encode each frame
1) the frame is converted to webp with GD and captured in a variable with
ob_start
2) we discard the fileformat header of this stream and save the frameData (starts with "VP8 " and goes to the end) in an array (buffer) along with the width, height and duration info of the frame
3) returns to 1 until there are no more frames
note: to change the compression quality edit the line
putting for example
the
null
is so that it does not record a filePART 2: build the animated webp according to the specification https://developers.google.com/speed/webp/docs/riff_container
0) open stream or set in a var
1) header file format (RIFF+filesize+WEBP)
1.a) calculate the file size (uint32) the file size is the total of bytes of the file -8 of the header, or the total of bytes of all the chunks + 4
2) VP8X chunk header
2.a) calculate chunksize (uint32) = 10
2.b) set the alpha and animation bits to 1
2.c) encode canvas width and height to uint24 (-1)
3) ANIM chunk header
3.a) compute chunksize (uint32) = 6
3.b) background color: BGRA (0,0,0,0)
3.c) loop count (uint16) 0=infinityandbeyond
4) for each frame saved in the array (buffer) PART 1.2
4.a) ANMF chunk header
4.b) calculate chunksize (uint32) = 16 + total bytes in frameData
4.c) X,Y origin of the frame (uint24) (/2)
4.d) width and height of the frame (uint24) (-1)
4.e) duration milliseconds (uint24)
4.f) reserved (6 bits) + alpha blending (1 bit) + discard frame (1 bit)
4.g) the Data frame saved in the buffer PART 1.2
5) save to disk or close stream
link of the repo in case you want to extend it or make a fork with the part of extracting the gif frames
https://github.com/aloMalbarez/php-GD-Animated-webp
to extract the gif frames you can use this class
https://github.com/jacoka/GIFDecoder
vibe
foreach ( $gifDecoder -> GIFGetFrames ( ) as $frame )
Edit I put a link to the GIFDecoder class of the original author
There really is little information, I had a hard time finding this:
https://gauntface.com/blog/2014/09/02/webp-support-with-imagemagick-and-php
There are also examples in the official documentation for PHP:
http://php.net/manual/en/function.imagewebp.php http://php.net/manual/en/function.imagecreatefromgif.php
But I summarize the first link (which is the interesting one) for your particular case:
Then you have the possibility to use a shell-like command with a binary of the type: https://developers.google.com/speed/webp/docs/gif2webp
Although I warn you that the format is poorly supported and I don't know to what extent it is reliable, based on my research, I would say that it is in a very green phase of development.