/src/AppBundle/Services/Image.php

https://gitlab.com/habracoder/serv · PHP · 208 lines · 165 code · 43 blank · 0 comment · 36 complexity · b2b0f9ab49cadb59a56f09d056108d26 MD5 · raw file

  1. <?php
  2. namespace AppBundle\Services;
  3. class Image
  4. {
  5. private $file;
  6. private $image;
  7. private $info;
  8. public function __construct($file)
  9. {
  10. if (file_exists($file)) {
  11. $this->file = $file;
  12. $info = getimagesize($file);
  13. $this->info = array(
  14. 'width' => $info[0],
  15. 'height' => $info[1],
  16. 'bits' => isset($info['bits']) ? $info['bits'] : '',
  17. 'mime' => isset($info['mime']) ? $info['mime'] : ''
  18. );
  19. $this->image = $this->create($file);
  20. } else {
  21. exit('Error: Could not load image ' . $file . '!');
  22. }
  23. }
  24. private function create($image)
  25. {
  26. $mime = $this->info['mime'];
  27. if ($mime == 'image/gif') {
  28. return imagecreatefromgif($image);
  29. } elseif ($mime == 'image/png') {
  30. return imagecreatefrompng($image);
  31. } elseif ($mime == 'image/jpeg') {
  32. return imagecreatefromjpeg($image);
  33. }
  34. }
  35. public function save($file, $quality = 90)
  36. {
  37. $info = pathinfo($file);
  38. $extension = strtolower($info['extension']);
  39. if (is_resource($this->image)) {
  40. if ($extension == 'jpeg' || $extension == 'jpg') {
  41. imagejpeg($this->image, $file, $quality);
  42. } elseif ($extension == 'png') {
  43. imagepng($this->image, $file);
  44. } elseif ($extension == 'gif') {
  45. imagegif($this->image, $file);
  46. }
  47. imagedestroy($this->image);
  48. }
  49. }
  50. public function resize($width = 0, $height = 0, $default = '')
  51. {
  52. if (!$this->info['width'] || !$this->info['height']) {
  53. return;
  54. }
  55. $xpos = 0;
  56. $ypos = 0;
  57. $scale = 1;
  58. $scale_w = $width / $this->info['width'];
  59. $scale_h = $height / $this->info['height'];
  60. if ($default == 'w') {
  61. $scale = $scale_w;
  62. } elseif ($default == 'h') {
  63. $scale = $scale_h;
  64. } else {
  65. $scale = min($scale_w, $scale_h);
  66. }
  67. if ($scale == 1 && $scale_h == $scale_w && $this->info['mime'] != 'image/png') {
  68. return;
  69. }
  70. $new_width = (int)($this->info['width'] * $scale);
  71. $new_height = (int)($this->info['height'] * $scale);
  72. $xpos = (int)(($width - $new_width) / 2);
  73. $ypos = (int)(($height - $new_height) / 2);
  74. $image_old = $this->image;
  75. $this->image = imagecreatetruecolor($width, $height);
  76. if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') {
  77. imagealphablending($this->image, false);
  78. imagesavealpha($this->image, true);
  79. $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
  80. imagecolortransparent($this->image, $background);
  81. } else {
  82. $background = imagecolorallocate($this->image, 235, 123, 123);
  83. }
  84. imagefilledrectangle($this->image, 0, 0, $width, $height, $background);
  85. imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);
  86. imagedestroy($image_old);
  87. $this->info['width'] = $width;
  88. $this->info['height'] = $height;
  89. }
  90. public function watermark($file, $position = 'bottomright')
  91. {
  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. {
  118. $image_old = $this->image;
  119. $this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y);
  120. imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->info['width'], $this->info['height']);
  121. imagedestroy($image_old);
  122. $this->info['width'] = $bottom_x - $top_x;
  123. $this->info['height'] = $bottom_y - $top_y;
  124. }
  125. public function rotate($degree, $color = 'FFFFFF')
  126. {
  127. $rgb = $this->html2rgb($color);
  128. $this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
  129. $this->info['width'] = imagesx($this->image);
  130. $this->info['height'] = imagesy($this->image);
  131. }
  132. private function filter($filter)
  133. {
  134. imagefilter($this->image, $filter);
  135. }
  136. private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000')
  137. {
  138. $rgb = $this->html2rgb($color);
  139. imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
  140. }
  141. private function merge($file, $x = 0, $y = 0, $opacity = 100)
  142. {
  143. $merge = $this->create($file);
  144. $merge_width = imagesx($merge);
  145. $merge_height = imagesy($merge);
  146. imagecopymerge($this->image, $merge, $x, $y, 0, 0, $merge_width, $merge_height, $opacity);
  147. }
  148. private function html2rgb($color)
  149. {
  150. if ($color[0] == '#') {
  151. $color = substr($color, 1);
  152. }
  153. if (strlen($color) == 6) {
  154. list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]);
  155. } elseif (strlen($color) == 3) {
  156. list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]);
  157. } else {
  158. return false;
  159. }
  160. $r = hexdec($r);
  161. $g = hexdec($g);
  162. $b = hexdec($b);
  163. return array($r, $g, $b);
  164. }
  165. }