Home >Backend Development >PHP Tutorial >Use ThinkPHP to generate thumbnails and display them
This article mainly introduces the relevant information of using ThinkPHP to generate thumbnails and display in detail. It has certain reference value. Interested friends can refer to
Using ThinkPHP to generate thumbnails and display. Display, has certain reference value, interested friends can refer to
First understand some functions in the parent class Image.class.php (ThinkPHP/Library/Think/Image.class.php)
1:open() opens the processed image
2:thumb() generates a thumbnail with a default scaling of 1 (for the meaning of 2, 3, 4, 5, 6, please refer to the parent page Class file Image.class.php)
3:save() Thumbnail to server
The steps to generate thumbnail are divided into the following four steps
* 1. Instantiation
* 2. Open the image open()
* 3. Generate a thumbnail thumb() with the default scaling
* 4. Save save()
Controller code:
//大图路径(此处大图路径可参考上篇 “使用ThinkPHP实现附件上传”的上传路径) $bigimg_path = $upload->rootPath.$file_info['savepath'].$file_info['savename']; //小图路径 $smallimg_path = $upload->rootPath.$file_info['savepath'].'small_'.$file_info['savename']; $img = new \Think\Image(); //实例化 $img->open($bigimg_path); //打开被处理的图片 $img->thumb(100,100); //制作缩略图(100*100) $img->save($smallimg_path); //保存缩略图到服务器 //把上传好的附件及缩略图存到数据库 $_POST['goods_big_img']=$bigimg_path; $_POST['goods_small_img']=$smallimg_path;
Entry file setting constants
It is convenient to display thumbnail paths in the front desk. Set constants in the entry file index.php to represent the site path and splice them with the image paths stored in the database
define('SITE_URL', 'http://www.tp.com/TP/shop/');
Display large and thumbnail images at the front desk Sketch
<td><!-- 大图 --> <img src="<{$smarty.const.SITE_URL}><{$v.goods_big_img}>" height="60" width="60"> </td> <td><!-- 小图 --> <img src="<{$smarty.const.SITE_URL}><{$v.goods_small_img}>" height="40" width="40"> </td>
Result
Storage path in database
Picture
Foreground display
The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to PHP Chinese net!
Related recommendations:
Analysis on ThinkPHP watermarking and setting watermark location
How to use thinkphp to obtain the client IP
The above is the detailed content of Use ThinkPHP to generate thumbnails and display them. For more information, please follow other related articles on the PHP Chinese website!