Force imagejpeg to .png - php

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

Related

Use PHP to convert PNG to JPEG

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.

Converting PNG to JPEG file

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.

How can I convert a PNG file to JPEG file in php without creating a new file?

I know we can use imagecreatefrompng and imagejpeg functions to convert PNG to JPEG. But it creates a new file.
I just need file_get_contents() to store the file into database. That means I need JPEG file contents from my PNG file
eg:-
imagejpeg(imagecreatefrompng($_REQUEST['sig_data']), "test.jpeg");
$appSig = addslashes(file_get_contents("test.jpeg"));
Above example I need to create a 'test.jpeg' file. If I didn't pass that as an argument to imagejpeg function, it print the file in screen. And I can't do object buffering also. imagejpg function just return the status.
Admittedly somewhat hacky, but you can capture the direct output of imagejpeg:
ob_start();
imagejpeg($resource); // no file name
$imageData = ob_get_clean();

rendering exact jpeg file through php

I am using following code to render a JPEG file, which is on disk, through the imagejpeg function:
//.... some headers....
$image = imagecreatefromjpeg($the_file);
imagejpeg($image);
Now, problem is, imagejpeg compresses the image (reduces the quality in my case) and does not output the same file. I know I can control the quality through a parameter, but it seems that imagejpeg does jpeg encoding/decoding. I don't want that (seems a waste, as the file is already a jpeg file). I want a simple function that will just "RENDER" the image (which is what I thought imagejpeg did till now). Can someone tell what to use?
You could use the readfile() function, to read the file from the disk, and output its content -- without doing any manipulation / transformation on it.

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