Code for uploading images and creating thumbnails in the phpGD Library

Source: Internet
Author: User
Code for uploading images and creating thumbnails in the phpGD Library

  1. Image Upload -bbs.it-home.org
  2. File upload (only jpg images can be uploaded)

2. process the page upload_img.php

  1. // Save the uploaded image

  2. $ Uploadfile = "upfiles/". $ _ FILES ['upfile'] ['name'];
  3. // Thumbnail storage address
  4. $ Smallfile = "upfiles/small _". $ _ FILES ['upfile'] ['name'];

  5. If ($ _ FILES ['upfile'] ['type']! = "Image/jpeg ")
  6. {
  7. Echo 'file type error ';
  8. }
  9. Else
  10. {
  11. Move_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name'], $ uploadfile); // upload a file

  12. $ DstW = 200; // Thumbnail width

  13. $ Dsomething = 200; // The height of the thumbnail.

  14. $ Src_image = ImageCreateFromJPEG ($ uploadfile );

  15. $ SrcW = ImageSX ($ src_image); // Obtain the image width
  16. $ SrcH = ImageSY ($ src_image); // Obtain the image height

  17. $ Dst_image = ImageCreateTrueColor ($ dstW, $ dsomething );

  18. ImageCopyResized ($ dst_image, $ src_image, 0, 0, 0, $ dstW, $ dsomething, $ srcW, $ srcH );
  19. ImageJpeg ($ dst_image, $ smallfile );

  20. Echo 'file uploaded successfully
    ';

  21. Echo "";
  22. }
  23. ?>

The above code is relatively simple. if you are interested, you can study the gd library function in the php Manual.

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.