Use ThinkPHP to generate thumbnails and display, and thinkphp to generate thumbnails.

Source: Internet
Author: User

Use ThinkPHP to generate thumbnails and display, and thinkphp to generate thumbnails.

Using ThinkPHP to generate thumbnails and display are of reference value. For more information, see

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

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.