/library/WeFlex/Image/Processor.php

https://github.com/rocknoon/Stack · PHP · 169 lines · 125 code · 16 blank · 28 comment · 9 complexity · 890d5f12e2154d6bed614520dc950995 MD5 · raw file

  1. <?php
  2. /*---------------------------------------------------------------
  3. 功能: 图片操作类
  4. 作者: Rocky
  5. -----------------------------------------------------------------*/
  6. class WeFlex_Image_Processor
  7. {
  8. //==========================================
  9. // 函数: switchpic($sourFile,$width=128,$height=128)
  10. // 功能: 转换图片格式,输出指定大小.
  11. // 参数: $sourFile 图片源文件
  12. // 参数: $newName 生成的文件URL
  13. // 参数: $width 生成缩略图的宽度
  14. // 参数: $height 生成缩略图的高度
  15. // $backcolor = array( 111,111,111 ) 三原色
  16. // 返回: 0 失败 成功时返回生成的图片路径
  17. //==========================================
  18. public static function switchpic($sourFile , $width=128, $height=128 , $transparent = 0 , $backcolor = null , $focus = null )
  19. {
  20. /**
  21. * gener new file name
  22. * Enter description here ...
  23. * @var unknown_type
  24. */
  25. if( $transparent ){
  26. $newName = substr($sourFile,0,strrpos($sourFile,'.')).'_'.$width . 'x' . $height . ".png";
  27. }else{
  28. $newName = substr($sourFile,0,strrpos($sourFile,'.')).'_'.$width . 'x' . $height . substr($sourFile,strrpos($sourFile,'.'));
  29. }
  30. $dst_width = $width;
  31. $dst_height = $height;
  32. $imageInfo = self::getInfo($sourFile);
  33. switch ($imageInfo["type"])
  34. {
  35. case 1: //gif
  36. $img = imagecreatefromgif($sourFile);
  37. break;
  38. case 2: //jpg
  39. $img = imagecreatefromjpeg($sourFile);
  40. break;
  41. case 3: //png
  42. $img = imagecreatefrompng($sourFile);
  43. break;
  44. default:
  45. return 0;
  46. break;
  47. }
  48. if (!$img)
  49. return 0;
  50. //$width = ($width > $imageInfo["width"]) ? $imageInfo["width"] : $width;
  51. //$height = ($height > $imageInfo["height"]) ? $imageInfo["height"] : $height;
  52. $srcW = $imageInfo["width"];
  53. $srcH = $imageInfo["height"];
  54. /*
  55. if ($srcW * $width > $srcH * $height)
  56. $height = round($srcH * $width / $srcW);
  57. else
  58. $width = round($srcW * $height / $srcH);
  59. */
  60. if( $focus == 'width' ){
  61. $height = round($srcH * $width / $srcW);
  62. $dst_height = $height;
  63. }elseif( $focus == 'height' ){
  64. $width = round($srcW * $height / $srcH);
  65. $dst_width = $width;
  66. }else{
  67. if ($srcW / $width > $srcH / $height)
  68. $height = round($srcH * $width / $srcW);
  69. else
  70. $width = round($srcW * $height / $srcH);
  71. }
  72. if (function_exists("imagecreatetruecolor")) //GD2.0.1
  73. {
  74. $new = imagecreatetruecolor($dst_width, $dst_height);
  75. $offset_x = ($dst_width-$width) / 2;
  76. $offset_y = ($dst_height-$height) / 2;
  77. $backcolor = self::_getbackcolor( $new , $backcolor);
  78. imagefill($new, 0, 0, $backcolor);
  79. if( $transparent ){
  80. //set back color transparent
  81. imagecolortransparent( $new , $backcolor );
  82. }
  83. ImageCopyResampled($new, $img, $offset_x, $offset_y, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
  84. }
  85. else
  86. {
  87. throw new Exception( "gd library doesn't have imagecreatetruecolor function" );
  88. // $new = imagecreate($dst_width, $dst_height);
  89. // $offset_x = ($dst_width-$width) / 2;
  90. // $offset_y = ($dst_height-$height) / 2;
  91. // ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
  92. }
  93. //*/
  94. if (file_exists($newName)) unlink($newName);
  95. $maketype = strtolower(substr(strrchr($newName,"."),1));
  96. switch($maketype)
  97. {
  98. case "jpg": ImageJPEG($new, $newName , 100);break;
  99. case "gif" : ImageGIF($new, $newName);break;
  100. case "png" : ImagePNG($new, $newName , 9);break;
  101. case "wbmp" : ImageWBMP($new, $newName);break;
  102. default: ImageJPEG($new, $newName);
  103. }
  104. ImageDestroy($new);
  105. ImageDestroy($img);
  106. chmod($newName,0777);
  107. return $newName;
  108. }
  109. public static function getInfo($file)
  110. {
  111. $data = getimagesize($file);
  112. $imageInfo["width"] = $data[0];
  113. $imageInfo["height"]= $data[1];
  114. $imageInfo["type"] = $data[2];
  115. $imageInfo["name"] = basename($file);
  116. //$imageInfo["size"] = filesize($file);
  117. return $imageInfo;
  118. }
  119. private static function _getbackcolor( $new , $backcolor = null ){
  120. switch( $backcolor ){
  121. case "white":
  122. $white = imagecolorallocate($new, 255, 255, 255);
  123. return $white;
  124. break;
  125. case "black":
  126. $black = imagecolorallocate($new, 0, 0, 0);
  127. return $black;
  128. break;
  129. default:
  130. $white = imagecolorallocate($new, 255, 255, 255);
  131. return $white;
  132. break;
  133. }
  134. }
  135. }
  136. ?>