Convert jpg image to gif, png & bmp format using PHP - 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.

Related

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 convert any image file into EPS format in PHP?

I have an image processing system in PHP with Imagemagick. The existing system will be processing images of EPS format for some process and PNGs for the remaining process. So, I need to upload the same image file in EPS and PNG. I am doing the file upload facility now, which should automate the procedure of converting any format image file into EPS and PNG and should save in corresponding locations.
What I need now is to be able to convert any image format file into EPS and PNG, so then I can process and save them, but there are some DPI limitations. So I need to save the files into these EPS and PNG formats so that only the existing system can use those files properly.
Please advice me if there is any way to convert image files into EPS and PNG with PHP and Imagemagick.
Thanks in advance.
You can convert any image to eps format by following code
public function convertImageToEps(){
$imgUrl = WWW_ROOT.'imfcs.jpg';
$imagic = new Imagick();
$imagic->readImage($imgUrl);
$imagic->setImageFormat('eps');
$imagic->writeImage(WWW_ROOT.'simfcs.eps');
return true ;
}

Convert animated gif into PNG via PHP

The following is the code I'm using in order to convert the GIF file into a PNG file and save it into a variable:
$art = $_FILES["art"]["name"];
$art_ext = pathinfo($art, PATHINFO_EXTENSION);
if(strtoupper($art_ext)=="GIF"){
$art = imagepng(imagecreatefromstring(file_get_contents($_FILES["art"]["tmp_name"])), $art."png");
}
if($art!=""){
move_uploaded_file($art, "images/".$art );
}
Also, is there a method for converting an animated gif to a static gif?
This code should work. I assume you are following the example at Convert JPG/GIF image to PNG in PHP? To convert a gif to a static jif you could honestly just rename the png file you created with your current code to jif. All modern web-browsers and applications will treat a static jif and a png the same.
To get the first frame of an animated gif, you can use imagecreatefromgif and to save as a gif, you use imagegif, so it would be:
$art = imagegif(imagecreatefromgif($_FILES["art"]["tmp_name"]), $art."gif");

How to convert .png file to .bmp?

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;

PHP Image Convert

In my web app users are allowed to upload images as their photos. How can I convert different image extensions to JPG? Input files are JPG, PNG or GIF.
Personally, I prefer Image Magick over GD. It's a lot better if you're dealing with large images too; you can run into memory allocation issues with GD.
With php, you can convert any image to an other using imagepng, imagejpeg, imagegif :
imagepng(imagecreatefromstring(file_get_contents($input)), 'output.png');
In this example, it will save the uploaded image in png with the path 'output.png'
You can use PHP GD.
For anybody who would want to get the binary out of a temporary file, here is my solution:
<?php
$temp = tmpfile();
imagepng(imagecreatefromstring($imgBinary), $temp);
$pathFile = stream_get_meta_data($temp)['uri']; // eg: /tmp/phpFx0513a
$pngBin = file_get_contents($pathFile)
?>

Categories