php 根据url自动生成缩略图

2023-05-16

原理:设置apache rewrite ,当图片不存在时,调用php创建图片。
例如
原图路径为:http://localhost/upload/news/2013/07/21/1.jpg
缩略图路径为:http://localhost/supload/news/2013/07/21/1.jpg

当访问 http://localhost/supload/news/2013/07/21/1.jpg 时,如图片存在,则显示图片。否则,调用createthumb.php生成图片。

目录结构如下:

www/PicThumb.class.php
www/ThumbConfig.php
www/upload/news/2013/07/21/1.jpg
www/upload/article/2013/07/21/2.jpg
www/supload/.htaccess
www/supload/watermark.png
www/supload/createthumb.php

http://localhost/ 指向 www目录

PicThumb.class.php 用法请查看这里

需要开启apache rewrite

sudo a2enmod rewrite
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine 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]
RewriteRule ^.*$ createthumb.php?path=%{REQUEST_URI} [NC,L]

</IfModule>
createthumb.php
<?php
define('WWW_PATH', dirname(dirname(__FILE__))); // 站点www目录

require(WWW_PATH.'/PicThumb.class.php'); // include PicThumb.class.php
require(WWW_PATH.'/ThumbConfig.php');    // include ThumbConfig.php

$logfile = WWW_PATH.'/createthumb.log';  // 日志文件
$source_path = WWW_PATH.'/upload/';      // 原路径
$dest_path = WWW_PATH.'/supload/';       // 目标路径

$path = isset($_GET['path'])? $_GET['path'] : '';  // 访问的图片URL

// 检查path
if(!$path){
    exit();
}

// 获取图片URI
$relative_url = str_replace($dest_path, '', WWW_PATH.$path);

// 获取type
$type = substr($relative_url, 0, strpos($relative_url, '/'));

// 获取config
$config = isset($thumb_config[$type])? $thumb_config[$type] : '';

// 检查config
if(!$config || !isset($config['fromdir'])){
    exit();
}

// 原图文件
$source = str_replace('/'.$type.'/', '/'.$config['fromdir'].'/', $source_path.$relative_url);

// 目标文件 
$dest = $dest_path.$relative_url;

// 创建缩略图
$obj = new PicThumb($logfile);
$obj->set_config($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(
        'fromdir' => 'news',
        'type' => 'fit',
        'width' => 200,
        'height' => 200,
        'bgcolor' => '#FFFF00'
    ),

    'article' => array(
        'fromdir' => 'article',
        'type' => 'crop',
        'width' => 250,
        'height' => 250,
        'watermark' => WWW_PATH.'/supload/watermark.png'
    )

);

?>
访问这三个路径后会按config自动生成缩略图
http://localhost/supload/news/2013/07/21/1.jpg
http://localhost/supload/news_1/2013/07/21/1.jpg
http://localhost/supload/article/2013/07/21/2.jpg
源码下载地址:点击下载

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

php 根据url自动生成缩略图 的相关文章

