Use Imagick on XAMPP1.7.7 for save image - php

I have the following code block:
<?php
$im = new imagick('test.jpg');
echo "a";
?>
if $im is omitted, the result is "a" but if $im isn't omitted then the result is "". Is this because imagick isn't installed well? If that's the answer, how can I install it well for XAMPP 1.7.7? On the net there's little information and they talk about using old versions of imagick but I don't like that idea.
Another question I have about imagick is that I want to reprocess an image and save it in my server. I have read about the use of move_uploaded_file but if I use writeimage I won't use that. Do I need to use it in a different way?
Last question: I pass an image encoded in 64 base and I decode in a php file. How can I assign the decode image to Imagick without saving it before?

To display.
<?
$im = new imagick('test.jpg');
$im->setImageFormat('jpg');
header('Content-type: image/jpg');
echo $im;
?>
To save
<?
$im = new imagick('test.jpg');
$im->setImageFormat('jpg');
$saveToDir = "/home/";
$imageName = "image.jpg";
$im->writeImage($saveToDir . $imageName);
?>

Related

How to trim or remove black part from image in php imagick?

I have created an image using Imagick in PHP. But there is a black part in the background. I want to remove it. For more clarification, i have attached the image.
`
header('Content-type: image/jpeg');
$image = new Imagick('image.png');
$image->trimImage(.2);
echo $image;`
Code works fine for me. Your Imgagick Version has to be 6.2.9 or higher: https://www.php.net/manual/en/imagick.trimimage.php
Have you tried using $image->trimImage(0.2) ?

How to copy EXIF information from one file to another using PEL?

I have script that adds a watermark to some pictures in my photographic blog website. The watermarked file is served to the browser and leaves the original untouched. This part is working OK.
Recently I've found out that using simply GD for the watermark is stripping important information from the original file, the EXIF data. I've found that the solution is using PEL, so I need some help using it.
I was able to figure out in how to install PEL, but was unable to get it to copy EXIF data from $original_image to $new_image.
$jpeg = new PelJpeg($original_image);
$exif = $jpeg->getExif();
$tiff = $exif->getTiff();
$ifd0 = $tiff->getIfd();
$exif = $ifd0->getSubIfd(PelIfd::EXIF);
$ifd1 = $ifd0->getNextIfd();
/*
creates copy of $original_image to $new_image, adds watermark to $new_image
*/
$jpeg = new PelJpeg($new_image);
$jpeg->setExif($exif);
header("Content-Type: image/jpeg");
ImageJPEG($new_image);
This also produces an error like this:
Catchable fatal error: Argument 1 passed to lsolesen\pel\PelJpeg::setExif() must be an instance of lsolesen\pel\PelExif, instance of lsolesen\pel\PelIfd given, called in /var/www/html/clerigo/exif.php on line 71 and defined in /var/www/html/clerigo/pel/src/PelJpeg.php on line 304.
[EDIT]
Ok, managed to make this work like this:
$jpeg = new PelJpeg($original);
$exif = $jpeg->getExif();
/*
creates copy of $original_image to $new_image, adds watermark to $new_image
*/
$jpeg = new PelJpeg($new_image);
$jpeg->setExif($exif);
ImageJPEG($new_image, "new_image.jpg");
$jpeg->saveFile("/var/www/html/clerigo/new_image.jpg");
The thing is, this is saving an image to a file, and the purpose is NOT to save any image, but serving it only to the browser on request, like this:
$jpeg = new PelJpeg($new_image);
$jpeg->setExif($exif);
$jpeg->saveFile($new_image);
header("Content-Type: image/jpeg");
ImageJPEG($new_image);
imagedestroy($new_image);
But, this results in an error:
Warning: file_put_contents() expects parameter 1 to be a valid path, resource given in /var/www/html/clerigo/pel/src/PelJpeg.php on line 600
Any ideas how to solve this?
Take a close look at what you're doing here:
$jpeg = new PelJpeg($new_image);
$jpeg->setExif($exif);
header("Content-Type: image/jpeg");
ImageJPEG($new_image);
You're creating a new image called $jpeg and applying the EXIF data to it, but then you're outputting $new_image with ImageJPEG(). You'll need to make a call to PelJpeg::saveFile() to save the changes you've made and then serve up that file.
OK, figured it out, here is the final and working code:
$jpeg = new PelJpeg($original_image);
$exif = $jpeg->getExif();
/*
creates copy of $original_image to $new_image, adds watermark to $new_image
*/
$jpeg = new PelJpeg($new_image);
$jpeg->setExif($exif);
header("Content-Type: image/jpeg");
echo $jpeg->getBytes();

