ImageWatermarkTemplate.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace Qcloud\Cos\ImageParamTemplate;
  3. class ImageWatermarkTemplate extends ImageTemplate
  4. {
  5. private $image;
  6. private $gravity;
  7. private $dx;
  8. private $dy;
  9. private $blogo;
  10. private $scatype;
  11. private $spcent;
  12. private $dissolve;
  13. private $batch;
  14. private $degree;
  15. public function __construct() {
  16. parent::__construct();
  17. $this->image = "";
  18. $this->gravity = "";
  19. $this->dx = "";
  20. $this->dy = "";
  21. $this->blogo = "";
  22. $this->scatype = "";
  23. $this->spcent = "";
  24. $this->dissolve = "";
  25. $this->batch = "";
  26. $this->degree = "";
  27. }
  28. /**
  29. * 水印图片地址,需要经过 URL 安全的 Base64 编码。
  30. * @param $value
  31. */
  32. public function setImage($value) {
  33. $this->image = "/image/" . $this->ciBase64($value);
  34. }
  35. /**
  36. * 图片水印位置,九宫格位置(参考九宫格方位图 ),默认值 SouthEast
  37. * @param $value
  38. */
  39. public function setGravity($value) {
  40. $this->gravity = "/gravity/" . $value;
  41. }
  42. /**
  43. * 水平(横轴)边距,单位为像素,缺省值为0
  44. * @param $value
  45. */
  46. public function setDx($value) {
  47. $this->dx = "/dx/" . $value;
  48. }
  49. /**
  50. * 垂直(纵轴)边距,单位为像素,默认值为0
  51. * @param $value
  52. */
  53. public function setDy($value) {
  54. $this->dy = "/dy/" . $value;
  55. }
  56. /**
  57. * 水印图适配功能,适用于水印图尺寸过大的场景(如水印墙)。共有两种类型:
  58. * 当 blogo 设置为1时,水印图会被缩放至与原图相似大小后添加
  59. * 当 blogo 设置为2时,水印图会被直接裁剪至与原图相似大小后添加
  60. * @param $value
  61. */
  62. public function setBlogo($value) {
  63. $this->blogo = "/blogo/" . $value;
  64. }
  65. /**
  66. * 根据原图的大小,缩放调整水印图的大小:
  67. * 当 scatype 设置为1时,按原图的宽缩放
  68. * 当 scatype 设置为2时,按原图的高缩放
  69. * 当 scatype 设置为3时,按原图的整体面积缩放
  70. * @param $value
  71. */
  72. public function setScatype($value) {
  73. $this->scatype = "/scatype/" . $value;
  74. }
  75. /**
  76. * 与 scatype 搭配使用:
  77. * 当 scatype 设置为1时,该有效值为[1,1000],单位为千分比
  78. * 当 scatype 设置为2时,该有效值为[1,1000],单位为千分比
  79. * 当 scatype 设置为3时,该有效值为[1,250],单位为千分比。
  80. * @param $value
  81. */
  82. public function setSpcent($value) {
  83. $this->spcent = "/spcent/" . $value;
  84. }
  85. /**
  86. * 图片水印的透明度,取值为1 - 100,默认90(90%不透明度)
  87. * @param $value
  88. */
  89. public function setDissolve($value) {
  90. $this->dissolve = "/dissolve/" . $value;
  91. }
  92. /**
  93. * 平铺水印功能,可将图片水印平铺至整张图片。值为1时,表示开启平铺水印功能
  94. * @param $value
  95. */
  96. public function setBatch($value) {
  97. $this->batch = "/batch/" . $value;
  98. }
  99. /**
  100. * 当 batch 值为1时生效。图片水印的旋转角度设置,取值范围为0 - 360,默认0
  101. * @param $value
  102. */
  103. public function setDegree($value) {
  104. $this->degree = "/degree/" . $value;
  105. }
  106. public function getImage() {
  107. return $this->image;
  108. }
  109. public function getGravity() {
  110. return $this->gravity;
  111. }
  112. public function getDx() {
  113. return $this->dx;
  114. }
  115. public function getDy() {
  116. return $this->dy;
  117. }
  118. public function getBlogo() {
  119. return $this->blogo;
  120. }
  121. public function getScatype() {
  122. return $this->scatype;
  123. }
  124. public function getSpcent() {
  125. return $this->spcent;
  126. }
  127. public function getDissolve() {
  128. return $this->dissolve;
  129. }
  130. public function getBatch() {
  131. return $this->batch;
  132. }
  133. public function getDegree() {
  134. return $this->degree;
  135. }
  136. public function queryString() {
  137. $head = "watermark/1";
  138. $res = "";
  139. if($this->image) {
  140. $res .= $this->image;
  141. }
  142. if($this->gravity) {
  143. $res .= $this->gravity;
  144. }
  145. if($this->dx) {
  146. $res .= $this->dx;
  147. }
  148. if($this->dy) {
  149. $res .= $this->dy;
  150. }
  151. if($this->blogo) {
  152. $res .= $this->blogo;
  153. }
  154. if($this->scatype) {
  155. $res .= $this->scatype;
  156. }
  157. if($this->spcent) {
  158. $res .= $this->spcent;
  159. }
  160. if($this->dissolve) {
  161. $res .= $this->dissolve;
  162. }
  163. if($this->batch) {
  164. $res .= $this->batch;
  165. }
  166. if($this->degree) {
  167. $res .= $this->degree;
  168. }
  169. if($res) {
  170. $res = $head . $res;
  171. }
  172. return $res;
  173. }
  174. public function resetRule() {
  175. $this->image = "";
  176. $this->gravity = "";
  177. $this->dx = "";
  178. $this->dy = "";
  179. $this->blogo = "";
  180. $this->scatype = "";
  181. $this->spcent = "";
  182. $this->dissolve = "";
  183. $this->batch = "";
  184. $this->degree = "";
  185. }
  186. }