PHP implementation method of automatically generating thumbnails based on URL

Source: Internet
Author: User
Tags relative require

This article describes the PHP implementation based on the URL automatically generated thumbnails of the method, is a very practical function. Share to everyone for your reference. The specific methods are as follows:

  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

  Need to turn on Apache rewrite:

sudo a2enmod rewrite

  The . htaccess file is as follows:

<ifmodule mod_rewrite.c>

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]

</ifmodule >

createthumb.php files are as follows:

<?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 files

$source _path = Www_path. ' /upload/'; 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);

Get 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 artwork File

$source = Str_replace ('/'. $type. /', '/'. $config [' Fromdir ']. ' /', $source _path. $relative _url);

Destination 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));

}

?>

thumbconfig.php files are as follows:

<?php

$thumb _config = Array (

' News ' => Array (

' Fromdir ' => ' news ',//source directory

' Type ' => ' fit ',

' Width ' => 100,

' Height ' => 100,

' bgcolor ' => ' #FF0000 '

),

' News_1 ' => Array (

' Fromdir ' => ' news ',

' Type ' => ' fit ',

' Width ' => 200,

' Height ' => 200,

' bgcolor ' => ' #FFFF00 '

),

' article ' => Array (

' Fromdir ' => ' article ',

' type ' => ' crop ',

' Width ' => 250,

' Height ' => 250,

' Watermark ' => www_path. ' /supload/watermark.png '

)

);

?>

After accessing these three paths, the thumbnails are automatically generated by 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

I hope this article will help you with your PHP program design.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.