PHP gd library generation thumbnail _php tutorial

Source: Internet
Author: User
Tags vars
PHP GD Library
PHP uses the GD library to generate thumbnails.
Code:
  1. Header ("CONTENT-TYPE:TEXT/HTML;CHARSET=GBK");
  2. Ini_set ("Date.timezone","Asia/chong");
  3. //Determine if the file is empty
  4. if (empty($_files)) {
  5. Echo "Upload file too large" ;
  6. Exit ;
  7. }
  8. //Determine if there is an error in file upload
  9. if ($_files[' pic '] [' ERROR ']) {
  10. Echo "Upload file";
  11. Exit ;
  12. }
  13. //Determine if file type is illegally getting file suffix
  14. $allowtype = Array ("jpg","png","JPEG","gif");
  15. $a = Explode ('.' , $_files [' pic '] [' name '];
  16. $index = Count ($a)-1;
  17. $ex = Strtolower ($a[$index]);
  18. if (!in_array ($ex,$allowtype)) {
  19. Echo "Illegal uploading of files";
  20. Exit ;
  21. }
  22. $file = Date (' ymdhis '). Rand (). "." . $ex ;
  23. $src = $_files [' pic '] [' Tmp_name '];
  24. $des = "upload/" . $file ;
  25. $rs =move_uploaded_file ($src,$des);
  26. //thumbnail image
  27. //Read already uploaded pictures
  28. $image =imagecreatefromjpeg ($des);
  29. $a = getimagesize ($des);
  30. $w = $a [0];
  31. $h = $a [1];
  32. if ($w>$h) {
  33. $width =300;
  34. $height = $width / $w * $h ;
  35. }Else if($w<$h) {
  36. $height =300;
  37. $width = $height / $h * $w ;
  38. }Else{
  39. $width =300;
  40. $height =300;
  41. } www.jbxue.com
  42. //Create blank new picture
  43. $newimage =imagecreatetruecolor ($width, $height);
  44. //copy Source Picture content copy new picture
  45. Imagecopyresized ($newimage, $image, 0,0, 0,0 , $width, $height, $w, $h);
  46. $filename = "Upload/s_" . $file ;
  47. Imagejpeg ($newimage,$filename);

http://www.bkjia.com/PHPjc/714728.html www.bkjia.com true http://www.bkjia.com/PHPjc/714728.html techarticle PHP GD library PHP uses the GD library to generate thumbnails. Code: formmethod= "POST" action= "suo_do.php" enctype= "Multipart/form-data" inputtype= "file" name= "pic"/inputtype= "Sub MIT "Value= ...

  • 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.