Php automatically generates thumbnails based on URLs (with source code)

Source: Internet
Author: User
Php automatically generates thumbnails based on URLs (with source code)

  1. Sudo a2enmod rewrite

2. 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]

For how to configure the htaccess file, see apache. htaccess configuration details. htaccess configuration details (not to be missed in all classic scenarios ). htaccess file usage summary. htaccess tutorial. htaccess learning summary Apache usage. examples of htaccess pseudo-static configuration htaccess pseudo-static rule configuration: htaccess URL rewriting rewrite and redirection redirect3, createthumb. php

  1. Define ('www _ path', 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']: ''; // URL of the Accessed image
  8. // Check the path
  9. If (! $ Path ){
  10. Exit ();
  11. }
  12. // Obtain the image URI
  13. $ Relative_url = str_replace ($ dest_path, '', WWW_PATH. $ path );
  14. // Obtain the 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. // Source image file
  23. $ Source = str_replace ('/'. $ type. '/', '/'. $ config ['fromdir']. '/', $ source_path. $ relative_url );
  24. // Target file
  25. $ Dest = $ dest_path. $ relative_url;
  26. // Create a 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. ?>

4. ThumbConfig. php

  1. $ Thumb_config = array (
  2. 'News' => array (
  3. 'Fromdir' => 'news', // source directory
  4. 'Type' => 'fit ',
  5. & Apos; width & apos; = & apos; 100 & apos,
  6. & Apos; height & apos; = & apos; 100 & apos,
  7. 'Bgcolor' => '# FF0000'
  8. ),
  9. 'News _ 1' => array (
  10. 'Fromdir' => 'news ',
  11. 'Type' => 'fit ',
  12. & Apos; width & apos; = & apos; 200 & apos,
  13. & Apos; height & apos; = & apos; 200 & apos,
  14. 'Bgcolor' => '# ffff00'
  15. ),
  16. 'Article' => array (
  17. 'Fromdir' => 'article ',
  18. 'Type' => 'crop ',
  19. & Apos; width & apos; = & apos; 250 & apos,
  20. & Apos; height & apos; = & apos; 250 & apos,
  21. 'Watermark' => WWW_PATH. '/supload/watermark.png'
  22. )
  23. );
  24. ?>

For example, a thumbnail is automatically generated according to config after the three paths are accessed.

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 of the thumbnail based on the url.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.