PHP generates thumbnail code (good compatibility)

Source: Internet
Author: User
Tags imagejpeg

PHP generated thumbnails of a lot of code, but can be fully compatible with gd1.6 and gd2.x, and can ensure that the thumbnail clarity of the code is almost no, the following code can achieve good compatibility. Share it, for everyone to learn the reference.

  1. function Imageresize ($srcFile, $toW, $toH, $toFile = "")
  2. {
  3. if ($toFile = = "") {$toFile = $srcFile;}
  4. $info = "";
  5. $data = getimagesize ($srcFile, $info);
  6. Switch ($data [2])
  7. {
  8. Case 1:
  9. if (!function_exists ("Imagecreatefromgif")) {
  10. echo "Your GD library cannot use GIF format pictures, please use JPEG or PNG format!" Return ";
  11. Exit ();
  12. }
  13. $im = Imagecreatefromgif ($srcFile);
  14. Break
  15. Case 2:
  16. if (!function_exists ("Imagecreatefromjpeg")) {
  17. echo "Your GD library can not use JPEG format pictures, please use other format pictures!" Return ";
  18. Exit ();
  19. }
  20. $im = Imagecreatefromjpeg ($srcFile);
  21. Break
  22. Case 3:
  23. $im = Imagecreatefrompng ($srcFile);
  24. Break
  25. }
  26. $srcW =imagesx ($im);
  27. $srcH =imagesy ($im);
  28. $toWH = $toW/$toH;
  29. $srcWH = $srcW/$srcH;
  30. if ($toWH <= $srcWH) {
  31. $ftoW = $toW;
  32. $ftoH = $ftoW * ($srcH/$srcW);
  33. }
  34. else{
  35. $ftoH = $toH;
  36. $ftoW = $ftoH * ($srcW/$srcH);
  37. }
  38. if ($srcW > $toW | | $srcH > $toH)
  39. {
  40. if (function_exists ("Imagecreatetruecolor")) {
  41. @ $ni = Imagecreatetruecolor ($ftoW, $ftoH);
  42. if ($ni) imagecopyresampled ($ni, $im, 0,0,0,0, $ftoW, $ftoH, $srcW, $srcH);
  43. else{
  44. $ni =imagecreate ($ftoW, $ftoH);
  45. Imagecopyresized ($ni, $im, 0,0,0,0, $ftoW, $ftoH, $srcW, $srcH);
  46. }
  47. }else{
  48. $ni =imagecreate ($ftoW, $ftoH);
  49. Imagecopyresized ($ni, $im, 0,0,0,0, $ftoW, $ftoH, $srcW, $srcH);
  50. }
  51. if (function_exists (' imagejpeg ')) imagejpeg ($ni, $toFile);
  52. Else Imagepng ($ni, $toFile);
  53. Imagedestroy ($ni);
  54. }
  55. Imagedestroy ($im);
  56. }
Copy Code
  • Related Article

    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.