PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/anchor/libraries/image.php

https://gitlab.com/valced/anchor-cms
PHP | 258 lines | 177 code | 60 blank | 21 comment | 23 complexity | e6d5288206d605bbad46b163a9e3225c MD5 | raw file
  1. <?php
  2. class Image {
  3. private $src_image, $src_w, $src_h;
  4. private $dst_image;
  5. public function __construct($src_image, $src_w, $src_h) {
  6. $this->src_image = $src_image;
  7. $this->src_w = $src_w;
  8. $this->src_h = $src_h;
  9. }
  10. public function width() {
  11. return $this->src_w;
  12. }
  13. public function height() {
  14. return $this->src_h;
  15. }
  16. public static function open($file) {
  17. if(file_exists($file) === false) {
  18. return false;
  19. }
  20. list($width, $height, $type) = getimagesize($file);
  21. if($type === IMAGETYPE_PNG) {
  22. return new static(imagecreatefrompng($file), $width, $height);
  23. }
  24. if($type === IMAGETYPE_JPEG) {
  25. return new static(imagecreatefromjpeg($file), $width, $height);
  26. }
  27. if($type === IMAGETYPE_GIF) {
  28. return new static(imagecreatefromgif($file), $width, $height);
  29. }
  30. return false;
  31. }
  32. public function check_available_memory($file, $dst_w, $dst_h, $bloat = 1.68) {
  33. // Get maxmemory limit in Mb convert to bytes
  34. $max = ((int) ini_get('memory_limit') * 1024) * 1024;
  35. // get image size
  36. list($src_w, $src_h) = getimagesize($file);
  37. // Source GD bytes
  38. $src_bytes = ceil((($src_w * $src_h) * 3) * $bloat);
  39. // Target GD bytes
  40. $dst_bytes = ceil((($dst_w * $dst_h) * 3) * $bloat);
  41. $total = $src_bytes + $dst_bytes + memory_get_usage();
  42. if($total > $max) {
  43. return false;
  44. }
  45. return $src_bytes + $dst_bytes;
  46. }
  47. public function sharpen() {
  48. // resource
  49. $res = $this->src_image;
  50. if(is_resource($this->dst_image)) {
  51. $res = $this->dst_image;
  52. }
  53. // define matrix
  54. $sharpen = array(
  55. array(0.0, -1.0, 0.0),
  56. array(-1.0, 5.0, -1.0),
  57. array(0.0, -1.0, 0.0)
  58. );
  59. // calculate the divisor
  60. $divisor = array_sum(array_map('array_sum', $sharpen));
  61. // apply the matrix
  62. imageconvolution($res, $sharpen, $divisor, 0);
  63. return $this;
  64. }
  65. public function blur() {
  66. // resource
  67. $res = $this->src_image;
  68. if(is_resource($this->dst_image)) {
  69. $res = $this->dst_image;
  70. }
  71. // define matrix
  72. $gaussian = array(
  73. array(1.0, 2.0, 1.0),
  74. array(2.0, 4.0, 2.0),
  75. array(1.0, 2.0, 1.0)
  76. );
  77. // calculate the divisor
  78. $divisor = array_sum(array_map('array_sum', $gaussian));
  79. // apply the matrix
  80. imageconvolution($res, $gaussian, $divisor, 0);
  81. return $this;
  82. }
  83. public function grayscale() {
  84. // resource
  85. $res = $this->src_image;
  86. if(is_resource($this->dst_image)) {
  87. $res = $this->dst_image;
  88. }
  89. imagefilter($res, IMG_FILTER_GRAYSCALE);
  90. return $this;
  91. }
  92. public function resize($dst_w, $dst_h) {
  93. $ratio = 0;
  94. // landscape
  95. if($this->src_w > $this->src_h) {
  96. $ratio = $dst_w / $this->src_w;
  97. $dst_h = $this->src_h * $ratio;
  98. }
  99. // portrait
  100. if($this->src_w < $this->src_h) {
  101. $ratio = $dst_h / $this->src_h;
  102. $dst_w = $this->src_w * $ratio;
  103. }
  104. // square : use smaller value as the match
  105. if($this->src_w == $this->src_h) {
  106. if($dst_w > $dst_h) {
  107. $dst_w = $dst_h;
  108. }else{
  109. $dst_h = $dst_w;
  110. }
  111. }
  112. $this->dst_image = imagecreatetruecolor($dst_w, $dst_h);
  113. $params = array(
  114. 'dst_image' => $this->dst_image,
  115. 'src_image' => $this->src_image,
  116. 'dst_x' => 0,
  117. 'dst_y' => 0,
  118. 'src_x' => 0,
  119. 'src_y' => 0,
  120. 'dst_w' => $dst_w,
  121. 'dst_h' => $dst_h,
  122. 'src_w' => $this->src_w,
  123. 'src_h' => $this->src_h
  124. );
  125. call_user_func_array('imagecopyresampled', array_values($params));
  126. return $this;
  127. }
  128. public function crop($dst_w, $dst_h, $src_x, $src_y) {
  129. // if the source image is square use smaller dest size
  130. // @link http://srccd.com/posts/the-move-to-anchor-cms
  131. if($this->src_w == $this->src_h) {
  132. if($dst_w > $dst_h) {
  133. $dst_w = $dst_h;
  134. }
  135. else {
  136. $dst_h = $dst_w;
  137. }
  138. }
  139. $this->dst_image = imagecreatetruecolor($dst_w, $dst_h);
  140. $params = array(
  141. 'dst_image' => $this->dst_image,
  142. 'src_image' => $this->src_image,
  143. 'dst_x' => 0,
  144. 'dst_y' => 0,
  145. 'src_x' => $src_x,
  146. 'src_y' => $src_y,
  147. 'dst_w' => $dst_w,
  148. 'dst_h' => $dst_h,
  149. 'src_w' => $dst_w,
  150. 'src_h' => $dst_h
  151. );
  152. call_user_func_array('imagecopyresampled', array_values($params));
  153. return $this;
  154. }
  155. public function palette($percentages = array(0.2, 0.7, 0.5)) {
  156. // Now set dimensions on where to pull color values from (based on percentages).
  157. $dimensions[] = ($this->src_w - ($this->src_w * $percentages[0]));
  158. $dimensions[] = ($this->src_h - ($this->src_h * $percentages[0]));
  159. $dimensions[] = ($this->src_w - ($this->src_w * $percentages[0]));
  160. $dimensions[] = ($this->src_h - ($this->src_h * $percentages[1]));
  161. $dimensions[] = ($this->src_w - ($this->src_w * $percentages[1]));
  162. $dimensions[] = ($this->src_h - ($this->src_h * $percentages[1]));
  163. $dimensions[] = ($this->src_w - ($this->src_w * $percentages[1]));
  164. $dimensions[] = ($this->src_h - ($this->src_h * $percentages[0]));
  165. $dimensions[] = ($this->src_w - ($this->src_w * $percentages[2]));
  166. $dimensions[] = ($this->src_h - ($this->src_h * $percentages[2]));
  167. // Here we'll pull the color values of certain pixels around the image based on our dimensions set above.
  168. for($k = 0; $k < 10; $k++) {
  169. $newk = $k + 1;
  170. $rgb[] = imagecolorat($this->src_image, $dimensions[$k], $dimensions[$newk]);
  171. $k++;
  172. }
  173. // Almost done! Now we need to get the individual r,g,b values for our colors.
  174. foreach($rgb as $colorvalue) {
  175. $r[] = dechex(($colorvalue >> 16) & 0xFF);
  176. $g[] = dechex(($colorvalue >> 8) & 0xFF);
  177. $b[] = dechex($colorvalue & 0xFF);
  178. }
  179. return array(
  180. strtoupper($r[0] . $g[0] . $b[0]),
  181. strtoupper($r[1] . $g[1] . $b[1]),
  182. strtoupper($r[2] . $g[2] . $b[2]),
  183. strtoupper($r[3] . $g[3] . $b[3]),
  184. strtoupper($r[4] . $g[4] . $b[4]));
  185. }
  186. public function output($type = 'png', $file = null) {
  187. if($type == 'png') {
  188. imagepng($this->dst_image, $file, 9);
  189. }
  190. elseif($type == 'jpeg' or $type == 'jpg') {
  191. imagejpeg($this->dst_image, $file, 75);
  192. }
  193. elseif($type == 'gif') {
  194. imagegif($this->dst_image, $file);
  195. }
  196. imagedestroy($this->dst_image);
  197. }
  198. }