Image Magick create thumbnail - php

Hi i am creating a thumbnail using imagemagick by following command
convert -define jpeg:size=".$this->info[0]."x".$this->info[1]." testi/".$this->filename." -auto-orient -thumbnail 200x200 -unsharp 0x.5 testi/".$this->filename
but this command only runs for jpack input file.
Can anyone tell me the command for any file type? like if gif then output shuld gif ?

Here is code:
<?php
// read page 1
$im = new imagick( 'test.pdf' );
// convert to jpg
$im->setImageColorspace(255);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(60);
$im->setImageFormat('jpeg');
//resize
$im->resizeImage(290, 375, imagick::FILTER_LANCZOS, 1);
//write image on server
$im->writeImage('thumb1.jpg');
$im->clear();
$im->destroy();
?>

What is a jpack file?
The -define will only work for jpg files but I thought if the output file was not a jpg it would be ignored; try removing that.
The output file should be the same name as the input file.
Try writing your code like this so you can see what the command actualy is; you can comment the echo out when its working.
$cmd = "-define jpeg:size={$this->info[0]}x{$this->info[1]} testi/{$this->filename} -auto-orient -thumbnail 200x200 -unsharp 0x.5 testi/{$this->filename}";
echo $cmd;
exec("convert $cmd");
I got a bit lost with the "$this->" business and so the code may not work as written. I try and keep things simple in my commands and would have put the filenames etc. into a variable outside the Imagemagick command.

Related

PHP - convert EPS to PNG using Imagick

I am trying to convert eps to png image using imagick.This is the code i am using.
$path = getcwd().'/uploads/1488/791/586/imprint_option_1A.eps';
$save_path = getcwd().'/uploads/1488/791/586/imprint_option_2E_c.png';
$image = new Imagick();
$image->readimage($path);
$image->setBackgroundColor(new ImagickPixel('transparent'));
$image->setResolution(300,300);
$image->scaleImage(600, 270);
$image->setImageFormat("png");
$image->writeImage($save_path);
but the transparency is not working i got image with white background ( Result image ). and when we scale image it loses clarity..
Any idea ?
Here is my eps file https://drive.google.com/open?id=0Bwq4DvGGbHVfT0FYTE94WW5GTnc
The function setResolution should be called before reading the image. Thus
$image = new Imagick();
$image->setResolution(1200, 1200);
$image->readImage($path);
should do it. As for the transparency, can you try to get the input as sRGB instead of CMYK? If I convert first the input file to pdf with epstopdf and then use this converted file in the PHP script, it produces a transparent PNG file.

Converting svg to png generates blank image

I have went through a lot of similar questions.i couldn't find an answer to my problem.
I have an svg image. I'm trying to convert it into a png image.
I have been using a 300dpi image as background of svg image.Now i have changed it into 600dpi. After that imagick returns an empty png image.
$svg=path to svg;
$im = new Imagick();
$im->readImageBlob($svg);
$im->setImageFormat("png32");
$im->setImageCompressionQuality(100);
$im->resizeImage(720, 445, imagick::FILTER_LANCZOS, 1);
$base64=base64_encode($im);
$im->clear();
$im->destroy();
return 'data:image/jpg;base64,' . $base64; //returns blank png
What am i missing here? Do i have to include any libraries??
PHP VERSION: 7
IMAGICK VERSION: ImageMagick 6.8.9-9
Looking at the documentation of Imagick::readImageBlob
Reads image from a binary string
this seems not the correct method for reading from a file path.
You should either use Imagick::readImage
Reads image from filename
$im = new Imagick();
$im->readImage($svg);
or the constructor Imagick::__construct
Imagick::__construct ( mixed $files )
Creates an Imagick instance for a specified image or set of images.
$im = new Imagick($svg);

PNGs resized with Imagick are black...what is the missing step in PHP?

When I use Imagick in PHP to resize PNG images, the output images are black. This is not the case when I resize JPGs; the PHP procedure (provided below) works perfectly for JPGs, so I suspect there's a missing step when processing PNG files.
I've first tested the conversion using ImageMagick on my local computer using this command:
convert x.png -resize 528 -filter Lanczos x-resized.png
...and it works; the resulting PNG file displays and is resized appropriately.
However, when I attempt to do the same on my server (both localhost and live), the result is a black image.
The PHP code I'm using is:
$source = "x.png" // I provide the complete path in my routine, etc.
$destination = "x-resized.png" // likewise.
$im = new Imagick();
$im->readImage($source);
$im->setImageFormat("png");
$im->resizeImage(528, null, Imagick::FILTER_LANCZOS, 1);
$im->writeImage($destination);
$im->clear();
What am I doing incorrectly?

Imagemagick remove the upper content while converting pdf to image

My pdf first page looks like :
When I run below command:
exec("convert -density 300 $pdf_path $temp_images 2>&1",$output);
It converts its page in image that looks like:
this thing happen only when the dimensions of pdf are width- 595 and height- 842.
Any suggestion will be appreciated.
Looks like the CropBox of the PDF is being used instead of the media size, or possibly ImageMagick is sending a fixed (incorrect, Letter) media size to Ghostscript in order to render the page.
Unfortunately that's about all I know regarding ImageMagick, you need someone who can tell you how to find and alter the Ghostscript invocation.
This code solve my problem but I want this in command line
$im = new Imagick();
$im->readImage( $pdf_path );
$im->setImageFormat( "jpg" );
$im->writeImage( $temp_images );
echo 'Image Converted';

Convert PNG to JPG and set transparent background to white with ImageMagick and PHP

How can I use ImageMagick (with the php extension) to set the transparent background to white when converting an image from PNG to JPEG?
At time of writing, you have not specified which extension you are using, but if you were using the commandline, the command would be:
convert image.png -background white -flatten -alpha off image.jpg
More information can be found on the Masking Usage documentation.
Using IMagick for instance, I think you could do this as follows:
(totally untested, never used IMagick and don't have it installed to test)
$image = new IMagick('image.png');
$flattened = new IMagick();
$flattened->newImage($image->getImageWidth(), $image->getImageHeight(), new ImagickPixel("white"));
$flattened->compositeImage($image, imagick::COMPOSITE_OVER, 0, 0);
$flattened->setImageFormat("jpg");
$flattened->writeImage('image.jpg');
$image->clear();
$image->destroy();
$flattened->clear();
$flattened->destroy();
If you are using the Imagick extension:
<?php
// load the source transparent png
$i = new IMagick('image.png');
// set the background to white
// you can also use 'rgb(255,255,255)' in place of 'white'
$i->setImageBackgroundColor(new ImagickPixel('white'));
// flattens multiple layers
$i = $i->flattenImages();
// the output format
$i->setImageFormat('jpg');
// save to disk
$i->writeImage('image.jpg');
// and/or output directly
// header('Content-Type: '.$i->getFormat());
// echo $i->getImageBlob();
// cleanup
$i->clear();
$i->destroy();

Categories