国内流行的内容管理系统(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.

314 lines
10KB

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