/sys/image/Gd.php

https://github.com/szeber/yapep · PHP · 158 lines · 110 code · 15 blank · 33 comment · 22 complexity · 489a89d33eeb9e299c7c4beccc877cb6 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of YAPEP.
  4. *
  5. * @package YAPEP
  6. * @subpackage Image
  7. * @author Zsolt Szeberenyi <szeber@svinformatika.hu>
  8. * @copyright 2008 The YAPEP Project All rights reserved.
  9. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  10. * @version $Rev: 11068 $
  11. */
  12. /**
  13. * GD image handler class
  14. *
  15. * This class uses the WideImage library
  16. *
  17. * @package YAPEP
  18. * @subpackage Image
  19. * @author Zsolt Szeberenyi <szeber@svinformatika.hu>
  20. * @copyright 2008 The YAPEP Project All rights reserved.
  21. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  22. * @version $Rev: 11068 $
  23. */
  24. class sys_image_Gd implements sys_image_IImage {
  25. /**
  26. *
  27. * @var wiImage
  28. */
  29. protected $pic;
  30. /**
  31. *
  32. * @var string
  33. */
  34. protected $format = null;
  35. /**
  36. * @var sys_IApplicationConfiguration
  37. */
  38. protected $config;
  39. public function __construct(sys_IApplicationConfiguration $config = null) {
  40. if (is_null($config)) {
  41. $config = sys_ApplicationConfiguration::getInstance();
  42. }
  43. $this->config = $config;
  44. }
  45. public function __destruct() {
  46. $this->close();
  47. }
  48. public function readImage($image) {
  49. $tmp = getimagesize($image);
  50. if (!is_array($tmp)) {
  51. return;
  52. }
  53. switch($tmp['2']) {
  54. case IMAGETYPE_GIF:
  55. $format = 'GIF';
  56. break;
  57. case IMAGETYPE_JPEG:
  58. $format = 'JPEG';
  59. break;
  60. case IMAGETYPE_PNG:
  61. $format = 'PNG';
  62. break;
  63. default:
  64. return;
  65. }
  66. $this->setImageFormat($format);
  67. $this->pic = wiImage::load($image, $format);
  68. return (bool)$this->pic;
  69. }
  70. public function setImageFormat($format) {
  71. $this->format = $format;
  72. }
  73. public function getImageWidth() {
  74. return $this->pic->getWidth();
  75. }
  76. public function getImageHeight() {
  77. return $this->pic->getHeight();
  78. }
  79. public function scaleImage($cols, $rows, $fit = false) {
  80. if ($fit) {
  81. $fill = 'fill';
  82. } else {
  83. $fill = 'inside';
  84. }
  85. $this->doScaleImage($cols, $rows, $fill);
  86. }
  87. protected function doScaleImage($cols, $rows, $fill) {
  88. if ($cols == 0 && $rows != 0) {
  89. $width = $this->getImageWidth();
  90. $height = $this->getImageHeight();
  91. $ar = $rows/$height;
  92. $cols = $ar*$width;
  93. } else if ($rows == 0 && $cols != 0) {
  94. $width = $this->getImageWidth();
  95. $height = $this->getImageHeight();
  96. $ar = $cols/$width;
  97. $rows = $ar*$height;
  98. }
  99. $this->pic = $this->pic->resize($cols, $rows, $fill);
  100. }
  101. public function writeImage($image = null) {
  102. $format = $this->format;
  103. if (!$format) {
  104. $format = 'JPEG';
  105. }
  106. if (is_null($image)) {
  107. echo $this->pic->asString($format);
  108. } else {
  109. $this->pic->saveToFile($image, $format);
  110. }
  111. }
  112. public function cropThumbnailImage($width, $height) {
  113. $this->doScaleImage($width, $heigth, 'outside');
  114. $top = 0;
  115. $left = 0;
  116. if ($this->getImageHeight() > $height) {
  117. $top = floor(($this->getImageHeight() - $height)/2);
  118. } else if ($this->getImageWidth() > $width) {
  119. $left = floor(($this->getImageWidth() - $width)/2);
  120. }
  121. $this->pic = $this->pic->crop($left, $top, $width, $height);
  122. }
  123. public function getImageSize() {
  124. $format = $this->format;
  125. if (!$format) {
  126. $format = 'JPEG';
  127. }
  128. $tmpFile = $this->config->getPath('uploadTmpDir').md5('gdTmpImage'.microtime(true));
  129. $this->pic->saveToFile($tmpFile, $format);
  130. $size = null;
  131. if (file_exists($tmpFile)) {
  132. $size = filesize($tmpFile);
  133. unlink($tmpFile);
  134. }
  135. return $size;
  136. }
  137. public function close() {
  138. if (is_object($this->pic)) {
  139. $this->pic->destroy();
  140. }
  141. }
  142. }
  143. ?>