: This article mainly introduces how to generate thumbnails (GD library) for uploading images in php. if you are interested in the PHP Tutorial, refer to it. First, let's take a simple php code to upload images to generate thumbnails for your reference. The details are as follows:
<?phpfunction createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){$details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.');$type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']);eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");');$thumbHeight = $details[1] * ($thumbWidth / $details[0]);$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight,$details[0], $details[1]);eval('image'.$type.'($thumbImg, "$thumbDirectory/$imageName"'.(($type=='jpeg')?', $quality':'').');');imagedestroy($srcImg);imagedestroy($thumbImg);}foreach ($_FILES["pictures"]["error"] as $key => $error) {if ($error == UPLOAD_ERR_OK) {$tmp_name = $_FILES["pictures"]["tmp_name"][$key];$name = $_FILES["pictures"]["name"][$key];move_uploaded_file($tmp_name, "data/$name");createThumbnail("/location/of/main/image", $name, "/location/to/store/thumb", 120, 80);//120 = thumb width :: 80 = thumb quality (1-100)}}?>
Next, we recommend a php instance to use the GD library to upload images and create thumbnails. you can view the code directly:
The GD Library is a powerful PHP library for image operations.
Add a line of reference in php. ini: extension = php_gd2.dll
Restart apache and make a test page var_dump (gd_info (). the output data indicates that the GD database has been referenced successfully.
Upload.html
"; }?>
I hope it will help you learn php programming. thank you for your support.
The above describes how to generate thumbnails (GD library) for uploading images in php, including some content. I hope to help my friends who are interested in PHP tutorials.