PDF 2 Image using PHP without loss of quality? - php

I'm trying to convert a pdf to an image file (png, jpg, gif does not matter).
But using the following code:
<?php
$im = new imagick('helloworld.pdf[0]');
$im->setImageFormat('png');
header('Content-Type: image/png');
echo $im;
?>
Picture quality is degraded significantly.
Is there any way to convert it with no or very little quality loss ?

you have to play with Imagick::setCompression, Imagick::setCompressionQuality and Imagick::setImageCompression
for this guy helped
$im->setImageCompression(\Imagick::COMPRESSION_UNDEFINED);
$im->setImageCompressionQuality(0);
anyway, you have to put some of this strings before you format the image to png

Related

Quality issue with converting a PNG to PDF with ImageMagick in PHP

I have a PNG file with quite decent quality and not at all small size. I convert it to PDF like this(in php):
$imagick = new Imagick();
$imagick->readImageBlob($image);
$imagick->setImageFormat('pdf');
echo $imagick->getImageBlob();
And I get a PDF file with my image in it. But the quality gets very poor. I thought I might fix with trying different density setting it with setResolution method. But this lead to either the image becoming very small or getting even worse quality. What am I doing wrong?

Setting background color as transparent while creating PNG image from EPS file

Im using PHP Imagick to process images, I need to convert an EPS file into PNG file and next the remaining code can process the created PNG, Im able to convert EPS as PNG and can process to, but when i do this the PNG file is creating with white background, I want it to be transparent where EPS file is not having any background either.
My code is as follows
$img = new Imagick();
$img->setResolution(300,300);
$img->readImage('my_file.eps); //reading the file
$img->setBackgroundColor ('#623423'); //setting background color but not working
$img->setImageFormat("png"); //setting the format to save//converting
$img->writeImage('converted.png'); //saving the converted file
But the generated png is coming with white bg, can some one help me how to create it with out BG color(transparent)?
Thanks in advance!.
Have you looked at this example?
<?php
$im = new Imagick();
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->readImage('carte_Alain2.svg');
$im->setImageFormat("png32");
header('Content-type: image/png');
echo $im;
?>

How to convert .png file to .bmp?

I need to convert a .png file to .bmp; I'm using the outcome in printer_draw_bmp() to print out a barcode.
GD can generate WBMP, but as far as I can tell that's not the same as .bmp. How can I do this conversion? Or is there another way to print a .png directly?
There is a opensource project on Github that allows reading and saving of BMP files (and other file formats) in PHP.
The project is called PHP Image Magician.
AFAIK, GD doesn't support bmp format. But you can use ImageMagick to save file in bmp format:
$im = new Imagick('image.png');
$im->writeImage('image.bmp');
Or if you want to output image to http response:
$im = new Imagick('image.png');
$im->setImageFormat('bmp');
echo $im;

Use PHP to convert PNG to JPEG

I have a website where users have been uploading a bunch of high quality PNG files. I want to use PHP to convert them to JPEG and re-size them to make them smaller in file size.
How can I do this when they upload the file? What is the process for doing this? Is a new image created or is it edited?
Thanks
You can use something like this :
function pngTojpg($pngImage, $outputPngFile, $outputJpgFile, $quality) {
$image = imagecreatefrompng($pngImage);
//Save the png image
imagepng($image, $outputPngFile);
//Save the jpeg image
imagejpeg($image, $outputJpgFile, $quality);
// Free up memory
imagedestroy($image);
}
"quality is optional, and ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75)"
The php doc : imagejpeg, imagecreatefrompng
These functions are from the GD library, here the installation instruction : Php GD
Use ImageMagick to do all kinds of conversions. You should be able to find examples at this link:
http://www.php.net/manual/en/book.imagick.php
Just try ImageMagick:
http://www.imagemagick.org/script/convert.php
I think, that is what you are looking for.
Well, you can use simple php code to do that but I use and recomend this library to work with images:
Verot - Class Upload http://www.verot.net/php_class_upload.htm
You can convert images to other format, reduce size, transform and do a lot others stuffs.

PHP Image Convert

In my web app users are allowed to upload images as their photos. How can I convert different image extensions to JPG? Input files are JPG, PNG or GIF.
Personally, I prefer Image Magick over GD. It's a lot better if you're dealing with large images too; you can run into memory allocation issues with GD.
With php, you can convert any image to an other using imagepng, imagejpeg, imagegif :
imagepng(imagecreatefromstring(file_get_contents($input)), 'output.png');
In this example, it will save the uploaded image in png with the path 'output.png'
You can use PHP GD.
For anybody who would want to get the binary out of a temporary file, here is my solution:
<?php
$temp = tmpfile();
imagepng(imagecreatefromstring($imgBinary), $temp);
$pathFile = stream_get_meta_data($temp)['uri']; // eg: /tmp/phpFx0513a
$pngBin = file_get_contents($pathFile)
?>

Categories