image_scaler.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: lionared
  5. * Date: 2018/6/6
  6. * Time: 上午11:12
  7. */
  8. namespace tools;
  9. class image_scaler
  10. {
  11. private $mOutImage;
  12. private $mInImage;
  13. private $mCx;
  14. private $mCy;
  15. public function __construct($width,$height)
  16. {
  17. $this->mOutImage = imagecreatetruecolor($width,$height);
  18. $this->mCx = $width;
  19. $this->mCy = $height;
  20. }
  21. public function __destruct()
  22. {
  23. @imagedestroy($this->mOutImage);
  24. @imagedestroy($this->mInImage);
  25. }
  26. public function scale($filename,$scale)
  27. {
  28. $ret = $this->read($filename);
  29. if($ret == false) return false;
  30. $cx = imagesx($this->mInImage);
  31. $cy = imagesy($this->mInImage);
  32. $size = $this->calc($cx,$cy,$scale);
  33. $dx = $size['cx'];
  34. $dy = $size['cy'];
  35. $x = intval(($cx - $dx) / 2 + 0.5);
  36. $y = intval(($cy - $dy) / 2 + 0.5);
  37. imagecopyresized($this->mOutImage, $this->mInImage,0,0, $x, $y, $this->mCx,$this->mCy,$dx,$dy);
  38. return true;
  39. }
  40. public function getImage() {
  41. return $this->mOutImage;
  42. }
  43. private function calc($cx,$cy,$scale)
  44. {
  45. $dx = intval($cy * $scale + 0.5);
  46. $cx = intval($cx);
  47. if($cx > $dx) {
  48. return ['cx' => $dx,'cy' => $cy];
  49. }
  50. elseif($cx == $dx) {
  51. return ['cx' => $cx,'cy' => $cy];
  52. }
  53. else {
  54. return ['cx' => $cx,'cy' => intval($cx / $scale + 0.5)];
  55. }
  56. }
  57. private function read($file)
  58. {
  59. $imagetype = exif_imagetype($file);
  60. if (! $imagetype) {
  61. return false;
  62. }
  63. switch ($imagetype)
  64. {
  65. case IMAGETYPE_GIF:
  66. $this->mInImage = @imagecreatefromgif($file);
  67. break;
  68. case IMAGETYPE_JPEG:
  69. case IMAGETYPE_JPEG2000:
  70. $this->mInImage = @imagecreatefromjpeg($file);
  71. break;
  72. case IMAGETYPE_PNG:
  73. $this->mInImage = @imagecreatefrompng($file);
  74. break;
  75. case IMAGETYPE_BMP:
  76. $this->mInImage = $this->imagecreatefrombmp($file);
  77. break;
  78. default:
  79. return false;
  80. }
  81. return true;
  82. }
  83. private function imagecreatefrombmp($filename)
  84. {
  85. $file = fopen( $filename, "rb" );
  86. $read = fread( $file, 10 );
  87. while( !feof( $file ) && $read != "" )
  88. {
  89. $read .= fread( $file, 1024 );
  90. }
  91. $temp = unpack( "H*", $read );
  92. $hex = $temp[1];
  93. $header = substr( $hex, 0, 104 );
  94. $body = str_split( substr( $hex, 108 ), 6 );
  95. if( substr( $header, 0, 4 ) == "424d" )
  96. {
  97. $header = substr( $header, 4 );
  98. // Remove some stuff?
  99. $header = substr( $header, 32 );
  100. // Get the width
  101. $width = hexdec( substr( $header, 0, 2 ) );
  102. // Remove some stuff?
  103. $header = substr( $header, 8 );
  104. // Get the height
  105. $height = hexdec( substr( $header, 0, 2 ) );
  106. unset( $header );
  107. }
  108. $x = 0;
  109. $y = 1;
  110. $image = imagecreatetruecolor( $width, $height );
  111. foreach( $body as $rgb )
  112. {
  113. $r = hexdec( substr( $rgb, 4, 2 ) );
  114. $g = hexdec( substr( $rgb, 2, 2 ) );
  115. $b = hexdec( substr( $rgb, 0, 2 ) );
  116. $color = imagecolorallocate( $image, $r, $g, $b );
  117. imagesetpixel( $image, $x, $height-$y, $color );
  118. $x++;
  119. if( $x >= $width )
  120. {
  121. $x = 0;
  122. $y++;
  123. }
  124. }
  125. return $image;
  126. }
  127. }