Home >Backend Development >PHP Tutorial >How to automatically generate thumbnails based on url through php

How to automatically generate thumbnails based on url through php

jacklove
jackloveOriginal
2018-06-09 15:25:361693browse

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://localhost/upload/news/2013/07/21/1.jpg
The thumbnail image path is: http://localhost/supload/news/2013 /07/21/1.jpg
When accessing http://localhost/supload/news/2013/07/21/1.jpg, if the picture exists, the picture 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 ://localhost/ points to the www directory

PicThumb.class.php usage please check 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://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

This article explains how to automatically generate thumbnails based on url through php. For more related content, please pay attention to php Chinese net.

Related recommendations:

Introduction to the method of using php output_buffering cache

How to implement BigPipe block output through php

How to use fsockopen GET/POST to submit forms and upload files through php

The above is the detailed content of How to automatically generate thumbnails based on url through php. For more information, please follow other related articles on the PHP Chinese website!

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