Home > Backend Development > PHP Tutorial > Sample code for php method to generate thumbnails using timthumb

Sample code for php method to generate thumbnails using timthumb

怪我咯
Release: 2023-03-13 18:18:01
Original
1485 people have browsed it

TimThumb is a very simple and convenient PHP program for cropping. As long as you set some parameters for it, it can generate thumbnails. Many WordPress themes now use the TimThumb PHP class library for thumbnail processing. This article mainly introduces how PHP uses timthumb to generate thumbnails. It analyzes the operation skills and related notes of PHP calling timthumb to generate thumbnails. Friends who need it can refer to the following. ,The details are as follows:

There are two ways to generate thumbnails:

1. Generate them in advance for calling

The common practice for thumbnails is to start with how many types of thumbnails you want to generate based on the picture specifications on the website. During the uploading of the picture , thumbnails will be generated for different needs. transfer. One disadvantage of this method is that if you increase the image specifications, the previously uploaded images do not have the specifications and will have to be regenerated.

2. When calling, generate thumbnails. This is why I wrote this article.

Recommend timthumb. Personally, I think this thing is definitely easy to use without considering performance and efficiency.

1, download address: http://timthumb.googlecode.com/svn/trunk/timthumb.php

2, usage method

After downloading , put it in your web environment, create a cache folder in the directory parallel to timthumb.php, and give it 755 permissions. If you use Windows, you don’t need it. The test code is as follows:

<?php
$image = "http://localhost/test/pen_pic03.png";
?>
<html>
<head>
<title>timthumb</title>
</head>
<body>
 <img src="http://localhost/test/timthumb.php?src=<?php echo $image; ?>&w=58&h=46&zc=1" alt="" />
</body>
</html>
Copy after login

Parameter description:

w=58 means the picture width is 58
h=46 means the picture height is 46

You can customize it as needed set up.

The biggest advantage of timthumb is convenience. I have not encountered such a tool so far. The shortcomings are equally obvious. Every time a request is made, php-cgi or php-cli will be called, CachingThe data is placed in the cache directory. If you call the data, you have to search the directory, and the performance will be relatively low.

The above is the detailed content of Sample code for php method to generate thumbnails using timthumb. 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