PHP generates thumbnail illustration sample code sharing

Source: Internet
Author: User

Share a use of PHP's GD library to generate thumbnails of examples, we refer to the use of the bar

The code is as follows:

<form method= "POST" action= "suo_do.php" enctype= "Multipart/form-data" >

<input type= "file" name= "Pic"/>

<input type= "Submit" value= "Upload 1"/>




Ini_set ("Date.timezone", "Asia/chong");

Determine if the file is empty

if (empty ($_files)) {

echo "Upload file too large";



To determine if there are errors in file uploads

if ($_files[' pic '] [' ERROR ']) {

echo "Upload file";



To determine if a file type illegally obtains a file suffix

$allowtype =array ("JPG", "PNG", "JPEG", "gif");

$a =explode ('. ', $_files[' pic '] [' name ']);

$index =count ($a)-1;

$ex =strtolower ($a [$index]);

if (!in_array ($ex, $allowtype)) {

echo "Illegal upload file";



$file =date (' Ymdhis '). Rand (). "." $ex;

$SRC =$_files[' pic '] [' tmp_name '];

$des = "upload/". $file;

$rs =move_uploaded_file ($SRC, $des);

Thumbnail image

Read already uploaded pictures

$image =imagecreatefromjpeg ($des);

$a =getimagesize ($des);

$w = $a [0];

$h = $a [1];

if ($w > $h) {

$width = 300;

$height = $width/$w * $H;

}else if ($w < $h) {

$height = 300;

$width = $height/$h * $W;


$width = 300;

$height = 300;


Create a blank new picture

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

Copy source picture content copy new picture

Imagecopyresized ($newimage, $image, 0,0, 0,0, $width, $height, $w, $h);

$filename = "Upload/s_". $file;

Imagejpeg ($newimage, $filename);

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.