|
| 1 | +<?php |
| 2 | + |
| 3 | +require __DIR__.'/vendor/autoload.php'; |
| 4 | + |
| 5 | +use GifCreator\GifCreator; |
| 6 | +use GifFrameExtractor\GifFrameExtractor; |
| 7 | +use PHPImageWorkshop\ImageWorkshop; |
| 8 | + |
| 9 | +$gifPath = __DIR__.'/images/original.gif'; // Your animated GIF path |
| 10 | +if (GifFrameExtractor::isAnimatedGif($gifPath)) { // check this is an animated GIF |
| 11 | + |
| 12 | + // Extractions of the GIF frames and their durations |
| 13 | + $gfe = new GifFrameExtractor(); |
| 14 | + $frames = $gfe->extract($gifPath); |
| 15 | + |
| 16 | + // Initialization of the watermark layer |
| 17 | + $watermarkLayer = ImageWorkshop::initFromPath(__DIR__.'/images/watermark.png'); |
| 18 | + |
| 19 | + // For each frame, we add a watermark and we resize it |
| 20 | + $retouchedFrames = array(); |
| 21 | + foreach ($frames as $frame) { |
| 22 | + |
| 23 | + // Initialization of the frame as a layer |
| 24 | + $frameLayer = ImageWorkshop::initFromResourceVar($frame['image']); |
| 25 | + |
| 26 | + $frameLayer->resizeInPixel(350, null, true); // Resizing |
| 27 | + $frameLayer->addLayerOnTop($watermarkLayer, 20, 20, 'LB'); // Watermarking |
| 28 | + |
| 29 | + $retouchedFrames[] = $frameLayer->getResult(); |
| 30 | + } |
| 31 | + |
| 32 | + // Then we re-generate the GIF |
| 33 | + $gc = new GifCreator(); |
| 34 | + $gc->create($retouchedFrames, $gfe->getFrameDurations(), 0); |
| 35 | + |
| 36 | + // And now save it ! |
| 37 | + file_put_contents(__DIR__.'/output.gif', $gc->getGif()); |
| 38 | +} |
0 commit comments