Generate thumbnails in the php GD library

Source: Internet
Author: User

Php GD library
Php uses the GD library to generate thumbnails.
Code:

  1. <Form method = "post" action = "suo_do.php" enctype = "multipart/form-data">
  2. <Input type = "file" name = "pic"/>
  3. <Input type = "submit" value = "Upload 1"/>
  4. </Form>
  5. <? Php
  6. Header ("content-type: text/html; charset = gbk ");
  7. Ini_set ("date. timezone", "Asia/chong ");
  8. // Determine whether the file is empty
  9. If (empty ($ _ FILES )){
  10. Echo "uploading files too large ";
  11. Exit;
  12. }
  13. // Determine whether the file is uploaded incorrectly
  14. If ($ _ FILES ['pic '] ['error']) {
  15. Echo "upload files ";
  16. Exit;
  17. }
  18. // Determine whether the file type is illegal to obtain the file suffix
  19. $ Allowtype = array ("jpg", "png", "jpeg", "gif ");
  20. $ A = explode ('.', $ _ FILES ['pic '] ['name']);
  21. $ Index = count ($ a)-1;
  22. $ Ex = strtolower ($ a [$ index]);
  23. If (! In_array ($ ex, $ allowtype )){
  24. Echo "Illegal File Upload ";
  25. Exit;
  26. }
  27. $ File = date ('ymdhis '). rand (). ".". $ ex;
  28. $ Src = $ _ FILES ['pic '] ['tmp _ name'];
  29. $ Des = "upload/". $ file;
  30. $ Rs = move_uploaded_file ($ src, $ des );
  31. // Thumbnail
  32. // Read the uploaded Image
  33. $ Image = imagecreatefromjpeg ($ des );
  34. $ A = getimagesize ($ des );
  35. $ W = $ a [0];
  36. $ H = $ a [1];
  37. If ($ w> $ h ){
  38. $ Width = 300;
  39. $ Height = $ width/$ w * $ h;
  40. } Else if ($ w <$ h ){
  41. $ Height = 300;
  42. $ Width = $ height/$ h * $ w;
  43. } Else {
  44. $ Width = 300;
  45. $ Height = 300;
  46. } Www.jbxue.com
  47. // Create a blank image
  48. $ Newimage = imagecreatetruecolor ($ width, $ height );
  49. // Copy source image content copy New Image
  50. Imagecopyresized ($ newimage, $ image, 0, 0, 0, $ width, $ height, $ w, $ h );
  51. $ Filename = "upload/s _". $ file;
  52. Imagejpeg ($ newimage, $ filename );

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.