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 thumbnail thumb() with 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. , spliced with the image path stored in the database
define('SITE_URL', 'http://www.tp.com/TP/shop/');
The front desk displays large images and thumbnails
<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>
Results
In the database Storage path
Picture
Foreground display
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP The difference between abstract and interface_javascript skills
Example of php cross-domain calling json_jquery
The above is the detailed content of How to generate thumbnails and display them using ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!