PHP를 통해 URL을 기반으로 썸네일을 자동으로 생성하는 방법

jacklove
풀어 주다: 2023-03-31 11:00:01
원래의
1641명이 탐색했습니다.

원리: 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 사용법 여기를 참조하세요

아파치 재작성을 활성화해야 합니다

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
로그인 후 복사

이 글 URL에 따라 PHP를 사용하는 방법을 설명합니다. 썸네일 자동 생성 관련 내용 더 많은 관련 내용은 PHP 중국어 웹사이트를 참고하세요.

관련 권장 사항:

php 출력_버퍼링 캐시 사용 방법 소개

php를 통해 BigPipe 블록 출력을 구현하는 방법

fsockopen GET/POST를 사용하여 php를 통해 양식을 제출하고 파일을 업로드하는 방법

위 내용은 PHP를 통해 URL을 기반으로 썸네일을 자동으로 생성하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!