How to convert .png file to .bmp? - php

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;

Related

tif to pdf/png enocoding

I am working on application where input files having extension .tif(Tagged Image File Format (TIFF)). The basic problem is to display .tif files in all browser which is not applicable on all major browsers. The solution i have accept is to convert them into png
I have am converting .tif file to png using http://image.intervention.io/api/encode in PHP Laravel 5.5
Image intervention is depends on imagick for tif encoding, i have installed this dependency but the encoding scheme is converting/display/store first image from tif file, not all files.
Can any one have solution from tif to PNG or PDF conversion using any PHP library?
I found very nice article here, the approach to solve this problem is,
$file = Storage::disk('s3')->get(request()->file_name);
$file_pdf =storage_path("app/bundle/tmp/".end($name).".pdf");
$document =new \Imagick();
$document->readImageBlob($file);
if (!$document->writeImages($file_pdf, true)) {
echo "Unable to write the file";
} else {
$document->clear();
}
return response()->file($file_pdf)->deleteFileAfterSend(true);
I have read s3 file stream using laravel storage and pass that blob using Imagick readImageBlob() method. The state of the art is $document->writeImages("to_pdf.pdf") file. At the end you can clear the file variable
My suggestion is: You have to use gdal2tiles.py to convert your .tif file into .png.
gdal2tiles Documentation
When you run this command its create an output folder, in which you get openlayers.html file, open it and see your .tif file in every browser easily.
If you need any help related to this, do comment. I'll guide you.

How to convert webP image format to normal ? php

I have an ios and android application. sometime the users upload webP image to my server. the problem is ios can't show this image when it's downloaded from my server.
so I want to check within my php code. if the image is webP format . then i will convert it to png format.
How could i do that using php?
It's late but just for the sake of it. It can be done using PHP only. Without any external tool.
Quoted from PHP.net documentation:
<?php
// Load the WebP file
$im = imagecreatefromwebp('./example.webp');
// Convert it to a jpeg file with 100% quality
imagejpeg($im, './example.jpeg', 100);
imagedestroy($im);
?>
So I assume you can use imagepng() instead of imagejpeg that is in the example.
Using libwebp:
( I assume $file is an absolute path and libwebp is installed)
$regex="/^(.*)(\.webp)$/";
if(preg_match($regex, $file)){
$out=preg_replace($regex, "${1}.png", $file);
exec("dwebp $file -o $out");
}
Didn't test, but should work...

How to open a .ico file in PHP?

I have tried to use php to caculate a image's avarage color.
So I use the php function: imagecolorat(), imagecreatefromstring()
This is a part of my code:
$fcontents = file_get_contents($imgname);
$im = #imagecreatefromstring($fcontents);
But I find it can read image successfully except .ico
How to deal with it?
Try https://github.com/lordelph/icofileloader
$loader = new Elphin\IcoFileLoader\IcoFileService;
$im = $loader->extractIcon('/path/to/icon.ico', 32, 32);
//$im is a GD image resource, so we could, for example, save this as a PNG
imagepng($im, '/path/to/output.png');
Function imagecreatefromstring() does not work with .ico - format: imagecreatefromstring() returns an image identifier representing the image obtained from the given image. These types will be automatically detected if your build of PHP supports them: JPEG, PNG, GIF, WBMP, and GD2.
Apparently, it is possible using this class
http://www.phpclasses.org/package/2369-PHP-Extract-graphics-from-ico-files-into-PNG-images.html

Generate Thumbnails with PHP for a wide range of file formats

I have had a client request on a upload facility for his clients, but after upload that a image thumbnail to be created.
All normal images are ok but he his talking about .psd, .pdf, .eps, .ppt
Having a good look around I think wih imagemagick & ghostscript will cater for most of these but I cant find a solution of PPT or EPS.
Im hoping that imagemagick will be able to do eps as it can do a psd.
Any suggestion on EPS or PPT file format.
Thank you if you can advice.
I'm late to the party I know, however...
This is what I use for .PDF, .EPS and .AI thumbnailing. (Assuming all necessary ImageMagick distros installed)
$file = 'filename.pdf.eps.ai';
$cache = $_SERVER['DOCUMENT_ROOT'].'/cache/';//ensure dir is writeable
$ext = "jpg";//just the extension
$dest = $cache.$file.'.'.$ext;
if (file_exists($dest)){
$img = new imagick();
$img->readImage($dest);
header( "Content-Type: image/jpg" );
echo $img;
exit;
} else {
$img = new imagick($_SERVER['DOCUMENT_ROOT'].'/'.$file.'[0]');
$img->setImageFormat($ext);
$width = $img->getImageheight();
//$img->cropImage($width, $width, 0, 0);
$img->scaleImage(105, 149, true);
$img->writeImage($dest);
header( "Content-Type: image/jpg" );
echo $img;
exit;
}
Don't know why it works, but it does - One code to rule them all right?
PPT is a powerpoint presentation. So creating an image that is a PPT would require some library that can pull this off.
Here are some resources to help you out.
Generate Powerpoint file on the fly
EPS is a vector format, so not unless you have your image as vector objects, you wont be able to do this correctly.
Just some thoughts - none of these things has been tested by myself.
EPS:
You should be able to convert your EPS to a PDF with ghostscript. Using imagemagick & ghostscript you can convert the PDF to some bitmap format (GIF, PNG or JPG).
PPT:
This seems to be somehow more complicated. If your are on a Windows machine you could resort to use the Powerpoint API from within a small hand-written converter. Another possibility would perhaps be to use Apache POI-HSLF whichs is a Java API to the Powerpoint file format. This would require a Java program for the conversion process. The last resort could be that study the Powerpoint binary file format and see if there is e.g. a thumbnail embedded (perhaps beeing used for the file icon in Windows Explorer) that could be extracted.
You could find some free icon sets and use a default icon for all .ppt file and another for all .eps. You can then further extend this for all file formats that cannot be converted to a image, such as audio files. Not the perfect solution but something a user may feel more comfortable with then just having text.

Convert jpg image to gif, png & bmp format using PHP

How can I convert a single jpg image into 3 different image formats, gif, png and bmp, using PHP?
You first create an image object out of your file with imagecreatefromjpeg(). You then dump that object into different formats (using imagegif() for example):
$imageObject = imagecreatefromjpeg($imageFile);
imagegif($imageObject, $imageFile . '.gif');
imagepng($imageObject, $imageFile . '.png');
imagewbmp($imageObject, $imageFile . '.bmp');
Use libGD — http://www.php.net/manual/en/book.image.php
I've set up a new opensource project on Github that allows reading and saving of BMP files (actual BMP files, not wbmp), and other file formats, in PHP. It's nice and easy to use.
The project is called PHP Image Magician.

Categories