php根據url自動產生縮圖實現代碼

來源:互聯網
上載者:User
  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]
複製代碼

2、createthumb.php檔案:

  1. define('WWW_PATH', dirname(dirname(__FILE__))); // 網站www目錄
  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'; // 記錄檔
  5. $source_path = WWW_PATH.'/upload/'; // 原路徑
  6. $dest_path = WWW_PATH.'/supload/'; // 目標路徑
  7. $path = isset($_GET['path'])? $_GET['path'] : ''; // 訪問的圖片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. '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. ?>
複製代碼

訪問以下三個路徑後會按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

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.