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

155 lines
4.9KB

  1. <?php
  2. if (!defined('DEDEINC')) exit('dedebiz');
  3. require_once DEDEINC."/libraries/imageresize.class.php";
  4. /**
  5. * 图像处理相关函数
  6. *
  7. * @version $id:image.func.php 15:59 2010年7月5日 tianya $
  8. * @package DedeBIZ.Helpers
  9. * @copyright Copyright (c) 2022 DedeBIZ.COM
  10. * @license https://www.dedebiz.com/license
  11. * @link https://www.dedebiz.com
  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. try{
  28. $image = new ImageResize($srcFile);
  29. $image->resizeToBestFit($toW, $toH);
  30. $image->save($toFile);
  31. return true;
  32. } catch (ImageResizeException $e) {
  33. return false;
  34. }
  35. }
  36. }
  37. /**
  38. * 获得GD的版本
  39. *
  40. * @access public
  41. * @return int
  42. */
  43. if (!function_exists('gdversion')) {
  44. function gdversion()
  45. {
  46. //没启用php.ini函数的情况下如果有GD默认视作2.0以上版本
  47. if (!function_exists('phpinfo')) {
  48. if (function_exists('imagecreate')) {
  49. return '2.0';
  50. } else {
  51. return 0;
  52. }
  53. } else {
  54. ob_start();
  55. phpinfo(8);
  56. $module_info = ob_get_contents();
  57. ob_end_clean();
  58. if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i", $module_info, $matches)) {
  59. $gdversion_h = $matches[1];
  60. } else {
  61. $gdversion_h = 0;
  62. }
  63. return $gdversion_h;
  64. }
  65. }
  66. }
  67. /**
  68. * 图片自动加水印函数
  69. *
  70. * @access public
  71. * @param string $srcFile 图片源文件
  72. * @param string $fromGo 位置
  73. * @return string
  74. */
  75. if (!function_exists('WaterImg')) {
  76. function WaterImg($srcFile, $fromGo = 'up')
  77. {
  78. include(DEDEDATA.'/mark/inc_photowatermark_config.php');
  79. require_once(DEDEINC.'/image.class.php');
  80. if (isset($GLOBALS['needwatermark'])) {
  81. $photo_markup = $photo_markdown = empty($GLOBALS['needwatermark']) ? '0' : '1';
  82. }
  83. if ($photo_markup != '1' || ($fromGo == 'collect' && $photo_markdown != '1')) {
  84. return;
  85. }
  86. $info = '';
  87. $srcInfo = @getimagesize($srcFile, $info);
  88. $srcFile_w = $srcInfo[0];
  89. $srcFile_h = $srcInfo[1];
  90. if ($srcFile_w < $photo_wwidth || $srcFile_h < $photo_wheight) {
  91. return;
  92. }
  93. if ($fromGo == 'up' && $photo_markup == '0') {
  94. return;
  95. }
  96. if ($fromGo == 'down' && $photo_markdown == '0') {
  97. return;
  98. }
  99. $TRUEMarkimg = DEDEDATA.'/mark/'.$photo_markimg;
  100. if (!file_exists($TRUEMarkimg) || empty($photo_markimg)) {
  101. $TRUEMarkimg = "";
  102. }
  103. if ($photo_waterpos == 0) {
  104. $photo_waterpos = rand(1, 9);
  105. }
  106. $cfg_watermarktext = array();
  107. if ($photo_marktype == '2') {
  108. if (file_exists(DEDEDATA.'/mark/simhei.ttf')) {
  109. $cfg_watermarktext['fontpath'] = DEDEDATA.'/mark/simhei.ttf';
  110. } else {
  111. return;
  112. }
  113. }
  114. $cfg_watermarktext['text'] = $photo_watertext;
  115. $cfg_watermarktext['size'] = $photo_fontsize;
  116. $cfg_watermarktext['angle'] = '0';
  117. $cfg_watermarktext['color'] = '255,255,255';
  118. $cfg_watermarktext['shadowx'] = '0';
  119. $cfg_watermarktext['shadowy'] = '0';
  120. $cfg_watermarktext['shadowcolor'] = '0,0,0';
  121. $photo_marktrans = 85;
  122. $img = new image($srcFile, 0, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $photo_marktype, $photo_marktrans, $TRUEMarkimg);
  123. $img->watermark(0);
  124. }
  125. }
  126. /**
  127. * 会对空白地方填充满
  128. *
  129. * @access public
  130. * @param string $srcFile 图片路径
  131. * @param string $toW 转换到的宽度
  132. * @param string $toH 转换到的高度
  133. * @param string $toFile 输出文件到
  134. * @param string $issave 是否保存
  135. * @return bool
  136. */
  137. if (!function_exists('ImageResizeNew')) {
  138. function ImageResizeNew($srcFile, $toW, $toH, $toFile = '', $issave = TRUE)
  139. {
  140. try{
  141. $image = new ImageResize($srcFile);
  142. $image->resizeToBestFit($toW, $toH);
  143. if ($issave) {
  144. $image->save($toFile);
  145. } else {
  146. $image->output();
  147. }
  148. return true;
  149. } catch (ImageResizeException $e) {
  150. return false;
  151. }
  152. }
  153. }