Method for automatically generating thumbnails based on url by php

  • 2021-07-21 07:52:29
  • OfStack

In this paper, the method of automatically generating thumbnails based on url by php is described, which is a very practical function. Share it for your reference. The specific methods are as follows:

Principle: Set apache rewrite, when the picture does not exist, call php to create a picture.

For example:

The path of the original image is: http://localhost/upload/news/2013/07/21/1. jpg
The thumbnail path is: http://localhost/supload/news/2013/07/21/1. jpg

When accessing http://localhost/supload/news/2013/07/21/1. jpg, the picture is displayed if it exists. Otherwise, call createthumb. php to generate the picture.

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/Point to www Directory

For usage of PicThumb.class.php, please see here: https://www.ofstack.com/article/55530. htm

Need to turn on apache rewrite:


sudo a2enmod rewrite 

The. htaccess file is as follows:


<IfModule mod_rewrite.c> 
RewriteEngine On 
 
# '-s' (is regular file, with size) 
# '-l' (is symbolic link) 
# '-d' (is directory) 
# 'ornext|OR' (or next condition) 
# 'nocase|NC' (no case) 
# 'last|L' (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>

The file createthumb. php is as follows:


<?php
define('WWW_PATH', dirname(dirname(__FILE__))); //  Site www Directory  
 
require(WWW_PATH.'/PicThumb.class.php'); // include PicThumb.class.php 
require(WWW_PATH.'/ThumbConfig.php');  // include ThumbConfig.php 
 
$logfile = WWW_PATH.'/createthumb.log'; //  Log file  
$source_path = WWW_PATH.'/upload/';   //  Original path  
$dest_path = WWW_PATH.'/supload/';    //  Target path  
 
$path = isset($_GET['path'])? $_GET['path'] : ''; //  Pictures accessed URL 
 
//  Check path 
if(!$path){ 
  exit(); 
} 
 
//  Get a picture URI 
$relative_url = str_replace($dest_path, '', WWW_PATH.$path); 
 
//  Get type 
$type = substr($relative_url, 0, strpos($relative_url, '/')); 
 
//  Get config 
$config = isset($thumb_config[$type])? $thumb_config[$type] : ''; 
 
//  Check config 
if(!$config || !isset($config['fromdir'])){ 
  exit(); 
} 
 
//  Original document  
$source = str_replace('/'.$type.'/', '/'.$config['fromdir'].'/', $source_path.$relative_url); 
 
//  Object file   
$dest = $dest_path.$relative_url; 
 
//  Create thumbnails  
$obj = new PicThumb($logfile); 
$obj->set_config($config); 
if($obj->create_thumb($source, $dest)){ 
  ob_clean(); 
  header('content-type:'.mime_content_type($dest)); 
  exit(file_get_contents($dest)); 
} 
 
?>

The ThumbConfig. php file is as follows:


<?php 
 
$thumb_config = array( 
 
  'news' => array( 
    'fromdir' => 'news', //  Source directory  
    'type' => 'fit', 
    'width' => 100, 
    'height' => 100, 
    'bgcolor' => '#FF0000' 
  ), 
 
  'news_1' => array( 
    'fromdir' => 'news', 
    'type' => 'fit', 
    'width' => 200, 
    'height' => 200, 
    'bgcolor' => '#FFFF00' 
  ), 
 
  'article' => array( 
    'fromdir' => 'article', 
    'type' => 'crop', 
    'width' => 250, 
    'height' => 250, 
    'watermark' => WWW_PATH.'/supload/watermark.png' 
  ) 
 
); 
 
?>

After accessing these three paths, press config to automatically generate thumbnails
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

The complete code of the example described in this article is downloaded here.

I hope this article is helpful to everyone's php programming.


Related articles: