[英]How can I gzip images without using .htaccess?
Godaddy won't enable mod_gzip for me, so I have to use headers like this: Godaddy不会为我启用mod_gzip,因此我必须使用以下标头:
<?php
ob_start ("ob_gzhandler");
header("Content-type: text/css; charset: UTF-8");
header("Cache-Control: must-revalidate");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
$offset = 60 * 60 * 24 * 7;
$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
?>
In this case, that would be for CSS files 在这种情况下,这将适用于CSS文件
Anyways, how can I use that with my images? 无论如何,如何将其用于图像? I mean, making the image: image.php and putting that code at the top but using
header("Content-type: image/png; charset: UTF-8");
我的意思是,制作图像:image.php并将该代码放在顶部,但使用
header("Content-type: image/png; charset: UTF-8");
doesn't work so, let me know. 不起作用,请告诉我。
GZipping images is not worth it. 压缩图像不值得。 The shrinkage is minuscule or even negative, because image formats do a pretty optimal job compressing already.
缩小幅度很小,甚至是负的,因为图像格式已经在压缩方面做得非常好。 Try zipping a JPG file to see what I mean.
尝试压缩JPG文件以了解我的意思。
Also, compressing data that is hard to compress is extremely processor intensive and you may end up using more resources rather than less. 此外,压缩难以压缩的数据非常耗费处理器资源,您最终可能会使用更多而不是更少的资源。
The Yahoo performance rules confirm this: Yahoo性能规则确认了这一点:
Image and PDF files should not be gzipped because they are already compressed.
图片和PDF文件不应压缩,因为它们已经被压缩。 Trying to gzip them not only wastes CPU but can potentially increase file sizes.
尝试对它们进行gzip压缩不仅浪费CPU,而且可能会增加文件大小。
also, remember that when doing compression in PHP, a PHP process is started every time a request comes in. You could be better off not compressing static resources at all (unless you're paying a lot of money for traffic). 另外,请记住,使用PHP进行压缩时,每次请求进入时都会启动一个PHP进程。最好不要完全压缩静态资源(除非您为流量支付了很多钱)。 Compression should generally be the web server's job, not PHP's.
压缩通常应该是Web服务器的工作,而不是PHP。
In general, gzipping GIF,PNG or JPEG images will not yield gains over a couple percent (in fact, the "compressed" file may be larger than the original). 通常,gzip,压缩GIF,PNG或JPEG图像不会产生超过百分之几的收益(实际上,“压缩”文件可能比原始文件大)。 If you really want to do that in plain php, use mod_rewrite(or modified image URLs) to redirect the URLs to a php script.
如果您真的想用纯PHP做到这一点,请使用mod_rewrite(或修改的图像URL)将URL重定向到php脚本。 Then, outputting the image like
然后,输出像
<?php
header('Content-Type:image/png');
echo file_get_contents('img.php');
does work (in your experiments, you have probably inadvertently added spaces into a binary PNG image and thereby broken it). 确实有效(在您的实验中,您可能无意间在二进制PNG图像中添加了空格,从而破坏了它)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.