PHP imagick or GD merge image from an array

I was looking in google but I can't find any solution.
I need merge multiple png file via php. Everything works when I declarate image path like this:
<?php
$img1 = new Imagick('face.png');
$img2 = new Imagick('glasses-1.png');
$img1->compositeImage( $img2, imagick::COMPOSITE_DEFAULT, 0, 0 );
$img1->setImageBackgroundColor('transparent');
$img_wynik = $img1->flattenImages();
$img_wynik->setImageFormat('png');
$img_wynik->writeImage( 'avatar.png' );
header('Content-type: image/png');
echo $img_wynik;
?>
But I realy need to use images array, so i made test file and it doesn't work.
I thought I can use simple array like this
$srcImagesPath = array('glasses-1.png', 'mouth-1.png') //for example
and later change my testing code to this
$img2 = new Imagick ($srcImagesPath)
but it's always give me merged face.png and last image from array.
Any idea how to build it?
$srcImagesPath is an array, so you'll need to specify an element of it instead of passing the array itself. I guess you want:
$img2 = new Imagick ($srcImagesPath[0])
or
$img2 = new Imagick ($srcImagesPath[1])
if you have the GD library installed (assuming you have):
$image1 = imagefrompng('glasses-1.png');
$image2 = imagefrompng('mouth-1.png');
Once this is done you can create a frame in a new image using:
imagecopymerge()
I'll let you sort the destination, sources and coordinates... call it a challenge ;)
From there all you have to do is output the composited image to a file using imagepng:
imagepng($compositedImage, $path_to_filesystem_destination);
Using the command directly will output the image to the browser, the second parameter saves the image to a file.

Converting transparent PDF files with Imagick

I have a problem creating thumbnails with PHP and imagick. The code is working ok and the thumbnail is generated in the correct size etc but when I try to place a PDF logo on the thumbnail it turns half transparent. I guess it has something to do with the PDF file being generated in InDesign and it probably doesn't have any background defined. Has anyone come across this problem or has an idea what to do about it? I tried to put a white canvas in the background but that didn't help. I also specified a channel for the compositeImage function but that didn't help either.
This is the PDF file I'm having issues with: https://dl.dropbox.com/u/13712643/Case_Study.pdf
The generated Thumbnail looks like this:
https://dl.dropbox.com/u/13712643/Case_Study1.jpg
The code I have produced so far: http:// pastebin.com/74CYC972
Any ideas? Thank you for your help.
I had the same issue and I solved it by using Imagick::compositeImage that was found in here: php imagick convert PNG to jpg
The code goes something like this:
$im = new Imagick();
$im->readimage($pdfFile."[$currentPage]");
$res = $im->getimageresolution();
$bg = new Imagick();
$bg->setresolution($res["x"],$res["y"]); //setting the same image resolution
//create a white background image with the same width and height
$bg->newimage($im->getimagewidth(), $im->getimageheight(), 'white');
$bg->compositeimage($im, Imagick::COMPOSITE_OVER, 0, 0); //merging both images
$bg->setimageformat("png");
//then you can write to a file
$bg->writeImage('white-background-pdf-image.png');
//or output it
header('Content-type: image/png');
echo $bg;
May be this what you are looking for :
$im->setBackgroundColor(new ImagickPixel('transparent'));
http://www.php.net/manual/en/imagick.setbackgroundcolor.php
None of the existing answers worked for me. Flatten the image just after creating a new imagick() worked instead:
$im = $im->flattenImages();
Edit: The flattenImages method has been deprecated & removed. Use
$im = $im->mergeImageLayers( imagick::LAYERMETHOD_FLATTEN );

php Imagemagick jpg black background

I have a php script to create jpg thumbnail of pdf as follows;
<?php
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("jpg");
$im->resizeImage(200,200,1,0);
// start buffering
ob_start();
$thumbnail = $im->getImageBlob();
$contents = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/jpg;base64,".base64_encode($thumbnail)."' />";
?>
But the resulting jpg have black background instead of white.. How can I fix this??
None of the previously posted answers worked for me however the below did:
$image = new Imagick;
$image->setResolution(300, 300);
$image->readImage("{$originalPath}[0]");
$image->setImageFormat('jpg');
$image->scaleImage(500, 500, true);
$image->setImageAlphaChannel(Imagick::VIRTUALPIXELMETHOD_WHITE);
As I'm using the Laravel framework I then take the converted image and store it using Laravels filesystem.
Storage::put($storePath, $image->getImageBlob());
Update: So I recently changed OS and whereas this previously worked on my Ubuntu machine on my Mac certain images were still coming out black.
I had to change the script to the below:
$image = new Imagick;
$image->setResolution(300, 300);
$image->setBackgroundColor('white');
$image->readImage("{$originalPath}[0]");
$image->setImageFormat('jpg');
$image->scaleImage(500, 500, true);
$image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
Seems important to set the background colour before reading in the image. I also flatten any possible layers and remove the alpha channel. I feel like I tried ALPHACHANNEL_REMOVE on my Ubuntu machine and it didn't work so hopefully between these answers readers can find something that works for them.
If your version of Imagick is not up to date, the setImageBackgroundColor may be wrong.
Swap the following line
$im->setImageBackgroundColor("red");
to this (Imagick version >= 2.1.0)
$im->setBackgroundColor(new ImagickPixel("red"));
or (Imagick version < 2.1.0)
$im->setBackgroundColor("red");
I solved it by;
$im = new imagick(realpath($file).'[0]');
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(100);
$im->setImageFormat("jpeg");
$im->writeImage("imagename.jpg");
Simply adding this prevents the JPG to be created with a black background
-alpha off
change this code $im->setimageformat("jpg"); to this code
$im->setimageformat("png"); if you face a background colour issue.
Just use flattenImages() right after creating a new imagick():
$im = new Imagick('file.pdf[0]');
$im = $im->flattenImages();
Edit: The flattenImages method has been deprecated & removed. Use
$im = $im->mergeImageLayers( imagick::LAYERMETHOD_FLATTEN );
After endless attempts to append a pdf file with a jpeg image without getting black areas, I found the solution: the function transformImageColorspace
Used in this order works perfectly:
$im = new Imagick();
$im->readImage("file.pdf");
$im->transformImageColorspace(Imagick::COLORSPACE_SRGB);
$im->setImageFormat('jpeg');
$im->writeImage('image.jpg');
I would like to add to the excellent and helpful answers by saying that a slightly different approach is required if your PDF has multiple pages.
Something I was surprised to discover is that the Imagick class implements Iterable, which means you can run it through a foreach loop and operate on each page in turn (which it turns out is necessary because the layer, colour, and alpha channel changes appear to only take effect on the last page) which will be presented to you as a separate Imagick object:
$im = new Imagick('path/to/file.pdf');
foreach ($im as $c => $page)
{
// do what you need to do with each page and then...
$im->writeImage('path/to/image-'.$c.'.jpg');
}

Categories