Several basic parameters of phpThumb
List some useful parameters:
src: The address of the target image
w: The width of the output image
h: The height of the output image (if you do not specify it Will be scaled according to the w parameter)
q: If the output is in JPG format, you can specify its output quality
bg: The background during output (if necessary)
sw, sh, sx, sy: Partial output, width, height, starting position
f: Output format, can be jpeg, png, gif, ico
sfn: Output a certain frame in the gif animation
fltr[]: Filter, can There are many effects, including sharpening, blurring, rotation, watermark, border, masking, color adjustment, etc.
For more effects, please refer to the official routine:
http://phpthumb.sourceforge.net/demo/ demo/phpThumb.demo.demo.php
Use phpThumb and .htaccess to cache thumbnails
Principle: The user visits a URL like your.com/thumbs/images/image.50×50.jpg, and the script generates your .com/images/image.jpg thumbnail and save it to your.com/thumbs/images/image.50×50.jpg. You don’t need to adjust PHP next time you visit.
Introduction
About a year ago I came across this awesome script called phpThumb, an open source project for scaling images. Of course you can use GD2 or imagemagick(magickwand) to do the same thing, but phpThumb is designed for this. It is quite simple to use:
If the number of visits is large, it will not be able to sustain it, because apache It is necessary to adjust PHP to parse the phpThumb code for each image request. Even though phpThumb has its own cache, it still has to call PHP to decide whether to read from the cache.
I once saw someone use mod_rewrite to redirect non-existent images to a script that can generate thumbnails to solve performance problems:
You need:
Apache
mod_rewrite
PHP
These things are usually available in virtual hosts. How to install them is beyond the scope of this article.
OK, tell me how to do it!
Upload phpThumb
Download phpThumb from here: http://phpthumb.sourceforge.net/ and upload it to yoursite.com/phpthumb
Configure Mod_Rewrite
New yoursite.com/thumbs/.htaccess :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index. php?thumb=$1 [L,QSA]
New thumbnail generation script:
New yoursite.com/thumbs/index.php
$thumb = $_GET['thumb']; if (!$thumb) { exit; } // $thumb_array = explode('.',$thumb); $image = '../'; foreach($thumb_array as $k=>$thumb_part){ if ($k != count($thumb_array)-2) { $image .= $thumb_part . '.'; } } $image = substr($image,0,-1); list($width,$height) = explode('x',$thumb_array[count($thumb_array)-2]); // if (file_exists($image)) { require('../phpthumb/phpthumb.class.php'); $phpThumb = new phpThumb(); $phpThumb->setSourceFilename($image); $phpThumb->setParameter('w',$width); $phpThumb->setParameter('h',$height); //$phpThumb->setParameter('far','C'); // scale outside //$phpThumb->setParameter('bg','<SPAN class=caps>FFFFFF</SPAN>'); // scale outside if ($phpThumb->GenerateThumbnail()) { mkdir(dirname($thumb),0777,true); if ($phpThumb->RenderToFile($thumb)) { header('Location: /thumbs/'.$thumb); exit; } } }
Test it!
Upload an image to yoursite.com/images/myimage.jpg
Open your browser and visit yoursite.com/thumbs/images/myimage.100×100.jpg
Check the thumbs directory, it should There's a thumbnail there. There is no need to adjust PHP next time you visit.
For more PHP image processing classes, please pay attention to the PHP Chinese website for related articles introducing the usage of phpThumb parameters!