Use ThinkPHP to generate and display thumbnails and thinkphp thumbnails.
First, understand some functions in the parent class Image. class. php (ThinkPHP/Library/Think/Image. class. php ).
1: open () open the processed image
2: Create a thumbnail of thumb () by default, and scale proportionally to 1 (for meanings of 2, 3, 4, 5, and 6, see the parent class file Image. class. php)
3: save () thumbnails to the server
To generate a thumbnail, follow these steps:
* 1. instantiation
* 2. open the image open ()
* 3. Generate the default proportional scaling of the thumbnail thumb ()
* 4. save ()
Controller code:
// Large image path (for details about the large image path, refer to the upload path for "using ThinkPHP to upload attachments" in the previous article)
$ Bigimg_path = $ upload-> rootPath. $ file_info ['savepath']. $ file_info ['savename']; // the path of the thumbnail $ smallimg_path = $ upload-> rootPath. $ file_info ['savepath']. 'Small _'. $ file_info ['savename']; $ img = new \ Think \ Image (); // instantiate $ img-> open ($ bigimg_path ); // open the processed image $ img-> thumb (100,100); // create a thumbnail (100*100) $ img-> save ($ smallimg_path ); // Save the thumbnail to the server // Save the uploaded attachment and thumbnail to the database $ _ POST ['goods _ big_img '] = $ bigimg_path; $ _ POST ['goods _ small_img '] = $ smallimg_path;
Constant set in the entry file
For the convenience of displaying the thumbnail path on the foreground, set a constant in index. php of the entrance file, indicating the site path, and splice it with the image path stored in the database
define('SITE_URL', 'http://www.tp.com/TP/shop/');
Big image and thumbnail displayed on the front-end
<Td> <! -- Big chart --> < {$ v. goods_big_img}> "height =" 60 "width =" 60 "> </td> <! -- Thumbnail --> < {$ v. goods_small_img}> "height =" 40 "width =" 40 "> </td>
Result
Database storage path
Image
Front-end display