ホームページ  >  記事  >  バックエンド開発  >  PHP は URL に基づいてサムネイル実装コードを自動的に生成します

PHP は URL に基づいてサムネイル実装コードを自動的に生成します

WBOY
WBOYオリジナル
2016-07-25 08:52:31810ブラウズ
  1. RewriteEngine On
  2. # '-s' (通常のファイル、サイズあり)
  3. # '-l' (シンボリックリンク)
  4. # '-d' (ディレクトリ)
  5. # 'ornext|OR' (または次の条件) )
  6. # 'nocase|NC' (大文字小文字なし)
  7. # 'last|L' (最後のルール)
  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]
复制代

2、createthumb.php文件:

  1. define('WWW_PATH', dirname(dirname(__FILE__))); // 站点www目录
  2. require(WWW_PATH.'/PicThumb.class.php'); // PicThumb.class.php をインクルードします
  3. require(WWW_PATH.'/ThumbConfig.php'); // ThumbConfig.php をインクルードします
  4. $logfile = WWW_PATH.'/createthumb.log'; // 日志文件
  5. $source_path = WWW_PATH.'/upload/'; // 原路径
  6. $dest_path = WWW_PATH.'/supload/'; // 目标路径
  7. $path = isset($_GET['path'])? $_GET['パス'] : ''; // 访问の画像URL
  8. // 检查path
  9. if(!$path){
  10. exit();
  11. }
  12. // 取得图片URI
  13. $relative_url = str_replace($dest_path, '', WWW_PATH.$path);
  14. // 获取type
  15. $type = substr($relative_url, 0, strpos($relative_url, '/'));
  16. // 获取config
  17. $config = isset($thumb_config[$type])? $thumb_config[$type] : '';
  18. // 检查config
  19. if(!$config || !isset($config['fromdir'])){
  20. exit();
  21. }
  22. // 原文文例
  23. $source = str_replace('/'.$type.'/', '/'.$config['fromdir'].'/', $source_path.$relative_url);
  24. // 目标文件
  25. $dest = $dest_path.$relative_url;
  26. // 创建缩略图
  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. ?>
复制代

3、ThumbConfig.php文件:

  1. $thumb_config = array(
  2. 'news' => array(
  3. 'fromdir' => 'news', // 来源目录
  4. 'type' => 'fit ',
  5. '幅' => 100,
  6. 'bgcolor' => '#FF0000'
  7. ),
  8. 'news_1' => ; 'ニュース'、
  9. 'タイプ' => '幅' =>
  10. 'bgcolor' =>
  11. 'article' => array(
  12. 'fromdir' => 'article',
  13. 'type' => 'crop',
  14. 'width' => 250,
  15. 'height' => 250,
  16. 'ウォーターマーク' => WWW_PATH.'/supload/watermark.png'
  17. )
  18. );
  19. ?>
  20. 复制代
  21. 访问以下三个路径后会按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

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。