The principle that PHP automatically generates thumbnails based on URLs

Source: Internet
Author: User
Tags exit config php define relative require

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/

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.