Php uploads an image to generate a thumbnail (GD Library), _ PHP Tutorial

Source: Internet
Author: User
Tags learn php programming
Php uploads images to generate thumbnails (GD Library ),. Php uploads an image to generate a thumbnail (GD Library). First, let's take a simple php Upload image to generate a thumbnail for your reference, the specific content is as follows: phpfunction php uploads an image to generate a thumbnail (GD Library ),

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:

GD Library isPHP is a powerful 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 ());Output data indicates that the GD database is referenced successfully.

Upload.html

 Upload imagesFile upload (only jpg images can be uploaded)

Processing page upload_img.php

<? Php // Save the uploaded image $ uploadfile = "upfiles /". $ _ FILES ['upfile'] ['name']; // Thumbnail save address $ smallfile = "upfiles/small _". $ _ FILES ['upfile'] ['name']; if ($ _ FILES ['upfile'] ['type']! = "Image/jpeg") {echo 'file type error ';} else {move_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name'], $ uploadfile ); // upload a file $ dstW = 200; // The width of the thumbnail $ d…… = 200; // The height of the thumbnail $ src_image = ImageCreateFromJPEG ($ uploadfile); $ srcW = ImageSX ($ src_image ); // get the image width $ srcH = ImageSY ($ src_image); // get the image height $ dst_image = ImageCreateTrueColor ($ dstW, $ dsomething); ImageCopyResized ($ dst_image, $ src_image, 0, 0, 0, $ dstW, $ dsomething, $ srcW, $ srcH); ImageJpeg ($ dst_image, $ smallfile); echo 'file uploaded successfully
'; Echo "" ;}?>

I hope it will help you learn php programming. thank you for your support.

Articles you may be interested in:
  • Php ajax example code for refreshing Upload of images
  • Simple PHP code for uploading and deleting images
  • Php getimagesize the length and width of the uploaded image detection code
  • Php gd2 uploads image/text watermark/image watermark/proportional thumbnails/implementation code
  • An extremely useful php image upload class (random name, thumbnail, and watermark)
  • 6 Solutions for renaming uploaded images based on php
  • Php uploads images to a specified location and saves them to the database
  • Example of storing php uploaded images to a database
  • Example of generating thumbnails for uploading images using php
  • Php achieves proportional scaling of the specified size to generate a thumbnail for the uploaded image

Compile (GD Library), first of all, let's take a simple php Upload image to generate a detailed code for thumbnails, and share it with you for your reference. the specific content is as follows: phpfunction...

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.