/src/vendor/forxer/gravatar/src/Image.php

https://gitlab.com/tunisiano187/PhpNuget · PHP · 465 lines · 212 code · 69 blank · 184 comment · 21 complexity · 335504ca7ad67814cecd50d9345f9586 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of forxer\Gravatar.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace forxer\Gravatar;
  9. use Exception\InvalidDefaultImageException;
  10. use Exception\InvalidImageExtensionException;
  11. use Exception\InvalidImageSizeException;
  12. use Exception\InvalidMaxRatingImageException;
  13. class Image extends Gravatar
  14. {
  15. /**
  16. * @var integer The size to use for avatars.
  17. */
  18. protected $iSize;
  19. /**
  20. * @var string The default image to use ; either a string of
  21. * the gravatar recognized default image "type" to use, or a URL
  22. */
  23. protected $sDefaultImage;
  24. /**
  25. * @var array List of accepted gravatar recognized default image "type".
  26. */
  27. protected $aValidDefaultsImages = ['404', 'mm', 'identicon', 'monsterid', 'wavatar', 'retro', 'blank'];
  28. /**
  29. * @var string The maximum rating to allow for the avatars.
  30. */
  31. protected $sMaxRating;
  32. /**
  33. * @var array List of accepted ratings.
  34. */
  35. protected $aValidRatings = ['g', 'pg', 'r', 'x'];
  36. /**
  37. * @var string The extension to append to the avatars URL.
  38. */
  39. protected $sExtension;
  40. /**
  41. * @var array List of accepted extensions.
  42. */
  43. protected $aValidExtensions = ['jpg', 'jpeg', 'gif', 'png'];
  44. /**
  45. * @var boolean Should we force the default image to always load?
  46. */
  47. protected $bForceDefault = false;
  48. /**
  49. * Build the avatar URL based on the provided email address.
  50. *
  51. * @param string $sEmail The email to get the gravatar for.
  52. * @return string The URL to the gravatar.
  53. */
  54. public function getUrl($sEmail = null)
  55. {
  56. if (null !== $sEmail) {
  57. $this->setEmail($sEmail);
  58. }
  59. return static::URL
  60. .'avatar/'
  61. .$this->getHash($this->getEmail())
  62. .$this->getParams();
  63. }
  64. /**
  65. * Return the avatar URL when this object is printed.
  66. *
  67. * @return string The URL to the gravatar.
  68. */
  69. public function __toString()
  70. {
  71. return $this->getUrl();
  72. }
  73. /**
  74. * Get or set the avatar size to use.
  75. *
  76. * @param integer $iSize The avatar size to use, must be less than 2048 and greater than 0.
  77. * @return number|\forxer\Gravatar\Image
  78. */
  79. public function size($iSize = null)
  80. {
  81. if (null === $iSize) {
  82. return $this->getSize();
  83. }
  84. return $this->setSize($iSize);
  85. }
  86. /**
  87. * Alias for the "size" method.
  88. *
  89. * @param integer $iSize The avatar size to use, must be less than 2048 and greater than 0.
  90. * @return number|\forxer\Gravatar\Image
  91. */
  92. public function s($iSize= null)
  93. {
  94. return $this->size($iSize);
  95. }
  96. /**
  97. * Get the currently set avatar size.
  98. *
  99. * @return integer The current avatar size in use.
  100. */
  101. public function getSize()
  102. {
  103. return $this->iSize;
  104. }
  105. /**
  106. * Set the avatar size to use.
  107. *
  108. * @param integer $size The avatar size to use, must be less than 2048 and greater than 0.
  109. * @throws InvalidImageSizeException
  110. * @return \forxer\Gravatar\Image The current Gravatar Image instance.
  111. */
  112. public function setSize($iSize = null)
  113. {
  114. if (null === $iSize) {
  115. return $this;
  116. }
  117. $iSize = intval($iSize);
  118. if ($iSize < 0 || $iSize > 2048) {
  119. throw new InvalidImageSizeException('Avatar size must be within 0 pixels and 2048 pixels');
  120. }
  121. $this->iSize = $iSize;
  122. return $this;
  123. }
  124. /**
  125. * Get or set the default image to use for avatars.
  126. *
  127. * @param string $sDefaultImage The default image to use. Use a valid image URL, or a recognized gravatar "default".
  128. * @param boolean $bForce Force the default image to be always load.
  129. * @return number|\forxer\Gravatar\Image
  130. */
  131. public function defaultImage($sDefaultImage = null, $bForce = false)
  132. {
  133. if (null === $sDefaultImage) {
  134. return $this->getDefaultImage();
  135. }
  136. return $this->setDefaultImage($sDefaultImage, $bForce);
  137. }
  138. /**
  139. * Alias for the "defaultImage" method.
  140. *
  141. * @param string $sDefaultImage The default image to use. Use a valid image URL, or a recognized gravatar "default".
  142. * @param boolean $bForce Force the default image to be always load.
  143. * @return number|\forxer\Gravatar\Image
  144. */
  145. public function d($sDefaultImage = null, $bForce = false)
  146. {
  147. return $this->defaultImage($sDefaultImage, $bForce);
  148. }
  149. /**
  150. * Get the current default image setting.
  151. *
  152. * @return string Default image.
  153. */
  154. public function getDefaultImage()
  155. {
  156. return $this->sDefaultImage;
  157. }
  158. /**
  159. * Set the default image to use for avatars.
  160. *
  161. * @param string $sDefaultImage The default image to use. Use a valid image URL, or a recognized gravatar "default".
  162. * @param boolean $bForce Force the default image to be always load.
  163. * @throws InvalidDefaultImageException
  164. * @return \forxer\Gravatar\Image The current Gravatar Image instance.
  165. */
  166. public function setDefaultImage($sDefaultImage = null, $bForce = false)
  167. {
  168. if (true === $bForce) {
  169. $this->enableForceDefault();
  170. }
  171. if (null === $sDefaultImage) {
  172. return $this;
  173. }
  174. $_image = strtolower($sDefaultImage);
  175. if (in_array($_image, $this->aValidDefaultsImages)) {
  176. $this->sDefaultImage = $_image;
  177. return $this;
  178. }
  179. if (filter_var($sDefaultImage, FILTER_VALIDATE_URL)) {
  180. $this->sDefaultImage = $sDefaultImage;
  181. return $this;
  182. }
  183. $message = sprintf(
  184. 'The default image "%s" is not a recognized gravatar "default" and is not a valid URL, default gravatar can be: %s',
  185. $sDefaultImage,
  186. implode(', ', $this->aValidDefaultsImages)
  187. );
  188. throw new InvalidDefaultImageException($message);
  189. }
  190. /**
  191. * Get or set if we have to force the default image to be always load.
  192. *
  193. * @param boolean $sRating
  194. * @return boolean|\forxer\Gravatar\Image
  195. */
  196. public function forceDefault($bForceDefault = null)
  197. {
  198. if (null === $bForceDefault) {
  199. return $this->getForceDefault();
  200. }
  201. return $this->setForceDefault($bForceDefault);
  202. }
  203. /**
  204. * Alias for the "forceDefault" method.
  205. *
  206. * @param boolean $bForceDefault
  207. * @return boolean|\forxer\Gravatar\Image
  208. */
  209. public function f($bForceDefault = null)
  210. {
  211. return $this->forceDefault($bForceDefault);
  212. }
  213. /**
  214. * Check if we are forcing the default image to be always load.
  215. *
  216. * @return boolean Are we forcing the default image?
  217. */
  218. public function getForceDefault()
  219. {
  220. return $this->bForceDefault;
  221. }
  222. /**
  223. * Alias for the "getForceDefault" method.
  224. *
  225. * @return boolean Are we forcing the default image?
  226. */
  227. public function forcingDefault()
  228. {
  229. return $this->getForceDefault();
  230. }
  231. /**
  232. * Set if the default image has to be always load.
  233. *
  234. * @param boolean $bForceDefault Should we force or not the default image to be always load.
  235. * @return \forxer\Gravatar\Image The current Gravatar Image instance.
  236. */
  237. public function setForceDefault($bForceDefault = false)
  238. {
  239. $this->bForceDefault = (bool)$bForceDefault;
  240. return $this;
  241. }
  242. /**
  243. * Force the default image to be always load.
  244. *
  245. * @return \forxer\Gravatar\Image The current Gravatar Image instance.
  246. */
  247. public function enableForceDefault()
  248. {
  249. $this->setForceDefault(true);
  250. return $this;
  251. }
  252. /**
  253. * Do not force the default image to be always load.
  254. *
  255. * @return \forxer\Gravatar\Image The current Gravatar Image instance.
  256. */
  257. public function disableForceDefault()
  258. {
  259. $this->setForceDefault(false);
  260. return $this;
  261. }
  262. /**
  263. * Get or set the maximum allowed rating for avatars.
  264. *
  265. * @param string $sRating
  266. * @return string|\forxer\Gravatar\Image
  267. */
  268. public function rating($sRating = null)
  269. {
  270. if (null === $sRating) {
  271. return $this->getMaxRating();
  272. }
  273. return $this->setMaxRating($sRating);
  274. }
  275. /**
  276. * Alias for the "rating" method.
  277. *
  278. * @param string $sRating
  279. * @return string|\forxer\Gravatar\Image
  280. */
  281. public function r($sRating = null)
  282. {
  283. return $this->rating($sRating);
  284. }
  285. /**
  286. * Get the current maximum allowed rating for avatars.
  287. *
  288. * @return string The string representing the current maximum allowed rating.
  289. */
  290. public function getMaxRating()
  291. {
  292. return $this->sMaxRating;
  293. }
  294. /**
  295. * Set the maximum allowed rating for avatars.
  296. *
  297. * @param string $sRating The maximum rating to use for avatars.
  298. * @throws InvalidMaxRatingImageException
  299. * @return \forxer\Gravatar\Image The current Gravatar Image instance.
  300. */
  301. public function setMaxRating($sRating = null)
  302. {
  303. if (null === $sRating) {
  304. return $this;
  305. }
  306. $sRating = strtolower($sRating);
  307. if (!in_array($sRating, $this->aValidRatings)) {
  308. $message = sprintf(
  309. 'Invalid rating "%s" specified, only allowed to be used are: %s',
  310. $sRating,
  311. implode(', ', $this->aValidRatings)
  312. );
  313. throw new InvalidMaxRatingImageException($message);
  314. }
  315. $this->sMaxRating = $sRating;
  316. return $this;
  317. }
  318. /**
  319. * Get or set the avatar extension to use.
  320. *
  321. * @param string $sExtension The avatar extension to use.
  322. * @return string|\forxer\Gravatar\Image
  323. */
  324. public function extension($sExtension = null)
  325. {
  326. if (null === $sExtension) {
  327. return $this->getExtension();
  328. }
  329. return $this->setExtension($sExtension);
  330. }
  331. /**
  332. * Alias for the "extension" method.
  333. *
  334. * @param string $sExtension
  335. * @return string|\forxer\Gravatar\Image
  336. */
  337. public function e($sExtension= null)
  338. {
  339. return $this->extension($sExtension);
  340. }
  341. /**
  342. * Get the currently set avatar extension.
  343. *
  344. * @return integer The current avatar extension in use
  345. */
  346. public function getExtension()
  347. {
  348. return $this->sImageExtension;
  349. }
  350. /**
  351. * Set the avatar extension to use.
  352. *
  353. * @param string $sExtension The avatar extension to use.
  354. * @throws InvalidImageExtensionException
  355. * @return \forxer\Gravatar\Image The current Gravatar Image instance.
  356. */
  357. public function setExtension($sExtension = null)
  358. {
  359. if (null === $sExtension) {
  360. return $this;
  361. }
  362. if (!in_array($sExtension, $this->aValidExtensions)) {
  363. $message = sprintf(
  364. 'The extension "%s" is not a valid one, extension image for Gravatar can be: %s',
  365. $sExtension,
  366. implode(', ', $this->aValidExtensions)
  367. );
  368. throw new InvalidImageExtensionException($message);
  369. }
  370. $this->sExtension = $sExtension;
  371. return $this;
  372. }
  373. /**
  374. * Compute params according to current settings.
  375. *
  376. * @return string
  377. */
  378. protected function getParams()
  379. {
  380. $aParams = [];
  381. if (null !== $this->getSize()) {
  382. $aParams['s'] = $this->getSize();
  383. }
  384. if (null !== $this->getDefaultImage()) {
  385. $aParams['d'] = $this->getDefaultImage();
  386. }
  387. if (null !== $this->getMaxRating()) {
  388. $aParams['r'] = $this->getMaxRating();
  389. }
  390. if ($this->forcingDefault()) {
  391. $aParams['f'] = 'y';
  392. }
  393. return (null !== $this->sExtension ? '.'.$this->sExtension : '')
  394. .(empty($aParams) ? '' : '?'.http_build_query($aParams, '', '&amp;'));
  395. }
  396. }