Detailed explanation of the use of PHP image processing class phpThumb

php中世界最好的语言
Release: 2023-03-27 08:34:02
Original
3227 people have browsed it

这次给大家带来PHP图片处理类phpThumb使用详解,PHP图片处理类phpThumb使用的注意事项有哪些,下面就是实战案例,一起来看一下。

phpThumb几个基本参数
一些有用的参数列一下:

src:目标图片的地址
w:输出图片的宽度
h:输出图片的高度(如果不指定他将按w参数等比缩放)
q:输出如果是JPG格式的,可以规定它的输出质量
bg:输出时的背景(如果需要)
sw、sh、sx、sy:局部输出,宽高、起始位置
f:输出格式,可以为jpeg、png、gif、ico
sfn:输出gif动画中的某一帧
fltr[]:滤镜,可以有很多效果,包括锐化、模糊、旋翻转、水印、边框、遮照、色彩调整等

官方例程:
http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php
使用 phpThumb 和 .htaccess 来缓存缩略图
原理:用户访问 your.com/thumbs/images/image.50×50.jpg 这样的网址,脚本生成 your.com/images/image.jpg 的缩略图,并且保存到 your.com/thumbs/images/image.50×50.jpg,下次访问就不用调 PHP 啦。
简介
大约一年以前我碰到了 phpThumb 这个牛掰的脚本,它是个用来缩放图片的开源项目。当然你可以用 GD2 或者 imagemagick(magickwand) 来干同样的事情,但 phpThumb 是专门干这个的。它用起来相当简单:

<img src="phpthumb/phpThumb.php?src=myimage.jpg&w=100&h=100">
Copy after login

如果访问量很大的话就撑不住了,因为 apache 要为每个图片的请求去调 PHP 来解析 phpThumb 的代码。尽管 phpThumb 自己有缓存,它还是要调 PHP 来决定是否从缓存里读。
我曾经看见有人用 mod_rewrite 把不存在的图片重定向到一个可以生成缩略图的脚本,以此来解决性能问题:
你需要:
Apache
mod_rewrite
PHP
这些东西通常虚拟主机都有,至于怎么安装就不在本文的讨论范围之内了。
OK,快告诉我怎么弄吧!
上传 phpThumb
从这里下载 phpThumb: http://phpthumb.sourceforge.net/ ,把它上传到 yoursite.com/phpthumb
配置 Mod_Rewrite
新建 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>
Copy after login

新建缩略图生成脚本:

新建 yoursite.com/thumbs/index.php

$thumb = $_GET[&#39;thumb&#39;]; 
if (!$thumb) { 
exit; 
} 
// 
$thumb_array = explode(&#39;.&#39;,$thumb); 
$image = &#39;../&#39;; 
foreach($thumb_array as $k=>$thumb_part){ 
if ($k != count($thumb_array)-2) { 
$image .= $thumb_part . &#39;.&#39;; 
} 
} 
$image = substr($image,0,-1); 
list($width,$height) = explode(&#39;x&#39;,$thumb_array[count($thumb_array)-2]); 
// 
if (file_exists($image)) { 
require(&#39;../phpthumb/phpthumb.class.php&#39;); 
$phpThumb = new phpThumb(); 
$phpThumb->setSourceFilename($image); 
$phpThumb->setParameter(&#39;w&#39;,$width); 
$phpThumb->setParameter(&#39;h&#39;,$height); 
//$phpThumb->setParameter(&#39;far&#39;,&#39;C&#39;); // scale outside 
//$phpThumb->setParameter(&#39;bg&#39;,&#39;<SPAN class=caps>FFFFFF</SPAN>&#39;); // scale outside 
if ($phpThumb->GenerateThumbnail()) { 
mkdir(dirname($thumb),0777,true); 
if ($phpThumb->RenderToFile($thumb)) { 
header(&#39;Location: /thumbs/&#39;.$thumb); 
exit; 
} 
} 
}
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

php图片裁剪与缩略图使用实例讲解

php preg_match匹配字符串长度案例解析

The above is the detailed content of Detailed explanation of the use of PHP image processing class phpThumb. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!