Introduction to the usage of PHP image processing class phpThumb parameters

  • 2020-05-16 06:29:53
  • OfStack

phpThumb has several basic parameters
1 some useful parameters are listed below:
src: the address of the target image
w: the width of the output image
h: the height of the output image (if it is not specified, it will scale according to the w parameter)
q: if the output is in JPG format, you can specify its output quality
bg: background when output (if required)
sw, sh, sx, sy: local output, width, height, starting position
f: output format, which can be jpeg, png, gif, ico
sfn: outputs a frame from the gif animation
fltr[] : filter can have many effects, including sharpening, blurring, turning, watermarking, border, shading, color adjustment, etc
See the official routine for more:
http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php
Use phpThumb and.htaccess to cache thumbnails
Principle: user access your com/thumbs/images/image. 50 x 50. jpg such sites, the script generated your. com/images/image jpg thumbnails, and save to your. com/thumbs/images/image. 50 x 50. jpg, access will be without adjustable PHP next time.
Introduction to the
About a year ago I came across phpThumb, an awesome script that is an open source project for scaling images. Of course you can do the same thing with GD2 or imagemagick(magickwand), but phpThumb is specifically for this. It's pretty simple to use:
< img src="phpthumb/phpThumb.php?src=myimage.jpg&w=100&h=100" >
If you have a lot of traffic, you won't be able to support it, because apache has to call PHP for each image request to parse phpThumb's code. Although phpThumb has its own cache, it calls PHP to decide whether to read from the cache.
I've seen someone use mod_rewrite to redirect a nonexistent image to a script that generates thumbnails to solve a performance problem:
You need to:
Apache
mod_rewrite
PHP
These things are usually available on virtual hosts, and how to install them is beyond the scope of this article.
OK, tell me how to do it!
Upload phpThumb
From here to download phpThumb: http: / / phpthumb sourceforge. net /, put it onto yoursite. com/phpthumb
Configuration Mod_Rewrite
New yoursite. com thumbs /. htaccess:
< IfModule mod_rewrite.c >
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?thumb=$1 [L,QSA]
< /IfModule >
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 1!
Upload 1 image to yoursite. com/images/myimage jpg
. Open your browser, visit yoursite com/thumbs/images/myimage. 100 x 100. jpg
Check the thumbs directory. There should be a thumbnail there. Don't call PHP for your next visit.
The official website of http: / / phpthumb. gxdlabs. com /

Related articles: