Home >Backend Development >PHP Tutorial >php automatically generates thumbnails based on url

php automatically generates thumbnails based on url

黄舟
黄舟Original
2017-02-18 09:19:461373browse

Principle: Set apache rewrite, and when the picture does not exist, call php to create the picture.
For example
The original image path is: http://www.php.cn/
The thumbnail path is: http://www.php.cn/

When When accessing http://www.php.cn/, if the image exists, the image will be displayed. Otherwise, call createthumb.php to generate the image.

The directory structure is as follows:

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/ points to the www directory

PicThumb.class.php usage please see here

needs to enable 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/

The above is the content of php automatically generating thumbnails based on the url. For more related content, please Follow the PHP Chinese website (www.php.cn)!


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