PHP Image Optimization
I need some ways to dynamically optimize images using PHP. Samp开发者_如何学JAVAle codes or a module would be very helpful. Thanks.
you can use thimthumb.php
go to your .htaccess
than add
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*\.(png|jpg))$ /timthumb.php?q=100&src=$1
</IfModule>
"q=100" is for the quality you can set it betwen 0-100 than creat a new file "img.php" in the home directory eg: www.your-website.com/timthumb.php
http://timthumb.googlecode.com/svn/trunk/timthumb.php
than find this
// Get original width and height
$width = imagesx ($image);
$height = imagesy ($image);
$origin_x = 0;
$origin_y = 0;
// generate new w/h if not provided
if ($new_width && !$new_height) {
$new_height = floor ($height * ($new_width / $width));
} else if ($new_height && !$new_width) {
$new_width = floor ($width * ($new_height / $height));
}
and replace it with this
// Get original width and height
$width = imagesx ($image);
$height = imagesy ($image);
$origin_x = 0;
$origin_y = 0;
// don't allow new width or height to be greater than the original
if( $new_width > $width ) {
$new_width = $width;
}
if( $new_height > $height ) {
$new_height = $height;
}
// generate new w/h if not provided
if ($new_width && !$new_height) {
$new_height = floor ($height * ($new_width / $width));
} else if ($new_height && !$new_width) {
$new_width = floor ($width * ($new_height / $height));
}
so when you go to www.your-website.com/images/pict.png it's like your loading www.your-website.com/img.php?q=100&src=/images/pict.png hope you like this! & sorry if i couldn't explaing well because my english is not good. :D
What do you mean by optimize? If you wish to keep the dimensions but reduce the size, take a look at imageJpeg for an example, and specifically the third argument (quality
). Consider 100
to be perfect quality, and start decreasing it to find an optimal balance between quality and size.
very simple one is here too: just change quality value (60) from 1 to 100, as you wish.
after execution of script, look at the filesize, and thats way, define your optimal quality value.
source
精彩评论