PHP Upload image generation thumbnail (GD library) _php Tips

Source: Internet
Author: User
Tags learn php

First to a simple PHP upload image to generate thumbnails of the detailed code, to share with you for your reference, the specific content as follows

<?php function Createthumbnail ($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality) {$details =
GetImageSize ("$imageDirectory/$imageName") or Die (' please upload images. ');
$type = Preg_replace (' @^.+ (? <=/) (. +) $@ ', ' $ ', $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["Pictur
Es "] [" 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);
 Thumb width:: Thumb = Quality (1-100)}?>

Next, we recommend an example PHP use the GD library to upload pictures and create thumbnails, directly look at the code:

The GD library is a very powerful library of PHP for image manipulation.

First add one line to the php.ini:extension=php_gd2.dll

Restart Apache, do a test page var_dump (Gd_info ()); The output data indicates that the GD library reference was successful.

Picture upload page upload.html

 
 

Process Page upload_img.php

<?php
 //Upload image save address
 $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 file

  $dstW =200;//thumbnail width
  $dstH =200;//thumbnail high

  $src _image=imagecreatefromjpeg ($uploadfile);
  $srcW =imagesx ($src _image); Get picture width
  $srcH =imagesy ($src _image);//Get Picture high

  $dst _image=imagecreatetruecolor ($dstW, $dstH);
  Imagecopyresized ($dst _image, $src _image,0,0,0,0, $dstW, $dstH, $srcW, $srcH);
  Imagejpeg ($dst _image, $smallfile);

  echo ' File upload success <br> ';
  echo "

We hope to learn PHP program design help, thank you for your support.

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.