1

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?

Winter
  • 3,894
  • 7
  • 24
  • 56
paulv
  • 83
  • 2
  • 12
  • There's no particular problem saving a JPEG with the extension `png`, but why not just use the extension `jpg` instead? Just giving it a different file extension doesn't change what type of file you're creating. – iainn Sep 06 '16 at 13:55
  • Using imagejpeg always make a JPEG file - even if you name it blabla.doc . The size is much smaller because of the low quality. – Klaus F. Sep 06 '16 at 13:56
  • 1
    The problem is PNG can only be compressed so much due to the specifics of the format. If you want a smaller size you need to use JPEG with everything that implies. – apokryfos Sep 06 '16 at 14:00
  • The problem is that I already have for about 20milions images hosted and I can't add the images into my db. Now the src of a image is something like this img src = $url.".png" so I need to save only .png (I don't want to add the extensions into my db with also 20mil rows) – paulv Sep 06 '16 at 14:03
  • @ Klaus Gf. If I forced imagejpeg to save a .png file, my image is .png. I don't see any .jpg extension. I wonder if there are any problems. – paulv Sep 06 '16 at 14:07
  • 1
    The problem is that this is not a png file but a jpg file with a png extension. The file has all properties of a jpg file but the name. – Philipp Palmtag Sep 06 '16 at 14:09
  • Yes, but I see that is readable like a .png file. So there should not be any problems? Also transparency is working. I've tested and I can show the image src like ='img.png' and is working. – paulv Sep 06 '16 at 14:13
  • I think I would call it "bad style" ;). And it is possible that you run into problems with MIME Types / Headers, because the file you are sending seems to be png but is jpg instead. – Philipp Palmtag Sep 06 '16 at 14:21

2 Answers2

2

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.

Philipp Palmtag
  • 1,310
  • 2
  • 16
  • 18
  • I understand now. Thank you! As I said, if I use imagepng with the $quality filter I get for about -20kb only. I need more, because my website is generating for about 50g / day of images. What do you say about Method 2? I see that pngquant is a very good solution but it doesn't work if I created the function like above. Is there a way to create imagepng like tmp_file ? So I can send it to pngquant function? – paulv Sep 06 '16 at 14:34
1

You should use imagepng() instead of imagejpeg() to create a png file.

http://php.net/manual/fr/function.imagepng.php

Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49