• 缩略图生成类 追忆


    1. <?
    2. class resizeimage  
    3. {  
    4. //图片类型  
    5. var $type;  
    6. //实际宽度  
    7. var $width;  
    8. //实际高度  
    9. var $height;  
    10. //改变后的宽度  
    11. var $resize_width;  
    12. //改变后的高度  
    13. var $resize_height;  
    14. //是否裁图  
    15. var $cut;  
    16. //源图象  
    17. var $srcimg;  
    18. //目标图象地址  
    19. var $dstimg;  
    20. //临时建的图象  
    21. var $im;  
    22. //生成的文件名后缀
    23. var $extstr;
    24. function resizeimage($img, $wid, $hei,$extstr,$c=0)  
    25. {  
    26. $this->srcimg = $img;  
    27. $this->resize_width = $wid;  
    28. $this->resize_height = $hei;  
    29. $this->cut = $c;  
    30. $this->extstr = $extstr;
    31. //图片的类型  
    32. $this->type = substr(strrchr($this->srcimg,"."),1);  
    33. //初始化图象  
    34. $this->initi_img();  
    35. //目标图象地址  
    36. $this -> dst_img();  
    37. $this->width =@imagesx($this->im);  
    38. $this->height =@imagesy($this->im);  
    39. //生成图象  
    40. $this->newimg();  
    41. @ImageDestroy($this->im);  
    42. }  
    43. function newimg()  
    44. {  
    45. //改变后的图象的比例  
    46. $resize_ratio =($this->resize_width)/($this->resize_height);  
    47. //实际图象的比例  
    48. if($this->height>0)
    49. $ratio =($this->width)/($this->height);  
    50. if(($this->cut)=="1")  
    51. //裁图  
    52. {  
    53. if($ratio>=$resize_ratio)  
    54. //高度优先  
    55. {  
    56. $newimg =@imagecreatetruecolor($this->resize_width,$this->resize_height);  
    57. @imagecopyresampled($newimg, $this->im,0,0,0,0, $this->resize_width,$this->resize_height,(($this->height)*$resize_ratio), $this->height);  
    58. @ImageJpeg($newimg,$this->dstimg);  
    59. }  
    60. if($ratio<$resize_ratio)  
    61. //宽度优先  
    62. {  
    63. $newimg =@imagecreatetruecolor($this->resize_width,$this->resize_height);  
    64. @imagecopyresampled($newimg, $this->im,0,0,0,0, $this->resize_width, $this->resize_height, $this->width,(($this->width)/$resize_ratio));  
    65. @ImageJpeg($newimg,$this->dstimg);  
    66. }  
    67. }  
    68. else  
    69. //不裁图  
    70. {  
    71. if($ratio>=$resize_ratio)  
    72. {  
    73. $newimg =@imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);  
    74. @imagecopyresampled($newimg, $this->im,0,0,0,0, $this->resize_width,($this->resize_width)/$ratio, $this->width, $this->height);  
    75. ImageJpeg($newimg,$this->dstimg);  
    76. }  
    77. if($ratio<$resize_ratio)  
    78. {  
    79. $newimg =@imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);  
    80. @imagecopyresampled($newimg, $this->im,0,0,0,0,($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);  
    81. @ImageJpeg($newimg,$this->dstimg);  
    82. }  
    83. }  
    84. }  
    85. //初始化图象  
    86. function initi_img()  
    87. {  
    88. $type=strtolower($this->type);//转换成小写,否则不写扩展名生成不了。
    89. if($type=="jpg"|| $type=="jpeg"|| $type=="jpe")  
    90. {  
    91. $this->im =@imagecreatefromjpeg($this->srcimg);  
    92. }  
    93. if($type=="gif")  
    94. {  
    95. $this->im =@imagecreatefromgif($this->srcimg);  
    96. }  
    97. if($type=="png")  
    98. {  
    99. $this->im =@imagecreatefrompng($this->srcimg);  
    100. }
    101. if($type=="bmp")
    102. {
    103. $this->im = $this->imagecreatefrombmp($this->srcimg);
    104. }
    105. }  
    106. function imagecreatefrombmp($p_sFile){
    107. $file = fopen($p_sFile,"rb");
    108. $read = fread($file,10);
    109. while(!feof($file)&&($read<>""))
    110. $read .= fread($file,1024);
    111. $temp = unpack("H*",$read);
    112. $hex =  $temp[1];
    113. $header = substr($hex,0,108);
    114. if(substr($header,0,4)=="424d"){
    115. $header_parts = str_split($header,2);
    116. $width = hexdec($header_parts[19].$header_parts[18]);
    117. $height = hexdec($header_parts[23].$header_parts[22]);
    118. unset($header_parts);
    119. }
    120. $x =0;
    121. $y =1;
    122. $image = imagecreatetruecolor($width,$height);
    123. $body = substr($hex,108);
    124. $body_size =(strlen($body)/2);
    125. $header_size =($width*$height);
    126. $usePadding =($body_size>($header_size*3)+4);
    127. for($i=0;$i<$body_size;$i+=3){
    128. if($x>=$width){
    129. if($usePadding)
    130. $i    +=    $width%4;
    131. $x    =    0;
    132. $y++;
    133. if($y>$height)
    134. break;
    135. }
    136. $i_pos    =    $i*2;
    137. $r        =    hexdec($body[$i_pos+4].$body[$i_pos+5]);
    138. $g        =    hexdec($body[$i_pos+2].$body[$i_pos+3]);
    139. $b        =    hexdec($body[$i_pos].$body[$i_pos+1]);
    140. $color    =    imagecolorallocate($image,$r,$g,$b);
    141. imagesetpixel($image,$x,$height-$y,$color);
    142. $x++;
    143. }
    144. unset($body);
    145. return $image;
    146. }
    147. //图象目标地址  
    148. function dst_img()  
    149. {  
    150. $full_length  = strlen($this->srcimg);  
    151. $type_length  = strlen($this->type);  
    152. $name_length  = $full_length-$type_length;  
    153. $name         = substr($this->srcimg,0,$name_length-1);  
    154. $this->dstimg = $name.$this->extstr.'.'.$this->type;  
    155. }
    156. staticfunction get_url($img,$extstr){
    157. $imgs = explode('.',$img);
    158. $ext =end($imgs);
    159. $full_length  = strlen($img);  
    160. $type_length  = strlen($ext);  
    161. $name_length  = $full_length-$type_length;  
    162. $name         = substr($img,0,$name_length-1);  
    163. return $name.$extstr.'.'.$ext;  
    164. }
    165. }
    166. ?>
  • 相关阅读:
    学习第五天
    第四天学习
    学习第三天
    学校键盘键位设置
    学习第二天
    fatal error C1902: 程序数据库管理器不匹配;请检查安装解决
    ffmpeg遇到inttypes.h和UINT64_C
    <ZZ>linux yum命令详解
    <ZZ>Linux rpm 命令参数使用详解[介绍和应用]
    转:Windows下WSH/JS实现SVN服务器钩子脚本阻止提交空日志信息和垃圾文件
  • 原文地址:https://www.cnblogs.com/phpliu/p/2731133.html
Copyright © 2020-2023  润新知