Principle: Set Apache rewrite, when the picture does not exist, invoke PHP to create a picture.
For example
The original path is: http://localhost/upload/news/2013/07/21/1.jpg
The thumbnail path is: http://localhost/supload/news/2013/07/21/1.jpg
Displays a picture when you access http://localhost/supload/news/2013/07/21/1.jpg, such as when a picture exists. Otherwise, call createthumb.php to generate the picture.
The directory structure is as follows:
www/picthumb.class.php
www/thumbconfig.php
Www/upload/news/2013/07/21/1.jpg
Www/upload/article/2013/07/21/2.jpg
Www/supload/.htaccess
Www/supload/watermark.png
www/supload/createthumb.php
http://localhost/point to www directory
PicThumb.class.php usage Please check here : http://blog.csdn.net/fdipzone/article/details/9316385
Need to turn on Apache rewrite
sudo a2enmod rewrite
. htaccess
<ifmodule mod_rewrite.c>
rewriteengine
on # ' s ' (is regular file, with size)
# '-l ' (is symbolic link) c4/># '-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]
</IfModule>
createthumb.php
<?php define (' Www_path ', DirName (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/'; The original path $dest _path = Www_path. ' /supload/'; Target path $path = isset ($_get[' path ')? $_get[' path ']: ';
Access to the picture URL//check path if (! $path) {exit ();
//Get picture uri $relative _url = str_replace ($dest _path, ", Www_path. $path);
Gets 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 (); }//Original file $source = Str_replace ('/'. $type. /', '/'. $config [' Fromdir ']. '
/', $source _path. $relative _url); Target file $dest = $dest _path. $relaTive_url;
Create thumbnails $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)); }?>
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/