Skip to content

Commit

Permalink
增加翻转和旋转功能
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Jul 14, 2016
1 parent 9f4dc01 commit 4cc5c71
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class Image
const WATER_SOUTHWEST = 7; //常量,标识左下角水印
const WATER_SOUTH = 8; //常量,标识下居中水印
const WATER_SOUTHEAST = 9; //常量,标识右下角水印
/* 翻转相关常量定义 */
const FLIP_X = 1; //X轴翻转
const FLIP_Y = 2; //Y轴翻转


/**
* 图像资源对象
Expand Down Expand Up @@ -182,6 +186,63 @@ public function size()
return [$this->info['width'], $this->info['height']];
}

/**
* 旋转图像
* @param int $degrees 顺时针旋转的度数
* @return $this
*/
public function rotate($degrees = 90)
{
do {
$img = imagerotate($this->im, -$degrees, imagecolorallocatealpha($this->im, 0, 0, 0, 127));
imagedestroy($this->im);
$this->im = $img;
} while (!empty($this->gif) && $this->gifNext());

$this->info['width'] = imagesx($this->im);
$this->info['height'] = imagesy($this->im);

return $this;
}

/**
* 翻转图像
* @param integer $direction 翻转轴,X或者Y
* @return $this
*/
public function flip($direction = self::FLIP_X)
{
//原图宽度和高度
$w = $this->info['width'];
$h = $this->info['height'];

do {

$img = imagecreatetruecolor($w, $h);

switch ($direction) {
case self::FLIP_X:
for ($y = 0; $y < $h; $y++) {
imagecopy($img, $this->im, 0, $h - $y - 1, 0, $y, $w, 1);
}
break;
case self::FLIP_Y:
for ($x = 0; $x < $w; $x++) {
imagecopy($img, $this->im, $w - $x - 1, 0, $x, 0, 1, $h);
}
break;
default:
throw new ImageException('不支持的翻转类型');
}

imagedestroy($this->im);
$this->im = $img;

} while (!empty($this->gif) && $this->gifNext());

return $this;
}

/**
* 裁剪图像
*
Expand Down Expand Up @@ -532,9 +593,11 @@ protected function gifNext()
$this->gif->image($img);
$next = $this->gif->nextImage();
if ($next) {
imagedestroy($this->im);
$this->im = imagecreatefromstring($next);
return $next;
} else {
imagedestroy($this->im);
$this->im = imagecreatefromstring($this->gif->image());
return false;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/FlipTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace tests;

use think\Image;

class FlipTest extends TestCase
{
public function testJpeg()
{
$pathname = TEST_PATH . 'tmp/flip.jpg';
$image = Image::open($this->getJpeg());
$image->flip()->save($pathname);

$file = new \SplFileInfo($pathname);

$this->assertTrue($file->isFile());

@unlink($pathname);
}


public function testGif()
{
$pathname = TEST_PATH . 'tmp/flip.gif';
$image = Image::open($this->getGif());
$image->flip(Image::FLIP_Y)->save($pathname);

$file = new \SplFileInfo($pathname);

$this->assertTrue($file->isFile());

@unlink($pathname);
}
}
44 changes: 44 additions & 0 deletions tests/RotateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace tests;

use think\Image;

class RotateTest extends TestCase
{
public function testJpeg()
{
$pathname = TEST_PATH . 'tmp/rotate.jpg';
$image = Image::open($this->getJpeg());
$image->rotate(90)->save($pathname);

$this->assertEquals(800, $image->height());

$file = new \SplFileInfo($pathname);

$this->assertTrue($file->isFile());

@unlink($pathname);
}

public function testGif()
{
$pathname = TEST_PATH . 'tmp/rotate.gif';
$image = Image::open($this->getGif());
$image->rotate(90)->save($pathname);

$file = new \SplFileInfo($pathname);

$this->assertTrue($file->isFile());

@unlink($pathname);
}
}

0 comments on commit 4cc5c71

Please sign in to comment.