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?
Related
I have written a function in PHP 7.4 that generates a thumbnail of the first page of a PDF document. The function works as intended for most PDF files.
However, some PDFs get cropped when I generate the thumbnail. Additionally, sometimes, I don't get the first page.
This PDF triggers both problems:
http://www.teavigoinfo.com/pdf/study-27.pdf
function PDF2Thumbnail($url) {
$image = #file_get_contents($url);
if($image) {
$im = new Imagick();
$im->readImageBlob($image);
$im->setIteratorIndex(0);
$im->thumbnailImage(300, 0);
$im->setImageFormat('png');
$im = $im->flattenImages();
header('Content-Type: image/png');
echo $im;
}
}
Thank you.
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
I tried to make a thumbnail of a pdf file which is hosted on another server. My current code is:
<?php
$im = new imagick("http://www.d3publisher.us/Walkthroughs/Naruto_NC_3_DS.pdf");
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
The problem is that code is only generating thumbnail for LAST PAGE of the pdf file. How can I make a thumbnail for first page only? I tried to add [0] at the imagick line.
$im = new imagick("http://www.d3publisher.us/Walkthroughs/Naruto_NC_3_DS.pdf[0]");
but it didn't work. It only work for local pdf file, i.e:
$im = new imagick("my-pdf-file.pdf[0]");
Please help me solve this problem.. Thanks..
You'll need to reset the active image to the first page. This can be done with Imagick::setIteratorIndex.
<?php
$im = new imagick("http://www.d3publisher.us/Walkthroughs/Naruto_NC_3_DS.pdf");
$im->setIteratorIndex(0);
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
Try...
$im->setImageIndex(0); //this will return 1th page of the pdf file
$im->setImageFormat('jpg');
"This can be done with Imagick::setIteratorIndex. .."
..or not. Simply has no effect . Setting it to one crashes something, setting it to 0 gets the last page..
function make_thumbnail($filename)
{
try
{
$imagick= new Imagick($filename);
}
catch(ImagickException $e)
{
// failed to make a thimbynail. what now?
// load up our trusty truetype font png instead?
$imagick->destroy();
return "0"; // shove any rubbish in the db - it will just say no image available when asked.
}
$imagick->setIteratorIndex(0);// rewind to first page or image of a multi series
$imagick->setImageFormat("png"); // turn it into a png
$imagick = $imagick->flattenImages(); // remove any transparency
$imagick->scaleImage(300,0); //resize...to less than 300px wide
$d = $imagick->getImageGeometry();
$h = $d['height'];
if($h > 300)
$imagick->scaleImage(0,300);
$imagick->setImageCompression(\Imagick::COMPRESSION_UNDEFINED);
$imagick->setImageCompressionQuality(0);
$imagick->setIteratorIndex(0);
$a = $imagick->getImageBlob(); // output as bytestream
$imagick->destroy();
return $a;
}
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 ;)