/system/library/image.php

https://github.com/arastta/arastta · PHP · 202 lines · 152 code · 43 blank · 7 comment · 35 complexity · e21ae0ca768c56736b6386b19ca7405e MD5 · raw file

  1. <?php
  2. /**
  3. * @package Arastta eCommerce
  4. * @copyright 2015-2018 Arastta Association. All rights reserved.
  5. * @copyright See CREDITS.txt for credits and other copyright notices.
  6. * @license GNU GPL version 3; see LICENSE.txt
  7. * @link https://arastta.org
  8. */
  9. class Image {
  10. private $file;
  11. private $image;
  12. private $info;
  13. public function __construct($file) {
  14. if (file_exists($file)) {
  15. $this->file = $file;
  16. $info = getimagesize($file);
  17. $this->info = array(
  18. 'width' => $info[0],
  19. 'height' => $info[1],
  20. 'bits' => isset($info['bits']) ? $info['bits'] : '',
  21. 'mime' => isset($info['mime']) ? $info['mime'] : ''
  22. );
  23. $this->image = $this->create($file);
  24. } else {
  25. exit('Error: Could not load image ' . $file . '!');
  26. }
  27. }
  28. private function create($image) {
  29. $mime = $this->info['mime'];
  30. if ($mime == 'image/gif') {
  31. return imagecreatefromgif ($image);
  32. } elseif ($mime == 'image/png') {
  33. return imagecreatefrompng($image);
  34. } elseif ($mime == 'image/jpeg') {
  35. return imagecreatefromjpeg($image);
  36. }
  37. }
  38. public function save($file, $quality = 90) {
  39. $info = pathinfo($file);
  40. $extension = strtolower($info['extension']);
  41. if (is_resource($this->image)) {
  42. if ($extension == 'jpeg' || $extension == 'jpg') {
  43. imagejpeg($this->image, $file, $quality);
  44. } elseif ($extension == 'png') {
  45. imagepng($this->image, $file);
  46. } elseif ($extension == 'gif') {
  47. imagegif ($this->image, $file);
  48. }
  49. imagedestroy($this->image);
  50. }
  51. }
  52. public function resize($width = 0, $height = 0, $default = '') {
  53. if (!$this->info['width'] || !$this->info['height']) {
  54. return;
  55. }
  56. $xpos = 0;
  57. $ypos = 0;
  58. $scale = 1;
  59. $scale_w = $width / $this->info['width'];
  60. $scale_h = $height / $this->info['height'];
  61. if ($default == 'w') {
  62. $scale = $scale_w;
  63. } elseif ($default == 'h') {
  64. $scale = $scale_h;
  65. } else {
  66. $scale = min($scale_w, $scale_h);
  67. }
  68. if ($scale == 1 && $scale_h == $scale_w && $this->info['mime'] != 'image/png') {
  69. return;
  70. }
  71. $new_width = (int)($this->info['width'] * $scale);
  72. $new_height = (int)($this->info['height'] * $scale);
  73. $xpos = (int)(($width - $new_width) / 2);
  74. $ypos = (int)(($height - $new_height) / 2);
  75. $image_old = $this->image;
  76. $this->image = imagecreatetruecolor($width, $height);
  77. if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') {
  78. imagealphablending($this->image, false);
  79. imagesavealpha($this->image, true);
  80. $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
  81. imagecolortransparent($this->image, $background);
  82. } else {
  83. $background = imagecolorallocate($this->image, 255, 255, 255);
  84. }
  85. imagefilledrectangle($this->image, 0, 0, $width, $height, $background);
  86. imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);
  87. imagedestroy($image_old);
  88. $this->info['width'] = $width;
  89. $this->info['height'] = $height;
  90. }
  91. public function watermark($file, $position = 'bottomright') {
  92. $watermark = $this->create($file);
  93. $watermark_width = imagesx($watermark);
  94. $watermark_height = imagesy($watermark);
  95. switch($position) {
  96. case 'topleft':
  97. $watermark_pos_x = 0;
  98. $watermark_pos_y = 0;
  99. break;
  100. case 'topright':
  101. $watermark_pos_x = $this->info['width'] - $watermark_width;
  102. $watermark_pos_y = 0;
  103. break;
  104. case 'bottomleft':
  105. $watermark_pos_x = 0;
  106. $watermark_pos_y = $this->info['height'] - $watermark_height;
  107. break;
  108. case 'bottomright':
  109. $watermark_pos_x = $this->info['width'] - $watermark_width;
  110. $watermark_pos_y = $this->info['height'] - $watermark_height;
  111. break;
  112. }
  113. imagecopy($this->image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, 120, 40);
  114. imagedestroy($watermark);
  115. }
  116. public function crop($top_x, $top_y, $bottom_x, $bottom_y) {
  117. $image_old = $this->image;
  118. $this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y);
  119. imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->info['width'], $this->info['height']);
  120. imagedestroy($image_old);
  121. $this->info['width'] = $bottom_x - $top_x;
  122. $this->info['height'] = $bottom_y - $top_y;
  123. }
  124. public function rotate($degree, $color = 'FFFFFF') {
  125. $rgb = $this->html2rgb($color);
  126. $this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
  127. $this->info['width'] = imagesx($this->image);
  128. $this->info['height'] = imagesy($this->image);
  129. }
  130. private function filter($filter) {
  131. imagefilter($this->image, $filter);
  132. }
  133. private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000') {
  134. $rgb = $this->html2rgb($color);
  135. imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
  136. }
  137. private function merge($file, $x = 0, $y = 0, $opacity = 100) {
  138. $merge = $this->create($file);
  139. $merge_width = imagesx($merge);
  140. $merge_height = imagesy($merge);
  141. imagecopymerge($this->image, $merge, $x, $y, 0, 0, $merge_width, $merge_height, $opacity);
  142. }
  143. private function html2rgb($color) {
  144. if ($color[0] == '#') {
  145. $color = substr($color, 1);
  146. }
  147. if (strlen($color) == 6) {
  148. list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]);
  149. } elseif (strlen($color) == 3) {
  150. list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]);
  151. } else {
  152. return false;
  153. }
  154. $r = hexdec($r);
  155. $g = hexdec($g);
  156. $b = hexdec($b);
  157. return array($r, $g, $b);
  158. }
  159. }