/packages/Image/ImageCache/Managers/ImageCache.class.php

https://bitbucket.org/alexamiryan/stingle · PHP · 97 lines · 57 code · 11 blank · 29 comment · 9 complexity · 31cc8097b285c343d3e0ed535e4e4d23 MD5 · raw file

  1. <?php
  2. class ImageCache
  3. {
  4. protected $config;
  5. public function __construct(Config $config){
  6. $this->config = $config;
  7. ensurePathLastSlash($this->config->cacheDir);
  8. }
  9. /**
  10. * Generate image cache and return resulting path
  11. *
  12. * @param string $folderName
  13. * @param Image $image
  14. * @param boolean $forceGenerate
  15. * @throws InvalidArgumentException
  16. * @throws RuntimeException
  17. * @return string
  18. */
  19. public function generateImageCache($folderName, Image $image, $forceGenerate = false){
  20. if(!in_array($folderName, $this->config->folders->toArray())){
  21. throw new InvalidArgumentException("There is no such folder defined with name $folderName!");
  22. }
  23. if(!file_exists($this->config->cacheDir . $folderName)){
  24. throw new RuntimeException("There is no such folder $folderName in cache directory!");
  25. }
  26. $resultingFilePath = $this->config->cacheDir . $folderName . "/" . $image->fileName;
  27. if($forceGenerate or !file_exists($resultingFilePath)){
  28. switch ($image->getType()){
  29. case Image::IMAGE_TYPE_JPEG:
  30. $image->writeJpeg($resultingFilePath);
  31. break;
  32. case Image::IMAGE_TYPE_GIF:
  33. $image->writeGif($resultingFilePath);
  34. break;
  35. case Image::IMAGE_TYPE_PNG:
  36. $image->writePng($resultingFilePath);
  37. break;
  38. }
  39. }
  40. return $resultingFilePath;
  41. }
  42. /**
  43. * Checks if image cache is present
  44. *
  45. * @param string $folderName
  46. * @param string $fileName
  47. * @throws InvalidArgumentException
  48. * @throws RuntimeException
  49. * @return boolean
  50. */
  51. public function isImageCached($folderName, $fileName){
  52. if(!in_array($folderName, $this->config->folders->toArray())){
  53. throw new InvalidArgumentException("There is no such folder defined with name $folderName!");
  54. }
  55. if(!file_exists($this->config->cacheDir . $folderName)){
  56. throw new RuntimeException("There is no such folder $folderName in cache directory!");
  57. }
  58. $resultingFilePath = $this->config->cacheDir . $folderName . "/" . $fileName;
  59. if(file_exists($resultingFilePath)){
  60. return true;
  61. }
  62. return false;
  63. }
  64. /**
  65. * Clear cache of given photo filename
  66. * @param string $fileName
  67. */
  68. public function clearImageCache($fileName){
  69. if(empty($fileName)){
  70. throw new InvalidArgumentException("\$fileName is empty!");
  71. }
  72. foreach ($this->config->folders as $folderName){
  73. if(file_exists($this->config->cacheDir . $folderName . "/" . $fileName)){
  74. @unlink($this->config->cacheDir . $folderName . "/" . $fileName);
  75. }
  76. }
  77. }
  78. /**
  79. * Clear whole image cache
  80. */
  81. public function clearWholeImageCache(){
  82. foreach ($this->config->folders as $folderName){
  83. @unlink($this->config->cacheDir . $folderName . "/*");
  84. }
  85. }
  86. }