123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- /**
- * Created by PhpStorm.
- * User: lionared
- * Date: 2018/6/6
- * Time: 上午11:12
- */
- namespace tools;
- class image_scaler
- {
- private $mOutImage;
- private $mInImage;
- private $mCx;
- private $mCy;
- public function __construct($width,$height)
- {
- $this->mOutImage = imagecreatetruecolor($width,$height);
- $this->mCx = $width;
- $this->mCy = $height;
- }
- public function __destruct()
- {
- @imagedestroy($this->mOutImage);
- @imagedestroy($this->mInImage);
- }
- public function scale($filename,$scale)
- {
- $ret = $this->read($filename);
- if($ret == false) return false;
- $cx = imagesx($this->mInImage);
- $cy = imagesy($this->mInImage);
- $size = $this->calc($cx,$cy,$scale);
- $dx = $size['cx'];
- $dy = $size['cy'];
- $x = intval(($cx - $dx) / 2 + 0.5);
- $y = intval(($cy - $dy) / 2 + 0.5);
- imagecopyresized($this->mOutImage, $this->mInImage,0,0, $x, $y, $this->mCx,$this->mCy,$dx,$dy);
- return true;
- }
- public function getImage() {
- return $this->mOutImage;
- }
- private function calc($cx,$cy,$scale)
- {
- $dx = intval($cy * $scale + 0.5);
- $cx = intval($cx);
- if($cx > $dx) {
- return ['cx' => $dx,'cy' => $cy];
- }
- elseif($cx == $dx) {
- return ['cx' => $cx,'cy' => $cy];
- }
- else {
- return ['cx' => $cx,'cy' => intval($cx / $scale + 0.5)];
- }
- }
- private function read($file)
- {
- $imagetype = exif_imagetype($file);
- if (! $imagetype) {
- return false;
- }
- switch ($imagetype)
- {
- case IMAGETYPE_GIF:
- $this->mInImage = @imagecreatefromgif($file);
- break;
- case IMAGETYPE_JPEG:
- case IMAGETYPE_JPEG2000:
- $this->mInImage = @imagecreatefromjpeg($file);
- break;
- case IMAGETYPE_PNG:
- $this->mInImage = @imagecreatefrompng($file);
- break;
- case IMAGETYPE_BMP:
- $this->mInImage = $this->imagecreatefrombmp($file);
- break;
- default:
- return false;
- }
- return true;
- }
- private function imagecreatefrombmp($filename)
- {
- $file = fopen( $filename, "rb" );
- $read = fread( $file, 10 );
- while( !feof( $file ) && $read != "" )
- {
- $read .= fread( $file, 1024 );
- }
- $temp = unpack( "H*", $read );
- $hex = $temp[1];
- $header = substr( $hex, 0, 104 );
- $body = str_split( substr( $hex, 108 ), 6 );
- if( substr( $header, 0, 4 ) == "424d" )
- {
- $header = substr( $header, 4 );
- // Remove some stuff?
- $header = substr( $header, 32 );
- // Get the width
- $width = hexdec( substr( $header, 0, 2 ) );
- // Remove some stuff?
- $header = substr( $header, 8 );
- // Get the height
- $height = hexdec( substr( $header, 0, 2 ) );
- unset( $header );
- }
- $x = 0;
- $y = 1;
- $image = imagecreatetruecolor( $width, $height );
- foreach( $body as $rgb )
- {
- $r = hexdec( substr( $rgb, 4, 2 ) );
- $g = hexdec( substr( $rgb, 2, 2 ) );
- $b = hexdec( substr( $rgb, 0, 2 ) );
- $color = imagecolorallocate( $image, $r, $g, $b );
- imagesetpixel( $image, $x, $height-$y, $color );
- $x++;
- if( $x >= $width )
- {
- $x = 0;
- $y++;
- }
- }
- return $image;
- }
- }
|