Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php 根据url自动生成缩略图

php 根据url自动生成缩略图

黄舟
黄舟asal
2017-02-18 09:19:461289semak imbas

原理:设置apache rewrite ,当图片不存在时,调用php创建图片。
例如
原图路径为:http://www.php.cn/
缩略图路径为:http://www.php.cn/

当访问 http://www.php.cn/ 时,如图片存在,则显示图片。否则,调用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://www.php.cn/ 指向 www目录

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

需要开启apache rewrite

sudo a2enmod rewrite

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On

# &#39;-s&#39; (is regular file, with size)
# &#39;-l&#39; (is symbolic link)
# &#39;-d&#39; (is directory)
# &#39;ornext|OR&#39; (or next condition)
# &#39;nocase|NC&#39; (no case)
# &#39;last|L&#39; (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(&#39;WWW_PATH&#39;, dirname(dirname(__FILE__))); // 站点www目录

require(WWW_PATH.&#39;/PicThumb.class.php&#39;); // include PicThumb.class.php
require(WWW_PATH.&#39;/ThumbConfig.php&#39;);    // include ThumbConfig.php

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

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

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

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

// 获取type
$type = substr($relative_url, 0, strpos($relative_url, &#39;/&#39;));

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

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

// 原图文件
$source = str_replace(&#39;/&#39;.$type.&#39;/&#39;, &#39;/&#39;.$config[&#39;fromdir&#39;].&#39;/&#39;, $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(&#39;content-type:&#39;.mime_content_type($dest));
    exit(file_get_contents($dest));
}

?>

ThumbConfig.php

<?php

$thumb_config = array(

    &#39;news&#39; => array(
        &#39;fromdir&#39; => &#39;news&#39;, // 来源目录
        &#39;type&#39; => &#39;fit&#39;,
        &#39;width&#39; => 100,
        &#39;height&#39; => 100,
        &#39;bgcolor&#39; => &#39;#FF0000&#39;
    ),

    &#39;news_1&#39; => array(
        &#39;fromdir&#39; => &#39;news&#39;,
        &#39;type&#39; => &#39;fit&#39;,
        &#39;width&#39; => 200,
        &#39;height&#39; => 200,
        &#39;bgcolor&#39; => &#39;#FFFF00&#39;
    ),

    &#39;article&#39; => array(
        &#39;fromdir&#39; => &#39;article&#39;,
        &#39;type&#39; => &#39;crop&#39;,
        &#39;width&#39; => 250,
        &#39;height&#39; => 250,
        &#39;watermark&#39; => WWW_PATH.&#39;/supload/watermark.png&#39;
    )

);

?>
访问这三个路径后会按config自动生成缩略图
http://www.php.cn/
http://localhost/supload/news_1/2013/07/21/1.jpg
http://www.php.cn/

 以上就是php 根据url自动生成缩略图 的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn