Output only progressive image format with scaleImage() and writeImage() imagemagick functions.
Thanks.
Your question is hard to understand, but I think you want a baseline JPEG and not an interlaced one.
If that is the case, all you need to do is remove the line:
$image->setInterlaceScheme(IMagick::INTERLACE_LINE);
Related
I'm trying to write a PHP function to convert an SVG image without any antialiasing (so that the final PNG is blocky and contains only the colours specified in the SVG).
The command line equivalent is:
convert +antialias /path/to.svg /path/to.png
I'm assuming that I need to use PHP's Imagick::setOption method to pass in "+antialias", but the documentation is very sparse.
The following snippet will write a PNG file, but none of the options prevents antialiased pixels being rendered:
$image = new Imagick();
// None of these have any affect - output image is always antialiased.
$image->setOption('+antialias', true);
$image->setOption('-antialias', true);
$image->setOption('+antialias', 'true');
$image->setOption('-antialias', 'true');
$image->setOption('antialias', true);
$image->setOption('antialias', false);
$image->setOption('antialias', 'true');
$image->setOption('antialias', 'false');
$image->readImage('/path/to.svg');
$image->writeImage('/path/to.png');
Any help would be great, thanks.
That is not a good way to anti-alias a vector file such as SVG. The proper way in command line would be to set the desired density before reading the file and then resize back to compensate for a large magnification when using a large density. So for example
convert -density 288 image.svg -resize 25% image.png
Nominal density (default) is 72. So 288 = 72*4. Thus we resize afterwards by 1/4 = 25%, unless you want a larger output. Then resize by a larger value.
In PHP Imagick, you can create a new Imagick() instance. Then set the desired density. Then read the input SVG. Then resize. Then set the PNG format and save to disk. See setImageResolution for setting the density in Imagick. See https://www.php.net/manual/en/imagick.setimageresolution.php
I don't think what you want is possible, unfortunately. ImageMagick shells out to Inkscape to render SVGs, and Inkscape has no command-line option to disable antialiasing.
https://graphicdesign.stackexchange.com/questions/138075/export-svg-without-anti-aliasing-in-inkscape-1-0-by-command-line
You could render at high resolution and then do nearest-neighbour downsampling. It would reduce the visible antialiasing, but you would still get some colours not in the SVG file.
Is there a way to turn a jpg to string, reverse of imagecreatefromstring?
I have to communicate to a server which needs binary of image, i saw plenty of jpg to binary but not the other way around.
Just a shot in the dark here... No real experience with this, just my thoughts after looking through some documentation...
I see in the documentation of imagecreatefromstring() an example is given where a base64 encoded string is converted into an image. Taking that example and flipping it around might just be what you are looking for.
$image = file_get_contents('image_file.jpg');
$imageString = base64_encode($image);
imagecreatefromstring takes a string which contains the binary data of an image and turns it into a gd image resource so you can manipulate it with the gd image library. Literally the "reverse" of that would be imagejpeg, which saves a gd image resource to a jpeg image.
I guess what you really want though is simply the initial string, which contains the binary data of the image to begin with. I.e.:
$imageString = file_get_contents('image.jpg');
$gd = imagecreatefromstring($imageString);
Just skip step 2.
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);
There's a need to transform .svg files and save em either in .svg or jpeg format. The problems with ImageMagick is that it saves transformed files on white background and I deadly need it on transparent.
Any suggestions with other tools or clear php? Would really appreciate it.
The right ImageMagick command should be:
convert -background none somefile.svg somefile.png
You should use PNG or GIF as file format, because JPEG doesn't support transparency.
To use it in PHP:
<?php
$svg_file_name = "somefile.svg";
$png_file_name = "somefile.png;
system("convert -background none $svg_file_name $png_file_name");
?>
I doubt you can transform SVG files easily from within php. SVG files are basically XML files, and the standard is public, so anyone can make a converter...
I'd go for the external tool, it's easier and faster than processing from within a scripted language, and a lot safer when the author of the script dosen't actually know how to find out the command line switches for an application, and that JPEG files does not support transparency:)
go for convert -background none somefile.svg somefile.png as Jens said...
You can't do transparency with JPEG, but here's how to save an SVG as a PNG with a transparent background...
$image = new Imagick();
$image->setBackgroundColor(new ImagickPixel('transparent'));
$image->readImage('somefile.svg');
// ... do any image manipulation you need to here ...
$image->setImageFormat('png32');
$image->writeImage('somefile.png');