Convert Jpg Image to Gif, Png & Bmp Format Using PHP

Convert jpg image to gif, png & bmp format 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');

Convert JPG/GIF image to PNG in PHP?

You just need imagepng() then. In fact it almost becomes a one-liner:

 imagepng(imagecreatefromstring(file_get_contents($filename)), "output.png");

You would use $_FILES["id"]["tmp_name"] for the filename, and a different output filename obviously. But the image format probing itself would become redundant.

PHP image conversion JPG/GIF/PNG to BMP / ICO

As far as I know ImageMagick can do this conversion (and it's free)

PHP - Unzip and convert images from .jpg to .bmp

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');

jpg bmp png extension GET from image For URL PHP mysql

Not quite sure what you mean, but something like this?
It puts all the extensions from the array in front of the file path, and if it exists, it outputs the image.

$extensions_to_check = array('jpg','png','gif','jpeg');
$dir = '/path/to/dir/';
$file_name = 'wallpaper_something';

foreach($extensions_to_check as $ext) {
$path = $dir . $file_name . $ext;
if (file_exists($path)) {
echo('<img src=\'' . $path . '\' />');
}
}

Save image in different formats

You cannot do that with GD, you need to use ImageMagick. I assume you have already image in a format png, and that image url is $imageUrl.;

$imageFormat = $_POST["format"];
$imageFormats = array("tiff", "bmp");
if (in_array($imageFormat, $imageFormats)) {
$handle = fopen($imageUrl, 'rb');
$imageMagick = new Imagick();
$imageMagick->readImageFile($handle);
$imageMagick->setImageFormat($imageFormat);
$imageMagick->setImageColorSpace(5);
$imageMagick->writeImage("path_to_tiff_image." . $imageFormat);
fclose($handle);
$imageMagick->destroy();
} else {
die("Invalid format!");
}

For PSD part, why do you want to save it as psd? There is no way to do that. If you say the reason of saving as psd, I can help you.

Convert JPEG to 8bit BMP

I hope that I found the answer. I've used imagick library becouse GD library doesn't respond bmp format, instead it uses wbmp which makes a diffrence.

My code to call imagick:

<?php
//convert jpeg to 8 bit bmp with imagick library

//Read the image
$im = new imagick( 'input.jpg' );

// Set number of colors
$numberColors = 256;

// Set colorspace
$colorSpace = Imagick::COLORSPACE_SRGB;

// Set tree depth
$treeDepth = 0;

// Set dither
$dither = false;

// Set quantize
$im->quantizeImage($numberColors, $colorSpace, $treeDepth, $dither, false);

// write to disk
$im->writeImage( 'output.bmp' );
?>


Related Topics



Leave a reply



Submit