Php uploads images to generate thumbnails (GD Library)

Source: Internet
Author: User
Tags learn php programming
: 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.

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.