PHP automatically generates thumbnails based on URL (source code attached)

WBOY
Release: 2016-07-25 08:55:28
Original
1439 people have browsed it
  1. sudo a2enmod rewrite
Copy the code

2, the content of the .htaccess file:

  1. RewriteEngine On
  2. # '-s' (is regular file, with size)
  3. # '-l' (is symbolic link)
  4. # '-d' (is directory)
  5. # 'ornext|OR' (or next condition)
  6. # 'nocase|NC' (no case)
  7. # 'last|L' (last rule)
  8. RewriteCond %{REQUEST_FILENAME} -s [OR]
  9. RewriteCond %{REQUEST_FILENAME} -l [OR]
  10. RewriteCond %{REQUEST_FILENAME} -d
  11. RewriteRule ^.*$ - [NC,L]
  12. RewriteRule ^.*$ createthumb.php?path=%{REQUEST_URI} [NC,L]
Copy code

For the configuration method of htaccess file, please refer to: Detailed explanation of apache .htaccess configuration .htaccess configuration detailed explanation (a comprehensive classic not to be missed) .htaccess file usage summary .htaccess tutorial.htaccess learning summary Example of Apache using .htaccess to configure pseudo-static configuration A few examples of htaccess pseudo-static rule configuration Discuss: htaccess URL rewrite and redirect redirect 3. createthumb.php

  1. define('WWW_PATH', dirname(dirname(__FILE__))); // Site www directory
  2. require(WWW_PATH.'/PicThumb.class.php'); // include PicThumb.class.php
  3. require(WWW_PATH.'/ThumbConfig.php'); // include ThumbConfig.php
  4. $logfile = WWW_PATH.'/createthumb.log'; // Log file
  5. $source_path = WWW_PATH.'/ upload/'; // Original path
  6. $dest_path = WWW_PATH.'/supload/'; // Target path
  7. $path = isset($_GET['path'])? $_GET['path'] : '' ; // Accessed image URL
  8. // Check path
  9. if(!$path){
  10. exit();
  11. }
  12. // Get image URI
  13. $relative_url = str_replace($dest_path, '', WWW_PATH.$ path);
  14. // Get type
  15. $type = substr($relative_url, 0, strpos($relative_url, '/'));
  16. // Get config
  17. $config = isset($thumb_config[$type]) ? $thumb_config[$type] : '';
  18. // Check config
  19. if(!$config || !isset($config['fromdir'])){
  20. exit();
  21. }
  22. // Original Image file
  23. $source = str_replace('/'.$type.'/', '/'.$config['fromdir'].'/', $source_path.$relative_url);
  24. // Target file
  25. $ dest = $dest_path.$relative_url;
  26. // Create thumbnail
  27. $obj = new PicThumb($logfile);
  28. $obj->set_config($config);
  29. if($obj->create_thumb($source , $dest)){
  30. ob_clean();
  31. header('content-type:'.mime_content_type($dest));
  32. exit(file_get_contents($dest));
  33. }
  34. ?>
Copy code

4,ThumbConfig.php

  1. $thumb_config = array(
  2. 'news' => array(
  3. 'fromdir' => 'news', // Source directory
  4. 'type' => 'fit ',
  5. 'width' => 100,
  6. 'height' => 100,
  7. 'bgcolor' => '#FF0000'
  8. ),
  9. 'news_1' => array(
  10. 'fromdir' => ; 'news',
  11. 'type' => 'fit',
  12. 'width' => 200,
  13. 'height' => 200,
  14. 'bgcolor' => '#FFFF00'
  15. ),
  16. 'article' => array(
  17. 'fromdir' => 'article',
  18. 'type' => 'crop',
  19. 'width' => 250,
  20. 'height' => 250,
  21. ' watermark' => WWW_PATH.'/supload/watermark.png'
  22. )
  23. );
  24. ?>
Copy code

For example, after accessing these three paths, thumbnails will be automatically generated by pressing 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

Attachment, php automatically generates the source code download address of the thumbnail according to the url



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!