Php code to generate a thumbnail based on the request and save it to the Linux image server

Source: Internet
Author: User
Php code to generate a thumbnail based on the request and save it to the Linux image server

  1. /**
  2. * Linux image server with thumbnails
  3. * Edit bbs.it-home.org
  4. */
  5. $ PicID = $ _ GET ['imgid'];
  6. $ PicTypes = ".". $ _ GET ['imgtype'];
  7. $ PicWidth = $ _ GET ['imgwidth'];
  8. If ($ picID! = "")
  9. {
  10. // The request is a thumbnail.
  11. If ($ picWidth> 0 ){
  12. // If the thumbnail exists
  13. If (file_exists ($ picID. "_". $ picWidth. $ picTypes ))
  14. {
  15. OutputImg ($ picID. "_". $ picWidth. $ picTypes );
  16. } Else
  17. {
  18. If (file_exists ($ picID. "_ 0". $ picTypes )){
  19. // Generate a thumbnail directly if no thumbnail exists
  20. ResizeImg ($ picID. "_ 0". $ picTypes, $ picWidth, $ picWidth, $ picID. "_". $ picWidth. $ picTypes );
  21. // Output
  22. OutputImg ($ picID. "_". $ picWidth. $ picTypes );
  23. } Else
  24. {
  25. // If the large image does not exist
  26. ResizeImg('noDefaultImage.gif ', $ picWidth, $ picWidth, noDefaultImage. "_". $ picWidth. ". gif ");
  27. // Output
  28. OutputImg ($ picID. "_". $ picWidth. $ picTypes );
  29. }
  30. }
  31. }
  32. // Determine whether a large image exists in the file
  33. If (file_exists ($ picID. "_ 0". $ picTypes ))
  34. {
  35. $ Img_file = $ picID. "_ 0". $ picTypes;
  36. OutputImg ($ img_file );
  37. }
  38. Else
  39. {
  40. // If no image exists
  41. $ Img_file = 'noDefaultImage.gif ';
  42. OutputImg ($ img_file );
  43. }
  44. }
  45. // Output image
  46. Function outputImg ($ img_file)
  47. {
  48. $ Fp = fopen ($ img_file, 'RB ');
  49. $ Content = fread ($ fp, filesize ($ img_file); // binary data
  50. Fclose ($ fp );
  51. Header ('content-Type: image/gif ');
  52. Echo $ content;
  53. }
  54. /**
  55. * Generating thumbnails
  56. * $ SrcName ---- Original Image path
  57. * $ NewWidth, $ newHeight ---- maximum width and height of the thumbnails respectively
  58. * $ NewName ---- name of the thumbnail file (including the path)
  59. * @ Param string $ srcName
  60. * @ Param int $ newWidth
  61. * @ Param int $ newHeight
  62. * @ Param string $ newName
  63. * Return viod
  64. */
  65. Function resizeImg ($ srcName, $ newWidth, $ newHeight, $ newName = "")
  66. {
  67. If ($ newName = "")
  68. {
  69. $ NameArr = explode ('.', $ srcName );
  70. $ ExpName = array_pop ($ nameArr );
  71. $ ExpName = $ expName;
  72. Array_push ($ nameArr, $ expName );
  73. $ NewName = implode ('.', $ nameArr );
  74. }
  75. $ Info = "";
  76. $ Data = getimagesize ($ srcName, $ info );
  77. Switch ($ data [2])
  78. {
  79. Case 1:
  80. If (! Function_exists ("imagecreatefromgif ")){
  81. Echo "your GD library cannot use images in GIF format. please use Jpeg or PNG format! Return ";
  82. Exit ();
  83. }
  84. $ Im = ImageCreateFromGIF ($ srcName );
  85. Break;
  86. Case 2:
  87. If (! Function_exists ("imagecreatefromjpeg ")){
  88. Echo "your GD library cannot use images in jpeg format. please use images in other formats! Return ";
  89. Exit ();
  90. }
  91. $ Im = ImageCreateFromJpeg ($ srcName );
  92. Break;
  93. Case 3:
  94. $ Im = ImageCreateFromPNG ($ srcName );
  95. Break;
  96. }
  97. $ SrcW = ImageSX ($ im );
  98. $ SrcH = ImageSY ($ im );
  99. $ NewWidthH = $ newWidth/$ newHeight;
  100. $ SrcWH = $ srcW/$ srcH;
  101. If ($ newWidthH <= $ srcWH ){
  102. $ FtoW = $ newWidth;
  103. $ FtoH = $ ftoW * ($ srcH/$ srcW );
  104. }
  105. Else {
  106. $ FtoH = $ newHeight;
  107. $ FtoW = $ ftoH * ($ srcW/$ srcH );
  108. }
  109. If ($ srcW> $ newWidth | $ srcH> $ newHeight)
  110. {
  111. If (function_exists ("imagecreatetruecolor "))
  112. {
  113. @ $ Ni = ImageCreateTrueColor ($ ftoW, $ ftoH );
  114. If ($ ni) ImageCopyResampled ($ ni, $ im, 0, 0, 0, $ ftoW, $ ftoH, $ srcW, $ srcH );
  115. Else
  116. {
  117. $ Ni = ImageCreate ($ ftoW, $ ftoH );
  118. ImageCopyResized ($ ni, $ im, 0, 0, 0, $ ftoW, $ ftoH, $ srcW, $ srcH );
  119. }
  120. }
  121. Else
  122. {
  123. $ Ni = ImageCreate ($ ftoW, $ ftoH );
  124. ImageCopyResized ($ ni, $ im, 0, 0, 0, $ ftoW, $ ftoH, $ srcW, $ srcH );
  125. }
  126. If (function_exists ('imagejpeg ') imagejpeg ($ ni, $ newName );
  127. Else ImagePNG ($ ni, $ newName );
  128. ImageDestroy ($ ni );
  129. }
  130. ImageDestroy ($ im );
  131. }
  132. ?>

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.