What I want to do is to save only the first frame of an animated GIF in static image (non animated gif).
Using Gmagick 1.1.2RC1 and GraphicMagick 3.1.18
I read heaps of posts talking about it. Some says that it was working in the previous version of Gmagick but not in the new one.
Here is my current test code:
public function testAnimatedGifFrame()
{
$testAnimGif = $this->assets.'/animated.gif';
$im = new \Gmagick($testAnimGif);
$frameNumber = $im->getNumberImages();
$this->assertEquals(47, $frameNumber, 'The number of frame for the animated GIF should be 47');
// Select the first frame and save it
$frameIndex = 0;
$img = $im->coalesceImages();
foreach ($img as $frame) {
die('here');
$frameName = ++$frameIndex . '.gif';
$frame->writeImage( $frameName );
}
}
The animated GIF is composed of 47 frames, but when using coalesceImages(), the script is never getting inside the foreach loop (it's never dying).
Not sure what I've missed here.
For those looking to do it using Imagick or Gmagick like I was.
Just save it as a JPG and BAM!
public function testAnimatedGifFrame()
{
$testAnimGif = $this->assets.'/animated.gif';
$singleFrame = $this->assets.'/test_single_frame.jpg';
$im = new \Gmagick($testAnimGif);
$frameNumber = $im->getNumberImages();
$this->assertEquals(47, $frameNumber, 'The number of frame for the animated GIF should be 47');
// Save the image as JPG to disable the animation
$im->setImageFormat('JPG');
$im->writeImage($singleFrame);
$im->destroy();
}
If you want to save the last image of the animation instead of the first one, you just need to add $im = $im->flattenImages(); before $im->setImageFormat('JPG');
I hope this will help some of you ;)
Related
I have the following code to generate an animated GIF from a list of images. It's working fine but I would like to compress it. I tried a lot of different strategies but the resulting size is always exactly the same.
What am I doing wrong?
<?php
namespace AppBundle\Service;
class GifCreator
{
public function createGifFromImages(array $images)
{
// https://stackoverflow.com/a/10095594
$gif = new \Imagick();
$gif->setFormat("gif");
// adds all the images
foreach ($images as $image) {
$frame = new \Imagick();
$frame->readImage($image->getRealPath());
// THE FOLLOWING 2 LINES ARE NOT WORKING
//$frame->setImageCompression(\Imagick::COMPRESSION_JPEG);
//$frame->setImageCompressionQuality(5);
$frame->setImageDelay(50);
$gif->addImage($frame);
}
// optimizes the GIF
$gif->optimizeImageLayers();
// https://hotexamples.com/examples/-/Imagick/optimizeImageLayers/php-imagick-optimizeimagelayers-method-examples.html
// THE FOLLOWING 2 LINES ARE NOT WORKING
//$gif->setImageCompression(\Imagick::COMPRESSION_LZW);
//$gif->setImageCompressionQuality(30);
// writes the GIF
$tempPath = tempnam(sys_get_temp_dir(), 'GIF');
$result = $gif->writeImages($tempPath, true);
return $tempPath;
}
}
It seems that addImage isn't applying the proper filters.
Basicly I'm trying to merge a static image into a gif with Imagick. I think I'm close to it, but the resulting gif so far implements a black image into the gif instead of the image I expect. The dimensions are correct though. This is what I have so far:
<?php
if(!empty($_POST['url'])){
$gif = new Imagick('original.gif');
$img = new Imagick($_POST['url']);
$file_dst = 'result.gif';
$gif = $gif->coalesceImages();
foreach($gif as $frame){
$frame->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$frame->setImageArtifact('compose:args', "1,0,-0.5,0.5");
$frame->compositeImage($img, Imagick::COMPOSITE_MATHEMATICS, 0, 0);
}
$gif = $gif->deconstructImages();
$gif->writeImages($file_dst, true);
echo "done";
}
?>
Am I missing something that's causing this issue? Or am I doing something wrong?
I'm uploading an image from my mobile app to the server. I need to add two black bars on the top and bottom of the image. So what i tried is creating a black rectangle size of 2400x1200px and resize the uploaded image to 2400x930px and lay it on top of the black background.
$key = "video";
$document['video'] = $this->uploadfile($key);
$key = "thumbnail";
$document['thumbnail'] = $this->uploadfile($key);
$msg="panoramic image uploaded!";
$originalImagePath = "http://..../1501157655mainImage.png";
//$originalImagePath .= $document['video'];
$originalImage = new Imagick($originalImagePath);
$background = new Imagick();
$pixel = new ImagickPixel('#000000');
$background->newImage(2400, 1200, $pixel);
$originalImage->resizeImage(2370, 930)
$finalImage = $background->compositeimage($originalImage, Imagick::COMPOSITE_COPY, 1200, 600);
$document['new_video'] = $this->uploadfile($finalImage);
But it does not work. it fails at the 'new Imagick' i believe. I'm totally new to php. (Only couple of days). This code is written in a class. I have imported the imagemagick library in the construct method with the line
$this->load->library('Image_magician');
Copied the Image_magician.php file in the libraries folder. Any help is much appreciated.
I want to change quality of animated gif image via imagick in php.
I wrote the below code :
$source = 'images/b.gif';
$imageDesc = 'images/finally.gif';
$thumb = new Imagick($source);
$thumb = $thumb->coalesceImages();
foreach ($thumb as $frame)
{
$frame->thumbnailImage(300,300);
}
$thumb = $thumb->deconstructImages();
$thumb->setImageCompressionQuality(20);
$thumb->writeImages($imageDesc, true);
finally image ($imageDesc) was created but image's quality not be changed!
I have a problem with my php: when I make conversion PDF to JPG not shown correctly.
This is the original pictures of pdf - > http://s16.postimg.org/ma0jizgt1/text_problem2_fw.png
This is the jpg after converting with imagick - > http://s14.postimg.org/ilhs9tt3l/text_problem_fw.png
Please can you help me ?
Thank you
PHP:
if (move_uploaded_file( $_FILES["files"]["tmp_name"], $uploadUrlPdf . $_FILES["files"]["name"]))
{
$_FILES['files']['name'];
$nr_pag = $_POST['nr_pagini'];
for($i = 0; $i < $nr_pag; $i++)
{
$fn = $uploadUrlSwf.sprintf("%02d", "$i").".jpg";
if (!file_exists($fn))
{
$im = new imagick();
$im->setResolution($dpi,$dpi);
$pdf = $uploadUrlPdf.$_FILES['files']['name']."[$i]";
$im->readimage($pdf);
$im->setImageFormat('jpg');
$im->writeImage($fn);
file_put_contents( $fn, (string)$im );
$im->clear();
$im->destroy();
}
}
}
else
{
echo "error!";
}
Try setting your dpi value higher. If that does not work, try another export format (other than jpg). SVG would be your best bet, because you can scale that while the text and other shapes are still perfect quality. JPG wil not be sharp at a high zoom level if the resolution (dpi) is too low.
This forum entry might help: anti-aliased text when exporting PDF to image