《php实现依据url自动生成缩略图的方法__2.docx》由会员分享,可在线阅读,更多相关《php实现依据url自动生成缩略图的方法__2.docx(6页珍藏版)》请在三一文库上搜索。
1、php实现依据url自动生成缩略图的方法_ 本文实例讲解并描述了php实现依据url自动生成缩略图的方法,是特别有用的功能。分享给大家供大家参考。具体方法如下: 原理 :设置apache rewrite ,当图片不存在时,调用php创建图片。 例如: 原图路径为:http:/localhost/upload/news/2021/07/21/1.jpg 缩略图路径为:http:/localhost/supload/news/2021/07/21/1.jpg 当访问 http:/localhost/supload/news/2021/07/21/1.jpg 时,如图片存在,则显示图片。否则,调用c
2、reatethumb.php生成图片。 名目结构 如下: /PicThumb.class.php /ThumbConfig.php /upload/news/2021/07/21/1.jpg /upload/article/2021/07/21/2.jpg /supload/.htaccess /supload/watermark.png /supload/createthumb.php http:/localhost/ 指向 名目 需要开启apache rewrite: sudo a2enmod rewrite .htaccess文件 如下: IfModule mod_rewrite.c R
3、ewriteEngine On # -s (is regular file, with size) # -l (is symbolic link) # -d (is directory) # ornext|OR (or next condition) # nocase|NC (no case) # last|L (last rule) RewriteCond %REQUEST_FILENAME -s OR RewriteCond %REQUEST_FILENAME -l OR RewriteCond %REQUEST_FILENAME -d RewriteRule .*$ - NC,L Rew
4、riteRule .*$ createthumb.php?path=%REQUEST_URI NC,L /IfModule createthumb.php文件如下: ?php define(_PATH, dirname(dirname(_FILE_); / 站点名目 require(_PATH./PicThumb.class.php); / include PicThumb.class.php require(_PATH./ThumbConfig.php); / include ThumbConfig.php $logfile = _PATH./createthumb.log; / 日志文件
5、$source_path = _PATH./upload/; / 原路径 $dest_path = _PATH./supload/; / 目标路径 $path = isset($_GETpath)? $_GETpath : ; / 访问的图片URL / 检查path if(!$path) exit(); / 猎取图片URI $relative_url = str_replace($dest_path, , _PATH.$path); / 猎取type $type = substr($relative_url, 0, strpos($relative_url, /); / 猎取config $c
6、onfig = isset($thumb_config$type)? $thumb_config$type : ; / 检查config if(!$config | !isset($configfromdir) exit(); / 原图文件 $source = str_replace(/.$type./, /.$configfromdir./, $source_path.$relative_url); / 目标文件 $dest = $dest_path.$relative_url; / 创建缩略图 $obj = new PicThumb($logfile); $obj-set_config($
7、config); if($obj-create_thumb($source, $dest) ob_clean(); header(content-type:.mime_content_type($dest); exit(file_get_contents($dest); ? ThumbConfig.php文件如下: ?php $thumb_config = array( news = array( fromdir = news, / 来源名目 type = fit, width = 100, height = 100, bgcolor = #FF0000 ), news_1 = array(
8、fromdir = news, type = fit, width = 200, height = 200, bgcolor = #FFFF00 ), article = array( fromdir = article, type = crop, width = 250, height = 250, watermark = _PATH./supload/watermark.png ) ); ? 访问这三个路径后会按config自动生成缩略图 http:/localhost/supload/news/2021/07/21/1.jpg http:/localhost/supload/news_1/2021/07/21/1.jpg http:/localhost/supload/article/2021/07/21/2.jpg 盼望本文所述对大家的php程序设计有所关心。 更多信息请查看IT技术专栏 .