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.

142 lines
4.0 KiB

  1. <?php
  2. class Picture {
  3. private $_path = CACHE_PATH;
  4. private $_uri = CACHE_URI;
  5. private $_key;
  6. private $_bin = false;
  7. /**
  8. * @desc Load a bin picture from a path
  9. */
  10. public function fromPath($path) {
  11. $handle = fopen($path, "r");
  12. $this->_bin = fread($handle, filesize($path));
  13. fclose($handle);
  14. }
  15. /**
  16. * @desc Load a bin picture from a base64
  17. */
  18. public function fromBase($base = false) {
  19. if($base) {
  20. $this->_bin = (string)base64_decode((string)$base);
  21. }
  22. }
  23. /**
  24. * @desc Convert to a base64
  25. */
  26. public function toBase() {
  27. if($this->_bin)
  28. return base64_encode($this->_bin);
  29. else
  30. return false;
  31. }
  32. /**
  33. * @desc Get a picture of the current size
  34. * @param $key The key of the picture
  35. * @param $size The size requested
  36. * @return The url of the picture
  37. */
  38. public function get($key, $size = false) {
  39. $this->_key = $key;
  40. $original = $this->_path.md5($this->_key).'.jpg';
  41. // We request the original picture
  42. if($size == false) {
  43. if(file_exists($original)) {
  44. $this->fromPath($original);
  45. return $this->_uri.md5($this->_key).'.jpg';
  46. } else {
  47. return false;
  48. }
  49. // We request a specific size
  50. } else {
  51. if(file_exists($this->_path.md5($this->_key).'_'.$size.'.jpg')) {
  52. $this->fromPath($this->_path.md5($this->_key).'_'.$size.'.jpg');
  53. return $this->_uri.md5($this->_key).'_'.$size.'.jpg';
  54. } else {
  55. if(file_exists($original)) {
  56. $this->fromPath($original);
  57. $this->createThumbnail($size);
  58. return $this->_uri.md5($this->_key).'_'.$size.'.jpg';
  59. } else {
  60. return false;
  61. }
  62. }
  63. }
  64. }
  65. /**
  66. * @desc Save a picture (original size)
  67. * @param $key The key of the picture
  68. */
  69. public function set($key) {
  70. $this->_key = $key;
  71. $path = $this->_path.md5($this->_key).'.jpg';
  72. // If the file exist we replace it
  73. if(file_exists($path) && $this->_bin) {
  74. unlink($path);
  75. // And destroy all the thumbnails
  76. foreach(
  77. glob(
  78. $this->_path.
  79. md5($key).
  80. '*.jpg',
  81. GLOB_NOSORT
  82. ) as $path_thumb) {
  83. unlink($path_thumb);
  84. }
  85. }
  86. if($this->_bin) {
  87. $source = imagecreatefromstring($this->_bin);
  88. if($source != false) {
  89. imagejpeg($source, $path, 95);
  90. imagedestroy($source);
  91. }
  92. }
  93. }
  94. /**
  95. * @desc Create a thumbnail of the picture and save it
  96. * @param $size The size requested
  97. */
  98. private function createThumbnail($size) {
  99. $path = $this->_path.md5($this->_key).'_'.$size.'.jpg';
  100. $thumb = imagecreatetruecolor($size, $size);
  101. $white = imagecolorallocate($thumb, 255, 255, 255);
  102. imagefill($thumb, 0, 0, $white);
  103. imageinterlace($thumb, true);
  104. $source = imagecreatefromstring($this->_bin);
  105. $width = imagesx($source);
  106. $height = imagesy($source);
  107. if($width >= $height) {
  108. // For landscape images
  109. $x_offset = ($width - $height) / 2;
  110. $y_offset = 0;
  111. $square_size = $width - ($x_offset * 2);
  112. } else {
  113. // For portrait and square images
  114. $x_offset = 0;
  115. $y_offset = ($height - $width) / 2;
  116. $square_size = $height - ($y_offset * 2);
  117. }
  118. if($source) {
  119. imagecopyresampled($thumb, $source, 0, 0, $x_offset, $y_offset, $size, $size, $square_size, $square_size);
  120. imagejpeg($thumb, $path, 95);
  121. imagedestroy($thumb);
  122. }
  123. }
  124. }