Home >Backend Development >PHP Tutorial >How to dynamically generate thumbnails in php and output them for display_PHP tutorial

How to dynamically generate thumbnails in php and output them for display_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:56:381152browse

How to dynamically generate thumbnails in php and output the display

The following introduces you to the method of dynamically generating thumbnails in php and outputting the display, involving related skills of php operating pictures, which is very It has practical value, friends in need can refer to it

The example in this article describes the method of dynamically generating thumbnails in php and outputting them for display. Share it with everyone for your reference. The details are as follows:

Calling method:

ใ€€?

1

1

This code can dynamically generate thumbnail displays for large pictures. The pictures are generated in the memory and do not generate real files on the hard disk.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

$filename= $_GET['filename'];

$width = $_GET['width'];

$height = $_GET['height'];

$path="http://localhost/images/"; //finish in "/"

// Content type

header('Content-type: image/jpeg');

// Get new dimensions

list($width_orig, $height_orig) = getimagesize($path.$filename);

if ($width && ($width_orig < $height_orig)) {

$width = ($height / $height_orig) * $width_orig;

} else {

$height = ($width / $width_orig) * $height_orig;

}

// Resample

$image_p = imagecreatetruecolor($width, $height);

$image = imagecreatefromjpeg($path.$filename);

imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig);

// Output

imagejpeg($image_p, null, 100);

// Imagedestroy

imagedestroy ($image_p);

?>

The thumbs.php file is as follows:

ใ€€?

1

3 4 5 6 7
8 9
10 11 12 13 14 15 16 17 18 19 20 21 22 23
<๐ŸŽœ>$filename= $_GET['filename'];<๐ŸŽœ> <๐ŸŽœ>$width = $_GET['width'];<๐ŸŽœ> <๐ŸŽœ>$height = $_GET['height'];<๐ŸŽœ> <๐ŸŽœ>$path="http://localhost/images/"; //finish in "/"<๐ŸŽœ> <๐ŸŽœ>// Content type<๐ŸŽœ> <๐ŸŽœ>header('Content-type: image/jpeg');<๐ŸŽœ> <๐ŸŽœ>// Get new dimensions<๐ŸŽœ> <๐ŸŽœ>list($width_orig, $height_orig) = getimagesize($path.$filename);<๐ŸŽœ> <๐ŸŽœ>if ($width && ($width_orig < $height_orig)) {<๐ŸŽœ> <๐ŸŽœ>$width = ($height / $height_orig) * $width_orig;<๐ŸŽœ> <๐ŸŽœ>} else {<๐ŸŽœ> <๐ŸŽœ>$height = ($width / $width_orig) * $height_orig;<๐ŸŽœ> <๐ŸŽœ>}<๐ŸŽœ> <๐ŸŽœ>//Resample<๐ŸŽœ> <๐ŸŽœ>$image_p = imagecreatetruecolor($width, $height);<๐ŸŽœ> <๐ŸŽœ>$image = imagecreatefromjpeg($path.$filename);<๐ŸŽœ> <๐ŸŽœ>imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig);<๐ŸŽœ> <๐ŸŽœ>//Output<๐ŸŽœ> <๐ŸŽœ>imagejpeg($image_p, null, 100);<๐ŸŽœ> <๐ŸŽœ>// Imagedestroy<๐ŸŽœ> <๐ŸŽœ>imagedestroy ($image_p);<๐ŸŽœ> <๐ŸŽœ>?>
I hope this article will be helpful to everyoneโ€™s PHP programming design. http://www.bkjia.com/PHPjc/987107.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/987107.htmlTechArticleHow to dynamically generate thumbnails in php and output them for display. The following introduces how php dynamically generates thumbnails and outputs them for display. Methods, involving related skills of operating images in PHP, are of great practical value...
Statement๏ผš
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