Home > Backend Development > PHP Tutorial > How to generate thumbnails and display them using ThinkPHP

How to generate thumbnails and display them using ThinkPHP

墨辰丷
Release: 2023-03-27 16:38:01
Original
2821 people have browsed it

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;
Copy after login

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>
Copy after login

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

php displays the file where the current file is located and all the files in the folder are expanded in a tree shape_ javascript skills

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template