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.
Related
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
I am trying to convert a vector image formats .emf,.wmf to a high resolution sharp and crisp raster image .gif,jpg. (Usually this could be easily done in Illustrator). But i am unable to do this in PHP. I am trying the following code but the results are either blurry or distorted or even totally black.
<?php
$image = new Imagick("1.emf");
$image->resizeImage(1500,0,Imagick::FILTER_LANCZOS,1);
$image->setImageFormat('gif');
$image->setresolution(900, 900);
$image->writeImage("2.gif");
?>
We just needed to set the resolution before loading the image.
$image = new Imagick();
$image->setresolution(300, 300);
$image->readimage($filename);
$image->resizeImage(1500,0,Imagick::FILTER_LANCZOS,1);
$image->setImageFormat('jpg');
$image->writeImage("1.jpg");
This code will convert a vector to a sharp and crisp raster image. It works for all vector formats (svg, ai, emf, wmf, etc). If the jpg result is unexpectedly a black image, you need to change the image transparency to white (check this link). Another way to get around with transparency problem is by getting your PHP updated to 5.5 and by installing Imagick for that version. By doing so, it will not cause any problems with transparent images and the above code will just work fine.
For testing purposes you could change jpg to png because it supports transparency.
I want to convert images to PNG format, resize them (width/height) and then convert them the same way http://tinypng.org does (to 8-bit paletted PNGs). How can I do this in PHP?
I know to convert to PNG, I use imagepng (built in PHP function), and to resize them width/height I use a combination of imagecopyresampled and imagecreatetruecolor (correct?), but I'm not sure how to convert the PNGs to 8-bit.
How do I efficiently compress a PNG? In my case, the images are small grayscale images with transparency.
Currently I'm playing with this:
// ...
$im->setImageFormat('png');
$im->setImageColorspace(\Imagick::COLORSPACE_GRAY);
$im->setImageCompression(\Imagick::COMPRESSION_LZW);
$im->setImageCompressionQuality(9);
$im->stripImage();
$im->writeImage($url_t);
As Imagick doesn't offer COMPRESSION_PNG, I've tried LZW but there's almost no change in the filesize (usually it's even bigger than before).
If I open the image in GIMP and simply save it, the filesize gets drastically reduced (e.g. 11,341 B --> 3,763 B or 11,057 B --> 3,538).
What is the correct way of saving a compressed PNG with Imagick?
Have a look at the first part of this answer:
Convert multipage PDF to PNG and back (Linux)
It explains the meaning + syntax of ImageMagick's -quality setting for PNGs.
I'm definitely not sure if it is correct way to save PNG, but my way is:
$im->setImageCompression(\Imagick::COMPRESSION_UNDEFINED);
$im->setImageCompressionQuality(0);
This gives me perfect quality of the image and file size very similar to PS6 saved 'Save for Web'. Sometimes even smaller sizes!
Is there any way to convert GIF file in JPG using php?
Yes, using the gd or ImageMagick libraries or friendly wrappers around them like WideImage.
There's several ways to convert image formats in PHP.
One way is with GD:
$im=imagecreatefromgif("agifimage.gif");
imagejpeg($im, "ajpegimage.jpg");
imagedestroy($im);