国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

313 lines
10KB

  1. <?php if (!defined('DEDEINC')) exit("Request Error!");
  2. /**
  3. * 图像处理相关函数
  4. *
  5. * @version $Id: image.func.php 1 15:59 2010年7月5日Z tianya $
  6. * @package DedeBIZ.Helpers
  7. * @copyright Copyright (c) 2021, DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license
  9. * @link https://www.dedebiz.com
  10. */
  11. // ------------------------------------------------------------------------
  12. /**
  13. * 缩图片自动生成函数,来源支持bmp、gif、jpg、png
  14. * 但生成的小图只用jpg或png格式
  15. *
  16. * @access public
  17. * @param string $srcFile 图片路径
  18. * @param string $toW 转换到的宽度
  19. * @param string $toH 转换到的高度
  20. * @param string $toFile 输出文件到
  21. * @return string
  22. */
  23. if (!function_exists('ImageResize')) {
  24. function ImageResize($srcFile, $toW, $toH, $toFile = "")
  25. {
  26. global $cfg_photo_type;
  27. if ($toFile == '') $toFile = $srcFile;
  28. $info = '';
  29. $srcInfo = GetImageSize($srcFile, $info);
  30. switch ($srcInfo[2]) {
  31. case 1:
  32. if (!$cfg_photo_type['gif']) return FALSE;
  33. $im = imagecreatefromgif($srcFile);
  34. break;
  35. case 2:
  36. if (!$cfg_photo_type['jpeg']) return FALSE;
  37. $im = imagecreatefromjpeg($srcFile);
  38. break;
  39. case 3:
  40. if (!$cfg_photo_type['png']) return FALSE;
  41. $im = imagecreatefrompng($srcFile);
  42. break;
  43. case 6:
  44. if (!$cfg_photo_type['bmp']) return FALSE;
  45. $im = imagecreatefromwbmp($srcFile);
  46. break;
  47. }
  48. $srcW = ImageSX($im);
  49. $srcH = ImageSY($im);
  50. if ($srcW <= $toW && $srcH <= $toH) return TRUE;
  51. $toWH = $toW / $toH;
  52. $srcWH = $srcW / $srcH;
  53. if ($toWH <= $srcWH) {
  54. $ftoW = $toW;
  55. $ftoH = $ftoW * ($srcH / $srcW);
  56. } else {
  57. $ftoH = $toH;
  58. $ftoW = $ftoH * ($srcW / $srcH);
  59. }
  60. if ($srcW > $toW || $srcH > $toH) {
  61. if (function_exists("imagecreateTRUEcolor")) {
  62. @$ni = imagecreateTRUEcolor($ftoW, $ftoH);
  63. if ($ni) {
  64. imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
  65. } else {
  66. $ni = imagecreate($ftoW, $ftoH);
  67. imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
  68. }
  69. } else {
  70. $ni = imagecreate($ftoW, $ftoH);
  71. imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
  72. }
  73. switch ($srcInfo[2]) {
  74. case 1:
  75. imagegif($ni, $toFile);
  76. break;
  77. case 2:
  78. imagejpeg($ni, $toFile, 85);
  79. break;
  80. case 3:
  81. imagepng($ni, $toFile);
  82. break;
  83. case 6:
  84. imagebmp($ni, $toFile);
  85. break;
  86. default:
  87. return FALSE;
  88. }
  89. imagedestroy($ni);
  90. }
  91. imagedestroy($im);
  92. return TRUE;
  93. }
  94. }
  95. /**
  96. * 获得GD的版本
  97. *
  98. * @access public
  99. * @return int
  100. */
  101. if (!function_exists('gdversion')) {
  102. function gdversion()
  103. {
  104. //没启用php.ini函数的情况下如果有GD默认视作2.0以上版本
  105. if (!function_exists('phpinfo')) {
  106. if (function_exists('imagecreate')) {
  107. return '2.0';
  108. } else {
  109. return 0;
  110. }
  111. } else {
  112. ob_start();
  113. phpinfo(8);
  114. $module_info = ob_get_contents();
  115. ob_end_clean();
  116. if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i", $module_info, $matches)) {
  117. $gdversion_h = $matches[1];
  118. } else {
  119. $gdversion_h = 0;
  120. }
  121. return $gdversion_h;
  122. }
  123. }
  124. }
  125. /**
  126. * 图片自动加水印函数
  127. *
  128. * @access public
  129. * @param string $srcFile 图片源文件
  130. * @param string $fromGo 位置
  131. * @return string
  132. */
  133. if (!function_exists('WaterImg')) {
  134. function WaterImg($srcFile, $fromGo = 'up')
  135. {
  136. include(DEDEDATA . '/mark/inc_photowatermark_config.php');
  137. require_once(DEDEINC . '/image.class.php');
  138. if (isset($GLOBALS['needwatermark'])) {
  139. $photo_markup = $photo_markdown = empty($GLOBALS['needwatermark']) ? '0' : '1';
  140. }
  141. if ($photo_markup != '1' || ($fromGo == 'collect' && $photo_markdown != '1')) {
  142. return;
  143. }
  144. $info = '';
  145. $srcInfo = @getimagesize($srcFile, $info);
  146. $srcFile_w = $srcInfo[0];
  147. $srcFile_h = $srcInfo[1];
  148. if ($srcFile_w < $photo_wwidth || $srcFile_h < $photo_wheight) {
  149. return;
  150. }
  151. if ($fromGo == 'up' && $photo_markup == '0') {
  152. return;
  153. }
  154. if ($fromGo == 'down' && $photo_markdown == '0') {
  155. return;
  156. }
  157. $TRUEMarkimg = DEDEDATA . '/mark/' . $photo_markimg;
  158. if (!file_exists($TRUEMarkimg) || empty($photo_markimg)) {
  159. $TRUEMarkimg = "";
  160. }
  161. if ($photo_waterpos == 0) {
  162. $photo_waterpos = rand(1, 9);
  163. }
  164. $cfg_watermarktext = array();
  165. if ($photo_marktype == '2') {
  166. if (file_exists(DEDEDATA . '/mark/simhei.ttf')) {
  167. $cfg_watermarktext['fontpath'] = DEDEDATA . '/mark/simhei.ttf';
  168. } else {
  169. return;
  170. }
  171. }
  172. $cfg_watermarktext['text'] = $photo_watertext;
  173. $cfg_watermarktext['size'] = $photo_fontsize;
  174. $cfg_watermarktext['angle'] = '0';
  175. $cfg_watermarktext['color'] = '255,255,255';
  176. $cfg_watermarktext['shadowx'] = '0';
  177. $cfg_watermarktext['shadowy'] = '0';
  178. $cfg_watermarktext['shadowcolor'] = '0,0,0';
  179. $photo_marktrans = 85;
  180. $img = new image($srcFile, 0, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $photo_marktype, $photo_marktrans, $TRUEMarkimg);
  181. $img->watermark(0);
  182. }
  183. }
  184. /**
  185. * 会对空白地方填充满
  186. *
  187. * @access public
  188. * @param string $srcFile 图片路径
  189. * @param string $toW 转换到的宽度
  190. * @param string $toH 转换到的高度
  191. * @param string $toFile 输出文件到
  192. * @param string $issave 是否保存
  193. * @return bool
  194. */
  195. if (!function_exists('ImageResizeNew')) {
  196. function ImageResizeNew($srcFile, $toW, $toH, $toFile = '', $issave = TRUE)
  197. {
  198. global $cfg_photo_type, $cfg_ddimg_bgcolor;
  199. if ($toFile == '') $toFile = $srcFile;
  200. $info = '';
  201. $srcInfo = GetImageSize($srcFile, $info);
  202. switch ($srcInfo[2]) {
  203. case 1:
  204. if (!$cfg_photo_type['gif']) return FALSE;
  205. $img = imagecreatefromgif($srcFile);
  206. break;
  207. case 2:
  208. if (!$cfg_photo_type['jpeg']) return FALSE;
  209. $img = imagecreatefromjpeg($srcFile);
  210. break;
  211. case 3:
  212. if (!$cfg_photo_type['png']) return FALSE;
  213. $img = imagecreatefrompng($srcFile);
  214. break;
  215. case 6:
  216. if (!$cfg_photo_type['bmp']) return FALSE;
  217. $img = imagecreatefromwbmp($srcFile);
  218. break;
  219. }
  220. $width = imageSX($img);
  221. $height = imageSY($img);
  222. if (!$width || !$height) {
  223. return FALSE;
  224. }
  225. $target_width = $toW;
  226. $target_height = $toH;
  227. $target_ratio = $target_width / $target_height;
  228. $img_ratio = $width / $height;
  229. if ($target_ratio > $img_ratio) {
  230. $new_height = $target_height;
  231. $new_width = $img_ratio * $target_height;
  232. } else {
  233. $new_height = $target_width / $img_ratio;
  234. $new_width = $target_width;
  235. }
  236. if ($new_height > $target_height) {
  237. $new_height = $target_height;
  238. }
  239. if ($new_width > $target_width) {
  240. $new_height = $target_width;
  241. }
  242. $new_img = ImageCreateTrueColor($target_width, $target_height);
  243. if ($cfg_ddimg_bgcolor == 0) $bgcolor = ImageColorAllocate($new_img, 0xff, 0xff, 0xff);
  244. else $bgcolor = 0;
  245. if (!@imagefilledrectangle($new_img, 0, 0, $target_width - 1, $target_height - 1, $bgcolor)) {
  246. return FALSE;
  247. }
  248. if (!@imagecopyresampled($new_img, $img, ($target_width - $new_width) / 2, ($target_height - $new_height) / 2, 0, 0, $new_width, $new_height, $width, $height)) {
  249. return FALSE;
  250. }
  251. //保存为目标文件
  252. if ($issave) {
  253. switch ($srcInfo[2]) {
  254. case 1:
  255. imagegif($new_img, $toFile);
  256. break;
  257. case 2:
  258. imagejpeg($new_img, $toFile, 100);
  259. break;
  260. case 3:
  261. imagepng($new_img, $toFile);
  262. break;
  263. case 6:
  264. imagebmp($new_img, $toFile);
  265. break;
  266. default:
  267. return FALSE;
  268. }
  269. }
  270. //不保存
  271. else {
  272. switch ($srcInfo[2]) {
  273. case 1:
  274. imagegif($new_img);
  275. break;
  276. case 2:
  277. imagejpeg($new_img);
  278. break;
  279. case 3:
  280. imagepng($new_img);
  281. break;
  282. case 6:
  283. imagebmp($new_img);
  284. break;
  285. default:
  286. return FALSE;
  287. }
  288. }
  289. imagedestroy($new_img);
  290. imagedestroy($img);
  291. return TRUE;
  292. }
  293. }