Home  >  Article  >  Backend Development  >  PHP image processing class phpThumb parameter usage introduction_PHP tutorial

PHP image processing class phpThumb parameter usage introduction_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:19:48698browse

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 If not specified, it will be scaled according to the w parameter)
q: If the output is in JPG format, its output quality can be specified
bg: The background during output (if needed)
sw, sh, sx , sy: local output, width, height, starting position
f: output format, which can be jpeg, png, gif, ico
sfn: output a certain frame in the gif animation
fltr[]: filter Mirror can have 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, The script generates a thumbnail of your.com/images/image.jpg and saves 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 on 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
Create 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

Copy code The code is as follows:

$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','FFFFFF'); // 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.
Official website http://phpthumb.gxdlabs.com/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325252.htmlTechArticlephpThumb Several basic parameters and 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 not specified, it will be scaled down according to the w parameter...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn