I cannot find a working example. This one is not working, it's not animated:
$imagick = new Imagick($_FILES['file']['tmp_name']);
$format = $imagick->getImageFormat();
if ($format == 'GIF') {
$imagick = $imagick->coalesceImages();
do {
$imagick->resizeImage(500, 500, Imagick::FILTER_BOX, 1);
} while ($imagick->nextImage());
$imagick = $imagick->deconstructImages();
$imagick->writeImages($name, true);
}
$imagick->clear();
$imagick->destroy();
In ImageMagick command line, this works for me
Original Animation:
convert animation.gif -coalesce -resize 500x500 -layers optimize -loop 0 resize.gif
Did you do the equivalent of -coalesce after reading your input animation? Did you do the optimize step after resizing?
Sorry, I do not use PHP Imagick or any other API.
Related
I need this line:
magick "input.jpg" -fuzz 4% -transparent white -background white -alpha remove -alpha off -quality 85% "result.jpg"
to PHP imagick:
$img = new Imagick($root . '/testall/imagemagick/input.jpg');
// ???
$img->setImageFormat('jpg');
$img->setImageCompressionQuality(85);
$img->writeImage($root . '/testall/imagemagick/result.jpg');
and I can't find fuzz in https://www.php.net/manual/de/book.imagick.php to even start with.
Please help
You might be able to use: Imagick::transparentPaintImage:
$img = new Imagick($root . '/testall/imagemagick/input.jpg');
$fuzz = 4 * \Imagick::getQuantum(); // Copied from the example?
$img->transparentPaintImage('white', 0, $fuzz, false);
...
Well I tried around with andlrc answer and this is the working result:
$img = new Imagick('input.jpg');
$fuzz = Imagick::getQuantum() * ($whitePercentage / 100);
$BackgroundColor = "rgb(255, 255, 255)";
$img->setImageFormat('png');
$img->transparentPaintImage($BackgroundColor, 0, $fuzz, false);
$img->setimagebackgroundcolor('white');
$img = $img->mergeImageLayers( imagick::LAYERMETHOD_FLATTEN );
$img->setImageFormat('jpg');
$img->setImageCompressionQuality($qualy);
$img->writeImage($root . '/testall/imagemagick/result.jpg');
With imagick command, i want to resize image form 2Mb to 200KB, i tried:
convert a.jpg -strip -define jpeg:extent=200k a1.jpg
And it work. But i want to use imagick function of php to resize blob image.
Php's documentation regarding the resizeImage method require width and height.
How can resize blob image from 2Mb to 200Kb like the command line above.
I tried:
$imageBlob = file_get_contents('a.jpg');
$image = new \Imagick();
$image->readImageBlob($imageBlob);
$height = $image->getImageHeight();
$width = $image->getImageWidth();
$image->resizeImage( $width, $height, \Imagick::FILTER_LANCZOS, 1 );
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_buffer($finfo, $image->getImageBlob());
$mime == "image/svg" && $mime.= "+xml";
$base64 = "data:$mime;base64," . base64_encode($image->getImageBlob());
echo "<img src=\"".$base64.'">';die;
But this doesn't reduce the size from 2Mb to 200Kb
I have not tested this, but am fairly sure you can use setOption like this to set the upper limit for a JPEG's filesize:
$imagick->setOption('jpeg:extent', $extent);
When converting CMYK Illustrator AI or PDF to PNG the transparency get rendered with a white background. When converting a RGB AI or PDF to PNG the transparency remains with the rendered PNG. I have tried implementing the results shown here: Convert PDF (with transparency *and* CMYK) to jpg with no success. SVG and PSD files have no problems with transparency getting rendered to PNG from RGB or CYMK.
Our Server has Ghostscript 9.07 installed.
$f = $_FILES['imageLoader']['name'];
$im = new Imagick();
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->readImage("upload/$t/$f");
$width = $im->getImageWidth();
if (pathinfo($f, PATHINFO_EXTENSION) == 'psd') {
$im = $im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
}
if (pathinfo($f, PATHINFO_EXTENSION) == 'svg') {
$im->setBackgroundColor(new ImagickPixel('transparent'));
$svg = file_get_contents("upload/$t/$f");
$im->readImageBlob($svg);
}
if (pathinfo($f, PATHINFO_EXTENSION) == 'ai') {
$im->transformImageColorspace(Imagick::COLORSPACE_SRGB);
}
if (pathinfo($f, PATHINFO_EXTENSION) == 'pdf') {
$im->transformImageColorspace(Imagick::COLORSPACE_SRGB);
}
$im->thumbnailImage(149, 240, true);
$im->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$im->setImageResolution(72,72);
$im->resampleImage(72,72,imagick::FILTER_UNDEFINED,0);
$im->setImageFormat('png32');
$im->writeImage($outFile);
// clean up
$im->clear();
$im->destroy();
CMYK conversion to PNG example:
RGB conversion to PNG example:
Update:
Finally got the code working with help from the nice guys at the ImageMagick forum. It appears a problem was to set the colorspace before the image was read. This code now works for me.
$im = new Imagick();
$im->setColorspace(Imagick::COLORSPACE_SRGB);
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->readImage("upload/$t/$f");
$width = $im->getImageWidth();
if (pathinfo($f, PATHINFO_EXTENSION) == 'psd' ) {
$im = $im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
}
if (pathinfo($f, PATHINFO_EXTENSION) == 'svg' ) {
$im->setBackgroundColor(new ImagickPixel('transparent'));
$svg = file_get_contents("upload/$t/$f");
$im->readImageBlob($svg);
}
$im->thumbnailImage(149, 240, true);
$im->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$im->setImageResolution(72,72);
$im->resampleImage(72,72,imagick::FILTER_UNDEFINED,0);
$im->setImageFormat('png32');
$im->writeImage($outFile);
// cleanup
$im->clear();
$im->destroy();
I have compiled ImageMagick 6.8.7-10 Q8 x86_64 on Ubuntu 13.04 64-bit.
I also installed imagick 3.2.0RC1 for PHP 5.5.7.
I am using the following to crop an animated gif:
$imagick = new \Imagick('anim2.gif');
$imagick = $imagick->coalesceImages();
foreach ($imagick as $frame) {
$frame->cropImage(80, 80, 0, 0);
$frame->thumbnailImage(80, 80);
$frame->setImagePage(80, 80, 0, 0);
}
$imagick = $imagick->deconstructImages();
$imagick->writeImages('test.gif', true);
This is the original image:
And the cropped image becomes:
Notice that the cropped image gains a blue background in the second frame.
Why is this happening?
The problem is that coalesce introduces the background. This may or may not be a bug in imagemagick.
If running from the command line, you need to set the background to none before coalescing:
convert anim2.gif -background none -coalesce temporary.gif
From PHP's imagick library, you need to set all the frames' backgrounds to none:
$imagick = new \Imagick('anim2.gif');
foreach ($imagick as $frame) {
$frame->setImageBackgroundColor('none'); //This is important!
}
$imagick = $imagick->coalesceImages();
foreach ($imagick as $frame) {
$frame->cropImage(80, 80, 0, 0);
$frame->thumbnailImage(80, 80);
$frame->setImagePage(80, 80, 0, 0);
}
$imagick = $imagick->deconstructImages();
$imagick->writeImages('test.gif', true);
The result:
I try this:
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
$image = imagecreatefromstring(file_get_contents($path));
$w = imagesx($image);
$h = imagesy($image);
$centerX=round($w/4);
$centerY=round($h/2);
$watermark = imagecreatefrompng('lool-face.png');
$ww = imagesx($watermark);
$wh = imagesy($watermark);
imagecopy($image, $watermark, $centerX, $centerY, 0, 0, $ww, $wh);
eregi('\.(gif|jpeg|jpg|png)$',$path,$regs);
switch( $regs[1] ) {
case 'gif':
header('Content-type: image/gif');
imagegif($image);
break;
but this don't working, the images is curdle.
And try this:
shell_exec('for i in sphere*.gif; do convert $i -font Arial -pointsize 20 \
-draw "gravity south \
fill black text 0,12 \'Copyright\' \
fill white text 1,11 \'Copyright\' " \
wmark_$i; done');
shell_exec("convert -delay 20 -loop 0 wmark_sphere*.gif animatespheres.gif");
$f = fopen("animatespheres.gif", 'rb');
fpassthru($f);
fclose($f);
This code does not work I do not know why it does not.
We wish you a directory where there Gif htaccess can do it in gifs watermarking
This class could be your solution:
PHP Classes transparent watermark
Take a look at the link below. I have done the same thing in php. I hope that this will helps you.
https://dl.dropboxusercontent.com/u/58146160/watermark%20image.rar