Generate thumbnail images in bulk with PHP

Source: Internet
Author: User
Tags save file
  1. function Mkdirs ($dirname, $mode =0777)//Create directory (directory, [mode])
  2. {
  3. if (!is_dir ($dirname))
  4. {
  5. Mkdirs ($dirname, $mode); If the directory does not exist, recursive builds
  6. return mkdir ($dirname, $mode);
  7. }
  8. return true;
  9. }
  10. function SaveFile ($filename, $content = ")//Save file (file, [content])
  11. {
  12. if (function_exists (file_put_contents))
  13. {
  14. File_put_contents ($filename, $content);
  15. }
  16. Else
  17. {
  18. $FP =fopen ($filename, "WB");
  19. Fwrite ($fp, $content);
  20. Fclose ($FP);
  21. }
  22. }
  23. function Getsuffix ($filename)//Get file name suffix
  24. {
  25. Return End (Explode (".", $filename));
  26. }
  27. function Checksuffix ($filename, $arr)//is the allowable type (current, allowed)
  28. {
  29. if (!is_array ($arr))
  30. {
  31. $arr =explode (",", Str_replace ("", "" ", $arr));
  32. }
  33. Return In_array ($filename, $arr)? 1:0;
  34. }
  35. Class image
  36. {
  37. var $src; Source Address
  38. var $newsrc; New map path (after localization)
  39. var $allowtype =array (". gif", ". jpg", ". png", ". jpeg"); Allowed picture types
  40. var $regif = 0; Whether the thumbnail gif, for 0 does not handle
  41. var $keep = 0; Whether to keep the source file (1 is reserved, 0 is MD5)
  42. var $over = 0; Whether an existing picture can be overwritten, 0 is not overwritten
  43. var $dir; Image source Directory
  44. var $newdir; The directory after processing
  45. function __construct ($olddir =null, $newdir =null)
  46. {
  47. $this->dir= $olddir? $olddir: "./images/temp";
  48. $this->newdir= $newdir? $newdir: "./images/s";
  49. }
  50. function renames ($SRC)
  51. {
  52. $MD 5FILE=SUBSTR (MD5 ($SRC), 10,10). STRRCHR ($src, "."); MD5 file name (for example: 3293okoe.gif)
  53. $MD 5file= $this->w. " _ ". $this->h." _ ". $md 5file; File name after processing
  54. return $this->newdir. " /". $MD 5file; Save the source picture, MD5 file name to a new directory
  55. }
  56. function Mini ($SRC, $w, $h, $q =80)//Generate thumbnail Mini (image address, width, height, quality)
  57. {
  58. $this->src= $src;
  59. $this->w= $w;
  60. $this->h= $h;
  61. if (STRRCHR ($SRC, ".") = = ". gif" && $this->regif==0)//whether to process GIF graphs
  62. {
  63. return $this->src;
  64. }
  65. if ($this->keep==0)//whether to keep the source file, default does not retain
  66. {
  67. $NEWSRC = $this->renames ($SRC); The file address after renaming
  68. }
  69. else//Maintain original
  70. {
  71. $src =str_replace ("\ \", "/", $SRC);
  72. $NEWSRC = $this->NEWDIR.STRRCHR ($src, "/");
  73. }
  74. if (file_exists ($NEWSRC) && $this->over==0)//If already present, return address directly
  75. {
  76. return $NEWSRC;
  77. }
  78. if (Strstr ($SRC, "/http") &&!strstr ($src, $_server[' http_host '))//If it is a network file, save it first
  79. {
  80. $SRC = $this->getimg ($SRC);
  81. }
  82. $arr =getimagesize ($SRC); Get Picture Properties
  83. $width = $arr [0];
  84. $height = $arr [1];
  85. $type = $arr [2];
  86. Switch ($type)
  87. {
  88. Case 1://1 = GIF,
  89. $im =imagecreatefromgif ($SRC);
  90. Break
  91. Case 2://2 = JPG
  92. $im =imagecreatefromjpeg ($SRC);
  93. Break
  94. Case 3://3 = PNG
  95. $im =imagecreatefrompng ($SRC);
  96. Break
  97. Default
  98. return 0;
  99. }
  100. Working with thumbnail images
  101. $nim =imagecreatetruecolor ($w, $h);
  102. $k 1=round ($h/$w, 2);
  103. $k 2=round ($height/$width, 2);
  104. if ($k 1< $k 2)
  105. {
  106. $width _a= $width;
  107. $height _a=round ($width * $k 1);
  108. $SW = 0;
  109. $sh = ($height-$height _a)/2;
  110. }
  111. Else
  112. {
  113. $width _a= $height/$k 1;
  114. $height _a= $height;
  115. $SW = ($width-$width _a)/2;
  116. $sh = 0;
  117. }
  118. Create a picture
  119. if (function_exists (imagecopyresampled))
  120. {
  121. Imagecopyresampled ($nim, $im, 0,0, $SW, $sh, $w, $h, $width _a, $height _a);
  122. }
  123. Else
  124. {
  125. Imagecopyresized ($nim, $im, 0,0, $SW, $sh, $w, $h, $width _a, $height _a);
  126. }
  127. if (!is_dir ($this->newdir))
  128. {
  129. mkdir ($this->newdir);
  130. }
  131. Switch ($type)//Save Picture
  132. {
  133. Case 1:
  134. $rs =imagegif ($nim, $NEWSRC);
  135. Break
  136. Case 2:
  137. $rs =imagejpeg ($nim, $NEWSRC, $q);
  138. Break
  139. Case 3:
  140. $rs =imagepng ($nim, $NEWSRC);
  141. Break
  142. Default
  143. return 0;
  144. }
  145. return $NEWSRC; Return to post-processing path
  146. }
  147. function Getimg ($filename)
  148. {
  149. $MD 5file= $this->dir. " /". SUBSTR (MD5 ($filename), 10,10). STRRCHR ($filename,". ");
  150. if (file_exists ($MD 5file))
  151. {
  152. return $MD 5file;
  153. }
  154. Start getting the file and return to the new path
  155. $img =file_get_contents ($filename);
  156. if ($img)
  157. {
  158. if (!is_dir ($this->dir))
  159. {
  160. mkdir ($this->dir);
  161. }
  162. SaveFile ($md 5file, $img);
  163. return $MD 5file;
  164. }
  165. }
  166. function reimg ($SRC, $w, $h, $q)//Convert thumbnails (file name and structure unchanged)
  167. {
  168. $this->keep=1;
  169. return $this->mini ($SRC, $w, $h, $q); Address generated by return
  170. }
  171. }
  172. $image =new image ();
  173. echo $image->reimg ("Images/zht.jpg", 75,75,80);
  174. echo "
    ";
  175. echo $image->reimg ("Images/m8920.jpg", 75,75,80);
  176. echo "
    ";
  177. echo $image->getimg ("./images/s/zht.jpg");
  178. ?>
Copy Code
Php
  • 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.