简体   繁体   English

将imagejpeg强制为.png

[英]Force imagejpeg to .png

I have a doubt. 我有个疑问。 Is there a problem if I force imagejpeg to save a .png file? 如果我强制imagejpeg保存.png文件是否存在问题? 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. 我想使用此方法,因为我可以使用$quality过滤器。 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). 在这种方法中,我保存了约25kb的.png文件,但是如果我使用imagepng,则我的图像约为200kb(我使用0-9的$ quality级别,但没有看到任何更改,只有-20 kb) 。 I don't want to make a mistake because my website is generating .png images every second. 我不想犯错,因为我的网站每秒都会生成.png图像。

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. 方法2。我试图用pngquant压缩.png图像,但是我不知道当我使用imagepng函数时该怎么做。 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. 在其他情况下,如果我有$png = $_FILES['file']['tmp_name']有效。 So is there a way to compress with pngquant, or is there a problem if I force imagejpeg to save a .png file? 那么有没有办法用pngquant压缩,或者如果我强制imagejpeg保存.png文件有问题吗?

You should use imagepng() instead of imagejpeg() to create a png file. 您应该使用imagepng()而不是imagejpeg()创建png文件。

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

Your forcing just the filename of the created file. 您仅强制创建文件的文件名。 The resulting file will be jpg with all its properties. 生成的文件将是具有其所有属性的jpg。 You will not get a compressed png file if you use the imagejpeg() method. 如果使用imagejpeg()方法,将不会获得压缩的png文件。

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. jpg是一种压缩格式,在压缩时会丢失图像信息,而png是无损格式。 png also has a compression level, but since no image informatio is destroyed, it will be bigger as an jpg file. png也具有压缩级别,但是由于没有图像信息被破坏,因此它将作为jpg文件更大。

If you use imagepng($image, $filename, 9) you get a png file with the best compression. 如果使用imagepng($image, $filename, 9)则会得到压缩效果最好的png文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM