- Sudo a2enmod rewrite
2. content of the. htaccess file:
- 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]
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
- Define ('www _ path', 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']: ''; // URL of the Accessed image
- // Check the path
- If (! $ Path ){
- Exit ();
- }
- // Obtain the image URI
- $ Relative_url = str_replace ($ dest_path, '', WWW_PATH. $ path );
- // Obtain the 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 ();
- }
- // Source image file
- $ Source = str_replace ('/'. $ type. '/', '/'. $ config ['fromdir']. '/', $ source_path. $ relative_url );
- // Target file
- $ Dest = $ dest_path. $ relative_url;
- // Create a thumbnail
- $ 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 ));
- }
- ?>
4. ThumbConfig. php
- $ Thumb_config = array (
- 'News' => array (
- 'Fromdir' => 'news', // source directory
- 'Type' => 'fit ',
- & Apos; width & apos; = & apos; 100 & apos,
- & Apos; height & apos; = & apos; 100 & apos,
- 'Bgcolor' => '# FF0000'
- ),
- 'News _ 1' => array (
- 'Fromdir' => 'news ',
- 'Type' => 'fit ',
- & Apos; width & apos; = & apos; 200 & apos,
- & Apos; height & apos; = & apos; 200 & apos,
- 'Bgcolor' => '# ffff00'
- ),
- 'Article' => array (
- 'Fromdir' => 'article ',
- 'Type' => 'crop ',
- & Apos; width & apos; = & apos; 250 & apos,
- & Apos; height & apos; = & apos; 250 & apos,
- 'Watermark' => WWW_PATH. '/supload/watermark.png'
- )
- );
- ?>
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. |