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)
?>
Related
I have a doubt. Is there a problem if I force imagejpeg to save a .png file?
Like in this example:
imagejpeg($img,$_SERVER['DOCUMENT_ROOT']."/test.png",80);
I want to use this method because I can use the $quality filter.
In this method I have a saved .png file for about 25kb, but if I use imagepng my image is for about 200kb (I used the $quality level from 0-9 but I didn't saw any changes, only -20 kb).
I don't want to make a mistake because my website is generating .png images every second.
Method 2.
I tried to compress the .png images with pngquant but I have no idea how to do it when I am using imagepng function.
I tried something like this, but It doesn't work if I pass the image to the function.
ob_start();
imagepng($img);
$png = ob_get_clean();
file_put_contents($_SERVER['DOCUMENT_ROOT']."/test.png", compress($png);
function compresss($img)
{
//...
}
In other cases if I have $png = $_FILES['file']['tmp_name'] is working. So is there a way to compress with pngquant, or is there a problem if I force imagejpeg to save a .png file?
Your forcing just the filename of the created file. The resulting file will be jpg with all its properties. You will not get a compressed png file if you use the imagejpeg() method.
You need to understand the difference of the both formats. While jpg is a compressed format which loses image information when compressing, png is a lossless format. png also has a compression level, but since no image informatio is destroyed, it will be bigger as an jpg file.
If you use imagepng($image, $filename, 9) you get a png file with the best compression.
You should use imagepng() instead of imagejpeg() to create a png file.
http://php.net/manual/fr/function.imagepng.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...
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;
I have a website where users have been uploading a bunch of high quality PNG files. I want to use PHP to convert them to JPEG and re-size them to make them smaller in file size.
How can I do this when they upload the file? What is the process for doing this? Is a new image created or is it edited?
Thanks
You can use something like this :
function pngTojpg($pngImage, $outputPngFile, $outputJpgFile, $quality) {
$image = imagecreatefrompng($pngImage);
//Save the png image
imagepng($image, $outputPngFile);
//Save the jpeg image
imagejpeg($image, $outputJpgFile, $quality);
// Free up memory
imagedestroy($image);
}
"quality is optional, and ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75)"
The php doc : imagejpeg, imagecreatefrompng
These functions are from the GD library, here the installation instruction : Php GD
Use ImageMagick to do all kinds of conversions. You should be able to find examples at this link:
http://www.php.net/manual/en/book.imagick.php
Just try ImageMagick:
http://www.imagemagick.org/script/convert.php
I think, that is what you are looking for.
Well, you can use simple php code to do that but I use and recomend this library to work with images:
Verot - Class Upload http://www.verot.net/php_class_upload.htm
You can convert images to other format, reduce size, transform and do a lot others stuffs.
I have a script to convert PNG files to JPEG files. Except, I'm not exactly sure how it works. What do use for $outputPngFile and $outputJpgFile? Can I do this with a tmp file, like when the user is uploading it? Then, how do I access the new file to move it to the proper image directory?
function pngTojpg($image, $outputPngFile, $outputJpgFile, $quality) {
$image = imagecreatefrompng($image);
//Save the png image
imagepng($image, $outputPngFile);
//Save the jpeg image
imagejpeg($image, $outputJpgFile, $quality);
// Free up memory
imagedestroy($image);
}
<?php
$image = imagecreatefrompng('yourlocation/image.png');
imagejpeg($image, 'yournewlocation/image.jpg', 70);
imagedestroy($image);
?>
It would probably help you to know that you're using the GD library that has been bundled with PHP.
What the function is doing is taking a path to a png image ($image), loading it into a GD resource that can be manipulated within PHP (imagecreatefrompng), saving the image as a png to the png output path ($outputPngFile), then saving the image as a jpg to the jpg output path ($outputJpgFile) with a particular compression factor ($quality), and finally destroying the image resource object, since it isn't needed anymore.
Since it's saving the image as a png as well, the function was obviously intended to be used to save an image from either an external source (given by a URL) or temporary files from a user upload. You can do either, PHP doesn't care so long as the path you provide to the image file is valid.