随机推荐

  • 冒泡,二分法插入,快速排序算法

    1 冒泡排序算法 过程 xff1a 1 遍历整个数组 xff0c 每两两相邻的元素进行比较 xff0c 如 a i gt a i 43 1 则互换位置 xff0c 每次比较消除一个逆序 2 每一次循环后 xff0c 下次再需要循环的次数减少
  • PHP缩小png图片,保留透明色方法

    将图片缩成合适的尺寸 xff0c jpg图片缩小比较容易 xff0c png图片如果带了透明色 xff0c 按jpg方式来缩小 xff0c 会造成透明色丢失 保存透明色主要利用gd库的两个方法 xff1a imagecolorallocat
  • Ubuntu的常用快捷键

    Ubuntu Gnome的桌面技巧 xff1a 61 61 61 键盘类 61 61 61 1 先同时按住 Alt 43 Ctrl 键 xff0c 再按键盘上的左 右光标 键 可以切换工作区 2 web时按 键 等于 查找功能 桌面或者目录
  • 无人驾驶虚拟仿真(四)--通过ROS系统控制小车行走

    简介 xff1a 实现键盘控制虚拟仿真小车移动 xff0c w s a d 空格 xff0c 对应向前 向后 向左 向右 急停切换功能 xff0c q键退出 1 创建key control节点 进入工作空间源码目录 xff1a cd myr
  • error while loading shared libraries的解決方法

    error while loading shared libraries的解決方法 运行程式時 xff0c 如遇到像下列這種錯誤 xff1a tests error while loading shared libraries xxx so
  • imagemagick安装方法

    1 下载ImageMagick http www imagemagick org download 下载 ImageMagick 6 8 5 10 tar gz xff0c 下载完毕后开始进行安装 cd Downloads tar xzvf
  • ubuntu中安装apache ab命令进行简单压力测试

    1 安裝ab命令 sudo apt get install apache2 utils 2 ab命令参数说明 Usage ab options http s hostname port path Options are 总的请求数 n re
  • 如何查看当前Apache的连接数

    查看了连接数和当前的连接数 netstat ant grep ip 80 wc l netstat ant grep ip 80 grep EST wc l 查看IP访问次数 netstat nat grep 34 80 34 awk 39
  • php 获取页面中的指定内容类

    功能 xff1a 1 获取内容中的url xff0c email xff0c image 2 替换内容中的url xff0c email xff0c image url xff1a lt a href 61 34 url 34 gt xxx
  • memcached启动参数

    memcached启动参数 p 指定端口号 xff08 默认11211 xff09 U lt num gt UDP监听端口 默认 11211 0 时关闭 s lt file gt 用于监听的UNIX套接字路径 xff08 禁用网络支持 xf
  • mysql常用方法

    1 CONCAT str1 str2 mysql gt SELECT CONCAT 39 My 39 39 S 39 39 QL 39 gt 39 MySQL 39 mysql gt SELECT CONCAT 39 My 39 NULL
  • shell 监控网站是否异常的脚本

    shell 监控网站是否异常的脚本 xff0c 如有异常自动发电邮通知管理员 流程 xff1a 1 检查网站返回的http code 是否等于200 xff0c 如不是200视为异常 2 检查网站的访问时间 xff0c 超过MAXLOADT
  • 文件转base64输出

    Data URI scheme是在RFC2397中定义的 xff0c 目的是将一些小的数据 xff0c 直接嵌入到网页中 xff0c 从而不用再从外部文件载入 优点 xff1a 减少http连接数 缺点 xff1a 这种格式的文件不会被浏览
  • php 支持断点续传的文件下载类

    php 支持断点续传 xff0c 主要依靠HTTP协议中 header HTTP RANGE实现 HTTP断点续传原理 Http头 Range Content Range HTTP头中一般断点下载时才用到Range和Content Rang
  • 基于Linkit 7697的红绿灯控制系统

    1 硬件准备 LinkIt 7697 1 xff0c 继电器模块 1 xff0c 面包板 1 xff0c RGB LED灯 1 xff08 共阳极 xff0c 工作电流20mA xff0c 红灯压降2 2 2V xff0c 绿灯蓝灯压降3
  • shell 记录apache status并自动更新到数据库

    1 获取apache status monitor log sh bin bash 连接数 site connects 61 netstat ant grep ip 80 wc l 当前连接数 site cur connects 61 ne
  • php 缩略图生成类,支持imagemagick及gd库两种处理

    功能 1 按比例缩小 放大 2 填充背景色 3 按区域裁剪 4 添加水印 包括水印的位置 透明度等 使用imagemagick GD库实现 imagemagick地址 www imagemagick org 需要安装imagemagick
  • php5.3 中显示Deprecated: Assigning the return value of new by reference is deprecated in 的解决方法

    今天需要将某个网站般去另一台服务器 设置好运行 xff0c 显示一大堆Deprecated Deprecated Assigning the return value of new by reference is deprecated in
  • 使用apache mod_env模块保存php程序敏感信息

    Apache模块 mod env 说明 xff1a 允许Apache修改或清除传送到CGI脚本和SSI页面的环境变量 模块名 xff1a env module 源文件 xff1a mod env c 本模块用于控制传送给CGI脚本和SSI页
  • php 根据url自动生成缩略图

    原理 xff1a 设置apache rewrite xff0c 当图片不存在时 xff0c 调用php创建图片 例如 原图路径为 xff1a http localhost upload news 2013 07 21 1 jpg 缩略图路径