Image.class.php 5.6 KB

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