如何通过php 根据url自动生成缩略图

jacklove
Lepaskan: 2023-03-31 11:00:01
asal
1641 orang telah melayarinya

原理:设置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
Salin selepas log masuk

.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>
Salin selepas log masuk

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));
}

?>
Salin selepas log masuk

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;
    )
);

?>
Salin selepas log masuk
访问这三个路径后会按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
Salin selepas log masuk

本文讲解了如何通过php 根据url自动生成缩略图的相关内容,更多相关内容请关注php中文网。

相关推荐:

介绍php output_buffering 缓存使用的方法

如何通过php 实现BigPipe分块输出

如何通过php 利用fsockopen GET/POST 提交表单及上传文件

Atas ialah kandungan terperinci 如何通过php 根据url自动生成缩略图. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
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
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!