Low resolution test when convert PDF to JPEG with PHP and ImageMagick - php

I have use this code to convert PDF file to JPEG images
$im = new Imagick();
$im->setResolution(90,90);
$im->readImage($pdf_file);
$im->setImageFormat('jpeg');
$im->writeImages($save_to,false);
$im->clear();
$im->destroy();
and it work but I have a problem that when there is a text with white background it will not be clear but I don't have this problem when ever I have colored BG.
this image will make every thing clear

JPEG compression generates such artefacts on edges where there is big color differences (such as between your black text and your white background). Try to to push up the compression quality
or use another image format for images containing text (such as png)

Add this functions:
$im = new Imagick();
$im->setResolution(90,90);
if ($width < 300) $im->sharpenImage(4, 1);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(100); // or some alse
$im->readImage($pdf_file);
$im->setImageFormat('jpeg');
$im->writeImages($save_to,false);
$im->clear();
$im->destroy();

Related

Imagick PHP EPS to PNG conversion no clean results on the edges

When I am converting an EPS to PNG with imagick for generating a thumbnail preview. The result is very bad around the edges of a EPS without a background.
The Colorspace is CMYK but the result is the same using RGB. As I read, PNG does not support CMYK so I have to convert it first to SRGB. But It does not change anything. Normally I would set the resolution to 72dpi, but I also have tested it with 300dpi. Is there maybe a special order for the method calls to consider?
My Code:
$im = new \Imagick();
$im->setResolution(72, 72);
$im->setColorspace(imagick::COLORSPACE_SRGB );
$im->readImage($target_file);
$im->setImageAlphaChannel(imagick::ALPHACHANNEL_DEACTIVATE);
$im->setImageDepth(8);
$im->adaptiveResizeImage(800, 0);
$im->setImageFormat("png");
$im->writeImage($thumb_dir . basename($zoom_file_ne . '.png'));
$im->destroy();

I create a cmyk image by php imagemagick, but cmyk color is different on photoshop?

NEED HELP!
I create a cmyk image by php imagemagick, but cmyk color is different on photoshop! e.g.: set ImagickPixel color cmyk(0,0,0,100)(black), but found cmyk(61,61,61,0) on photoshop.
why? and how to set the correct cmyk color?
You need to set the colorspace to CMYK, other wise your pixels will be converted to RGB.
$img->setImageColorspace(Imagick::COLORSPACE_CMYK);
http://php.net/manual/en/imagick.setimagecolorspace.php
Also all make sure that you are using file type that supports CMYK. ( eg. .jpg, .tif )
Edit
It seems Imagick has a bug.
Until it is fixed you can try and use this work around using transformImageColorspace.
$draw = new \ImagickDraw();
$fillColor = new \ImagickPixel();
$fillColor->setColor('cmyk(0%,0%,0%,100%');
$draw->setFillColor($fillColor);
$draw->rectangle(100, 100, 400, 400);
$img = new \Imagick();
$img->newImage(500, 500, 'white');
$img->drawImage($draw);
$img->transformImageColorspace(Imagick::COLORSPACE_CMYK);
$img->setImageFormat("jpg");
header('Content-Type: image/'.$img->getImageFormat());
echo $img;

PHP imagegif producing image with poor quality

I am working on a script which will convert jpg to gif. The problem I am facing is that the output of imagegif is with bad quality and some colors change. ( black )
Here are to samples. One in JPG and the other in GIF.
http://oi62.tinypic.com/atx1j.jpg [JPG]
http://oi62.tinypic.com/oiyscy.jpg [GIF]
As you can see the colors of the GIF image has changed alitle.
I am using the following code
$img = imagecreatefromstring(base64_decode($image));
imagegif($img, "output.gif");
How can I improve the quality of the gif image?
Here's how you could achieve slightly better quality:
<?php
// load image
$image = imagecreatefromstring(file_get_contents('http://oi62.tinypic.com/atx1j.jpg'));
// create a true color image of the same size
$image2 = imagecreatetruecolor(imagesx($image), imagesy($image));
// copy the original gif image on to the true color image
imagecopy($image2, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
// output image
header("Content-Type: image/gif");
imagegif($image2);
imagedestroy($image);
imagedestroy($image2);
?>
Source Image
PHP Generated GIF Image
As you can see, it's slightly better. Very close to orignal. But you are still limited to 256 colors in GIF =[
GIF is inherently worse in quality than JPEG, this is because GIF only has 256 colours available to use, while JPEG has 4,294,967,296 colours...
If you have fewer than 256 colours in your image, GIF is actually higher quality than JPEG, since it doesn't do any compression, while jpeg compresses.
This has nothing to do with PHP: The JPEG format is a true color (or grayscale) format, while the GIF format is a palette format, with max palette size way below true color.
You need to reconsider your conversion process.

Converting an SVG to PNG and keep transparency does not work

I am trying to convert an SVG to PNG and keep the transparency, but that does not work. Instead, I get a black background.
This is my code:
$png = new Imagick();
$png->setBackgroundColor(new ImagickPixel("transparent"));
$png->readImageBlob($svg);
$png->setImageFormat("png32");
Thanks in advance!

Imagick convert SVG to PNG - colors replaced by black and white

I'am trying to convert an SVG Image which is created by the SVGGraph library ( http://www.goat1000.com/svggraph.php).
The SVG is colored (red, green, yellow, gray, ...) in the browser and everything is fine. But when I convert it, its just black and white.
With this code I convert it:
//new instance of imagick
$im = new Imagick();
//read the svg file/data (its not saved on the filesystem)
$im->readImageBlob($svgFile);
$im->setImageFormat("png24");
$im->writeImage('cylinder.png');
$im->clear();
$im->destroy();
I've tried it with jpeg and png as output format but the result is the same, all colors will be replaced with black
does anyone have an idea how to fix that ?
Try this way:
$im = new Imagick();
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->readImageBlob($svgFile);
$im->setImageFormat("png24");
$im->writeImage('cylinder.png');
$im->clear();
$im->destroy()

Categories