简体   繁体   English

如何不使用.htaccess压缩图片?

[英]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.

相关问题 htaccess,如何使用gzip压缩+隐藏.html扩展名? - htaccess, how can I use gzip compression + hide .html extensions? 如何使用.htaccess或php.ini使用gzip压缩来优化站点php文件? - How do I site optimization php files with gzip compression using .htaccess or php.ini? 使用 php 如何检查 GZIP 是否已打开? - Using php how can I check if GZIP is turned on? Gzip没有使用.htaccess在Apache中工作 - Gzip not working in Apache using .htaccess 如何使用.htaccess提供gziped字体? (没有mod gzip或deflate) - How to serve a gziped font using .htaccess? (no mod gzip or deflate) 如何在不使用Android中使用Base 64编码的字符串的情况下使用Web服务上载图像并将图像发送到服务器? - How can I upload and send Images to server using webservices without using Base 64 encoded string in Android? 如何在不使用开发工具包的情况下使用 cURL 删除 Amazon AWS 中的图像 - How can I delete images in Amazon AWS with cURL without using the SDK 我如何每月在服务器上运行php脚本而不运行cron作业。 例如使用.htaccess - How can i run a php script on a server every month without running a cron job. e.g. using .htaccess https和来自其他网站的图片。 如何在不修改.htaccess文件的情况下使用php或javascript删除http图像的警告? - https and images from another site. How to remove warnings for http images using php or javascript without modifying .htaccess file? 如何仅使用htaccess重写图像的URL - How to rewrite URLs using htaccess only for images
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM