PHP Convert jp2 to jpeg output - php

Using strictly the gmagick or imagick libraries for PHP I am trying to convert jpeg2000 data to browser-compatible jpeg. The jp2 works fine, but trying to convert to jpeg seems to just return the same data, no conversion being done.
$image = new Gmagick($_img->getImageBlob());
$image->setImageFormat("jpeg");
Header("Content-type: image/jpeg");
echo $image;
$_img contains the input jp2 data. I want to avoid using exec for conversion using the system library to make this platform independent.

Related

BMP->conversion->PNG->conversion->BMP (PHP function)

What is the best way in PHP to convert bmp images to png and then to convert back again without losing any information since png is the lossless format?
Both BMP images before and after conversion must be the same size. I have used image intervention but bmp image after conversion from png doesn't have the same size as original bmp.
Thank you.
Using php functions imagepng and imagecreatefrombmp resolved the problem. But only for 24-bit bitmap images.
Previously I have used image intervention with imagick driver but it didn't worked.

How to optimize PHP Imagick JPG to PNG conversion

I'm implement a code to convert JPEG to PNG format using imagick in PHP.
The problem is that the PNG image resulting from the conversion is four times larger than the original in JPEG.
How can I optimize this to obtain a size similar to the original?
The conversion code:
private function JPG2PNG($path, $newPath) {
$image = new Imagick();
$image->getCompressionQuality();
$image->readimage($path);
$image->setImageFormat("png32");
$image->setImageCompressionQuality(0);
$image->writeImage($newPath);
unlink($path);
}
You can't obtain similar size from original.
Check the manual here: http://php.net/manual/en/imagick.constants.php
Remember PNG is a lossless image this does not affect the actual image quality unlike JPG.
It's ok to use Imagick for converting images, but if you want to compress PNG files don't rely on Imagick use PngCrush instead.
Or you can use this source http://optipng.sourceforge.net/pngtech/optipng.html

Convert PDF to JPG image with PHP

I am using ImageMagik to try and convert the contents of a PDF to JPG, but keep getting an empty jpg. I have made sure the perms are 777 on everything for testing so I am a little lost how to continue.
Here is the script I am running
<?php
exec('convert testfile.pdf output.jpg', $output, $return_var);
?>
Try this.
<?php
$pdf = 'testfile.pdf';
$save = 'output.jpg';
exec('convert "'.$pdf.'" -colorspace RGB -resize 800 "'.$save.'"', $output, $return_var);
?>
Use the absolute path to the binary, like this:
exec('/usr/bin/convert testfile.pdf output.jpg', $output, $return_var);
But make sure your convert binary is actually on /usr/bin you can check that out with the following command:
which convert
convert -normalize yourfile.pdf[0] yourdestination.jpg
ImageMagick internally use GhostScript and Generally the conversion of ImageMagick is slow Comparing to Ghoastscript, so If you are only interested on getting convert pdf to images then Ghostscript gs command is faster.
below is an sample wrapper around Ghostscript which I wrote few days back.
PDFLib-Php
$pdflib = new ImalH\PDFLib\PDFLib();
$pdflib->setPdfPath($pdf_file_path);
$pdflib->setOutputPath($folder_path_for_images);
$pdflib->setImageQuality(95);
$pdflib->setDPI(300);
$pdflib->setPageRange(1,$pdflib->getNumberOfPages());
$pdflib->convert();
Here you have my solution. Use Imagick directly in your php code.
Convert all PDF pages to JPG
// create Imagick object
$imagick = new Imagick();
// Reads image from PDF
$imagick->readImage('file.pdf');
// Writes an image
$imagick->writeImages('converted.jpg', false);
Convert specific PDF page to JPG
// create Imagick object
$imagick = new Imagick();
// Read image from PDF
$imagick->readImage('test.pdf[0]');
// Writes an image
$imagick->writeImages('converted_page_one.jpg');
Another way to deal with this problem is to use spatie/pdf-to-image library.
Cheers!

How to convert CMYK/RGB TIFF to RGB JPEG using PHP IMagick

I have a PHP application which needs to deal with incoming TIFF files. I have neither control nor knowledge over the colorspaces of this TIFFs and the application should store all incoming images as RGB JPEGs.
Problem is, incoming TIFF files are anything: CMYK, RGB, some sort of YCbCr wrapped in sRGB, and so on, and I need to convert them somehow to RGB JPEGs before saving.
I need some sort of a conversion function in PHP which uses IMagick extension which can get any binary TIFF data and convert it to proper RGB JPEG binary data. It needs to handle different colorspaces inside TIFF images correctly. Output format (RGB JPEG) stays the same for any input file.
The following obvious solution converts some CMYK TIFFs correctly, some CMYK TIFFs get inverted colors and YCbCr RGB TIFFs get totally corrupted by red overlay:
$converter = new IMagick();
$converter->setResourceLimit(6, 1);
$converter->readImageBlob($data);
if ($converter->getImageColorspace() != IMagick::COLORSPACE_RGB
&& $converter->getImageColorspace() != IMagick::COLORSPACE_GRAY
) {
$icc_rgb = file_get_contents('sRGB_v4_ICC_preference.icc');
$converter->profileImage('icc', $icc_rgb);
$converter->setImageColorspace(IMagick::COLORSPACE_RGB);
}
$converter->setImageFormat('jpeg');
$converter->setImageCompression(Imagick::COMPRESSION_JPEG);
$converter->setImageCompressionQuality(60);
$converter->resizeImage(1000, 1000, IMagick::FILTER_LANCZOS, 1, true);
$converter->stripImage();
$result = $converter->getImagesBlob();
This solution is taken from there: http://blog.rodneyrehm.de/archives/4-CMYK-Images-And-Browsers-And-ImageMagick.html Obviously, it doesn't work for all colorspaces, because it doesn't detect them reliably. As you can see, it even uses the sRGB_v4 ICC color profile downloaded from it's homepage.
Google finds me one particular solution to the red overlay problem (just one of the conversion screw-ups), but it's only for console and when you know beforehand that you deal with YCbCr images:
convert some.tif -set colorspace YCbCr -colorspace RGB some.jpg
I can live with passthru-ing convert and pass to convert all the magical switches needed, but I suppose I need to detect the source image's colorspace beforehand and call a identify | grep before every convert in an otherwise PHP application is an overkill.
I've experienced this same issue.
It also came up in the imagick forums and the correction was pushed into ImageMagick 6.8.0-4 .
So upgrading should solve this issue. I've upgraded to ImageMagick 6.8.1-9 and haven't encountered this since.

Convert a PDF to jpeg on the fly with ImageMagick

I'm trying to convert the first page of a PHP generated PDF to an image, and have done so with the following code:
exec("convert http://####.com/tcpdf/examples/example_009.php[0] -resize 100 sample.jpeg");
However I don't want to save the image, I'm looking for a way of including the command in a PHP script in place of an image, e.g: <img src="display_image_script.php?pdf=dynamic_pdf.php">
Is there a way to get ImageMagick to return the image within the PHP page using header('Content-Type: image/jpeg')?
Untested, but try:
header('Content-type: image/jpeg');
passthru("convert somePdfFile.pdf jpeg:-");
You need the passthru to stream the binary back to the browser and the jpeg:- in the command string converts pdf to jpeg and returns the jpeg binary on stdout.

Categories