/vendor/intervention/image/tests/ImageTest.php

https://bitbucket.org/helfreire/tccsite · PHP · 1186 lines · 990 code · 117 blank · 79 comment · 0 complexity · ef04b76e4003d5f4bceffd4687e22d35 MD5 · raw file

  1. <?php
  2. use Intervention\Image\Image;
  3. class ImageTest extends PHPUnit_Framework_Testcase
  4. {
  5. protected function setUp()
  6. {
  7. }
  8. protected function tearDown()
  9. {
  10. }
  11. private function getTestImage()
  12. {
  13. return new Image('public/test.jpg');
  14. }
  15. public function testConstructorPlain()
  16. {
  17. $img = new Image;
  18. $this->assertInstanceOf('Intervention\Image\Image', $img);
  19. $this->assertInternalType('resource', $img->resource);
  20. $this->assertInternalType('int', $img->width);
  21. $this->assertInternalType('int', $img->height);
  22. $this->assertEquals($img->width, 1);
  23. $this->assertEquals($img->height, 1);
  24. $color = $img->pickColor(0, 0, 'array');
  25. $this->assertInternalType('int', $color['r']);
  26. $this->assertInternalType('int', $color['g']);
  27. $this->assertInternalType('int', $color['b']);
  28. $this->assertInternalType('float', $color['a']);
  29. $this->assertEquals($color['r'], 0);
  30. $this->assertEquals($color['g'], 0);
  31. $this->assertEquals($color['b'], 0);
  32. $this->assertEquals($color['a'], 0);
  33. }
  34. public function testConstructorWithPath()
  35. {
  36. $img = new Image('public/test.jpg');
  37. $this->assertInstanceOf('Intervention\Image\Image', $img);
  38. $this->assertInternalType('resource', $img->resource);
  39. $this->assertInternalType('int', $img->width);
  40. $this->assertInternalType('int', $img->height);
  41. $this->assertEquals($img->width, 800);
  42. $this->assertEquals($img->height, 600);
  43. $this->assertEquals($img->dirname, 'public');
  44. $this->assertEquals($img->basename, 'test.jpg');
  45. $this->assertEquals($img->extension, 'jpg');
  46. $this->assertEquals($img->filename, 'test');
  47. $this->assertEquals($img->mime, 'image/jpeg');
  48. }
  49. /**
  50. * @expectedException Intervention\Image\Exception\ImageNotFoundException
  51. */
  52. public function testConstructorWithInvalidPath()
  53. {
  54. $img = new Image('public/foo/bar/invalid_image_path.jpg');
  55. }
  56. /**
  57. * @expectedException Intervention\Image\Exception\InvalidImageTypeException
  58. */
  59. public function testContructorWithPathInvalidType()
  60. {
  61. $img = new Image('public/text.txt');
  62. }
  63. public function testConstructoWithString()
  64. {
  65. $data = file_get_contents('public/test.jpg');
  66. $img = new Image($data);
  67. $this->assertInstanceOf('Intervention\Image\Image', $img);
  68. $this->assertInternalType('resource', $img->resource);
  69. $this->assertInternalType('int', $img->width);
  70. $this->assertInternalType('int', $img->height);
  71. $this->assertEquals($img->width, 800);
  72. $this->assertEquals($img->height, 600);
  73. }
  74. /**
  75. * @expectedException Intervention\Image\Exception\InvalidImageDataStringException
  76. */
  77. public function testConstructionWithInvalidString()
  78. {
  79. // the semi-random string is base64_decoded to allow it to
  80. // pass the isBinary conditional.
  81. $data = base64_decode('6KKjdeyUAhRPNzxeYybZ');
  82. $img = new Image($data);
  83. }
  84. public function testConstructorWithResource()
  85. {
  86. $resource = imagecreatefromjpeg('public/test.jpg');
  87. $img = new Image($resource);
  88. $this->assertInstanceOf('Intervention\Image\Image', $img);
  89. $this->assertInternalType('resource', $img->resource);
  90. $this->assertInternalType('int', $img->width);
  91. $this->assertInternalType('int', $img->height);
  92. $this->assertEquals($img->width, 800);
  93. $this->assertEquals($img->height, 600);
  94. }
  95. /**
  96. * @expectedException Intervention\Image\Exception\InvalidImageResourceException
  97. */
  98. public function testConstructorWithInvalidResource()
  99. {
  100. $resource = fopen('public/test.jpg', 'r+');
  101. $img = new Image($resource);
  102. }
  103. public function testConstructorCanvas()
  104. {
  105. $img = new Image(null, 800, 600);
  106. $this->assertInstanceOf('Intervention\Image\Image', $img);
  107. $this->assertInternalType('resource', $img->resource);
  108. $this->assertInternalType('int', $img->width);
  109. $this->assertInternalType('int', $img->height);
  110. $this->assertEquals($img->width, 800);
  111. $this->assertEquals($img->height, 600);
  112. $color = $img->pickColor(50, 50, 'array');
  113. $this->assertInternalType('int', $color['r']);
  114. $this->assertInternalType('int', $color['g']);
  115. $this->assertInternalType('int', $color['b']);
  116. $this->assertInternalType('float', $color['a']);
  117. $this->assertEquals($color['r'], 0);
  118. $this->assertEquals($color['g'], 0);
  119. $this->assertEquals($color['b'], 0);
  120. $this->assertEquals($color['a'], 0);
  121. }
  122. public function testStaticCallMakeFromPath()
  123. {
  124. $img = Image::make('public/test.jpg');
  125. $this->assertInternalType('resource', $img->resource);
  126. $this->assertInternalType('int', $img->width);
  127. $this->assertInternalType('int', $img->height);
  128. $this->assertEquals($img->width, 800);
  129. $this->assertEquals($img->height, 600);
  130. $this->assertEquals($img->dirname, 'public');
  131. $this->assertEquals($img->basename, 'test.jpg');
  132. $this->assertEquals($img->extension, 'jpg');
  133. $this->assertEquals($img->filename, 'test');
  134. $this->assertEquals($img->mime, 'image/jpeg');
  135. }
  136. public function testStaticCallMakeFromString()
  137. {
  138. $data = file_get_contents('public/test.jpg');
  139. $img = Image::make($data);
  140. $this->assertInternalType('resource', $img->resource);
  141. $this->assertInternalType('int', $img->width);
  142. $this->assertInternalType('int', $img->height);
  143. $this->assertEquals($img->width, 800);
  144. $this->assertEquals($img->height, 600);
  145. }
  146. public function testStaticCallMakeFromResource()
  147. {
  148. $resource = imagecreatefromjpeg('public/test.jpg');
  149. $img = Image::make($resource);
  150. $this->assertInternalType('resource', $img->resource);
  151. $this->assertInternalType('int', $img->width);
  152. $this->assertInternalType('int', $img->height);
  153. $this->assertEquals($img->width, 800);
  154. $this->assertEquals($img->height, 600);
  155. }
  156. public function testStaticCallCanvas()
  157. {
  158. $img = Image::canvas(300, 200);
  159. $this->assertInternalType('resource', $img->resource);
  160. $this->assertInternalType('int', $img->width);
  161. $this->assertInternalType('int', $img->height);
  162. $this->assertEquals($img->width, 300);
  163. $this->assertEquals($img->height, 200);
  164. $img = Image::canvas(32, 32, 'b53717');
  165. $this->assertInternalType('resource', $img->resource);
  166. $this->assertInternalType('int', $img->width);
  167. $this->assertInternalType('int', $img->height);
  168. $this->assertEquals($img->width, 32);
  169. $this->assertEquals($img->height, 32);
  170. $this->assertEquals($img->pickColor(15, 15, 'hex'), '#b53717');
  171. }
  172. public function testStaticCallRaw()
  173. {
  174. $data = file_get_contents('public/test.jpg');
  175. $img = Image::raw($data);
  176. $this->assertInternalType('resource', $img->resource);
  177. $this->assertInternalType('int', $img->width);
  178. $this->assertInternalType('int', $img->height);
  179. $this->assertEquals($img->width, 800);
  180. $this->assertEquals($img->height, 600);
  181. }
  182. public function testCreateCanvasWithTransparentBackground()
  183. {
  184. $img = Image::canvas(100, 100);
  185. $color = $img->pickColor(50, 50, 'array');
  186. $this->assertInternalType('int', $color['r']);
  187. $this->assertInternalType('int', $color['g']);
  188. $this->assertInternalType('int', $color['b']);
  189. $this->assertInternalType('float', $color['a']);
  190. $this->assertEquals($color['r'], 0);
  191. $this->assertEquals($color['g'], 0);
  192. $this->assertEquals($color['b'], 0);
  193. $this->assertEquals($color['a'], 0);
  194. }
  195. public function testOpenImage()
  196. {
  197. $img = new Image;
  198. $img->open('public/test.jpg');
  199. $this->assertInternalType('resource', $img->resource);
  200. $this->assertInternalType('int', $img->width);
  201. $this->assertInternalType('int', $img->height);
  202. $this->assertEquals($img->width, 800);
  203. $this->assertEquals($img->height, 600);
  204. $this->assertEquals($img->dirname, 'public');
  205. $this->assertEquals($img->basename, 'test.jpg');
  206. $this->assertEquals($img->extension, 'jpg');
  207. $this->assertEquals($img->filename, 'test');
  208. $this->assertEquals($img->mime, 'image/jpeg');
  209. }
  210. public function testResizeImage()
  211. {
  212. $img = $this->getTestImage();
  213. $img->resize(320, 240);
  214. $this->assertInternalType('int', $img->width);
  215. $this->assertInternalType('int', $img->height);
  216. $this->assertEquals($img->width, 320);
  217. $this->assertEquals($img->height, 240);
  218. $this->assertEquals($img->pickColor(50, 50, 'hex'), '#fffaf4');
  219. $this->assertEquals($img->pickColor(260, 190, 'hex'), '#ffa600');
  220. // Only resize the width.
  221. $img = $this->getTestImage();
  222. $height = $img->height;
  223. $img->resize(320);
  224. $this->assertInternalType('int', $img->width);
  225. $this->assertInternalType('int', $img->height);
  226. $this->assertEquals($img->width, 320);
  227. // Check if the height is still the same.
  228. $this->assertEquals($img->height, $height);
  229. $this->assertEquals($img->pickColor(75, 65, 'hex'), '#fffcf3');
  230. $this->assertEquals($img->pickColor(250, 150, 'hex'), '#ffc150');
  231. // Only resize the width.
  232. $img = $this->getTestImage();
  233. $width = $img->width;
  234. $img->resize(null, 240);
  235. $this->assertInternalType('int', $img->width);
  236. $this->assertInternalType('int', $img->height);
  237. // Check if the width is still the same.
  238. $this->assertEquals($img->width, $width);
  239. $this->assertEquals($img->height, 240);
  240. $this->assertEquals($img->pickColor(150, 75, 'hex'), '#fff4e0');
  241. $this->assertEquals($img->pickColor(540, 10, 'hex'), '#ffda96');
  242. // auto height
  243. $img = $this->getTestImage();
  244. $img->resize(320, null, true);
  245. $this->assertInternalType('int', $img->width);
  246. $this->assertInternalType('int', $img->height);
  247. $this->assertEquals($img->width, 320);
  248. $this->assertEquals($img->height, 240);
  249. $this->assertEquals($img->pickColor(50, 50, 'hex'), '#fffaf4');
  250. $this->assertEquals($img->pickColor(260, 190, 'hex'), '#ffa600');
  251. // auto width
  252. $img = $this->getTestImage();
  253. $img->resize(null, 240, true);
  254. $this->assertInternalType('int', $img->width);
  255. $this->assertInternalType('int', $img->height);
  256. $this->assertEquals($img->width, 320);
  257. $this->assertEquals($img->height, 240);
  258. $this->assertEquals($img->pickColor(50, 50, 'hex'), '#fffaf4');
  259. $this->assertEquals($img->pickColor(260, 190, 'hex'), '#ffa600');
  260. // preserve simple upsizing
  261. $img = $this->getTestImage();
  262. $img->resize(1000, 1000, true, false);
  263. $this->assertInternalType('int', $img->width);
  264. $this->assertInternalType('int', $img->height);
  265. $this->assertEquals($img->width, 800);
  266. $this->assertEquals($img->height, 600);
  267. // test dominant width for auto-resizing
  268. $img = $this->getTestImage();
  269. $img->resize(1000, 1200, true);
  270. $this->assertInternalType('int', $img->width);
  271. $this->assertInternalType('int', $img->height);
  272. $this->assertEquals($img->width, 1000);
  273. $this->assertEquals($img->height, 750);
  274. // Test image upsizing.
  275. $img = $this->getTestImage();
  276. // Keep original width and height.
  277. $original_width = $img->width;
  278. $original_height = $img->height;
  279. // Increase values a bit.
  280. $width = $original_width + 500;
  281. $height = $original_height + 350;
  282. // Try resizing to higher values while upsizing is set to false.
  283. $img->resize($width, $height, false, false);
  284. $this->assertInternalType('int', $img->width);
  285. $this->assertInternalType('int', $img->height);
  286. // Check if width and height are still the same.
  287. $this->assertEquals($img->width, $original_width);
  288. $this->assertEquals($img->height, $original_height);
  289. }
  290. /**
  291. * @expectedException Intervention\Image\Exception\DimensionOutOfBoundsException
  292. */
  293. public function testResizeImageWithoutDimensions()
  294. {
  295. $img = $this->getTestImage();
  296. $img->resize();
  297. }
  298. public function testWidenImage()
  299. {
  300. $img = $this->getTestImage();
  301. $img->widen(100);
  302. $this->assertInternalType('int', $img->width);
  303. $this->assertInternalType('int', $img->height);
  304. $this->assertEquals($img->width, 100);
  305. $this->assertEquals($img->height, 75);
  306. $img->widen(1000);
  307. $this->assertInternalType('int', $img->width);
  308. $this->assertInternalType('int', $img->height);
  309. $this->assertEquals($img->width, 1000);
  310. $this->assertEquals($img->height, 750);
  311. }
  312. public function testHeightenImage()
  313. {
  314. $img = $this->getTestImage();
  315. $img->heighten(150);
  316. $this->assertInternalType('int', $img->width);
  317. $this->assertInternalType('int', $img->height);
  318. $this->assertEquals($img->width, 200);
  319. $this->assertEquals($img->height, 150);
  320. $img->heighten(900);
  321. $this->assertInternalType('int', $img->width);
  322. $this->assertInternalType('int', $img->height);
  323. $this->assertEquals($img->width, 1200);
  324. $this->assertEquals($img->height, 900);
  325. }
  326. public function testResizeCanvas()
  327. {
  328. $img = $this->getTestImage();
  329. $img->resizeCanvas(300, 200); // pin center
  330. $this->assertInstanceOf('Intervention\Image\Image', $img);
  331. $this->assertInternalType('int', $img->width);
  332. $this->assertInternalType('int', $img->height);
  333. $this->assertEquals($img->width, 300);
  334. $this->assertEquals($img->height, 200);
  335. $this->assertEquals('#ffe8bc', $img->pickColor(0, 0, 'hex'));
  336. $this->assertEquals('#ffaf1c', $img->pickColor(299, 199, 'hex'));
  337. $img = $this->getTestImage();
  338. $img->resizeCanvas(300, 200, 'top-left');
  339. $this->assertInstanceOf('Intervention\Image\Image', $img);
  340. $this->assertInternalType('int', $img->width);
  341. $this->assertInternalType('int', $img->height);
  342. $this->assertEquals($img->width, 300);
  343. $this->assertEquals($img->height, 200);
  344. $this->assertEquals('#ffffff', $img->pickColor(0, 0, 'hex'));
  345. $this->assertEquals('#fee3ae', $img->pickColor(299, 199, 'hex'));
  346. $img = $this->getTestImage();
  347. $img->resizeCanvas(300, 200, 'top');
  348. $this->assertInstanceOf('Intervention\Image\Image', $img);
  349. $this->assertInternalType('int', $img->width);
  350. $this->assertInternalType('int', $img->height);
  351. $this->assertEquals($img->width, 300);
  352. $this->assertEquals($img->height, 200);
  353. $this->assertEquals('#fffbf2', $img->pickColor(0, 0, 'hex'));
  354. $this->assertEquals('#ffc559', $img->pickColor(299, 199, 'hex'));
  355. $img = $this->getTestImage();
  356. $img->resizeCanvas(300, 200, 'top-right');
  357. $this->assertInstanceOf('Intervention\Image\Image', $img);
  358. $this->assertInternalType('int', $img->width);
  359. $this->assertInternalType('int', $img->height);
  360. $this->assertEquals($img->width, 300);
  361. $this->assertEquals($img->height, 200);
  362. $this->assertEquals('#ffe2ae', $img->pickColor(0, 0, 'hex'));
  363. $this->assertEquals('#ffac12', $img->pickColor(299, 199, 'hex'));
  364. $img = $this->getTestImage();
  365. $img->resizeCanvas(300, 200, 'left');
  366. $this->assertInstanceOf('Intervention\Image\Image', $img);
  367. $this->assertInternalType('int', $img->width);
  368. $this->assertInternalType('int', $img->height);
  369. $this->assertEquals($img->width, 300);
  370. $this->assertEquals($img->height, 200);
  371. $this->assertEquals('#fefdf9', $img->pickColor(0, 0, 'hex'));
  372. $this->assertEquals('#ffca6a', $img->pickColor(299, 199, 'hex'));
  373. $img = $this->getTestImage();
  374. $img->resizeCanvas(300, 200, 'right');
  375. $this->assertInstanceOf('Intervention\Image\Image', $img);
  376. $this->assertInternalType('int', $img->width);
  377. $this->assertInternalType('int', $img->height);
  378. $this->assertEquals($img->width, 300);
  379. $this->assertEquals($img->height, 200);
  380. $this->assertEquals('#ffca66', $img->pickColor(0, 0, 'hex'));
  381. $this->assertEquals('#ffa600', $img->pickColor(299, 199, 'hex'));
  382. $img = $this->getTestImage();
  383. $img->resizeCanvas(300, 200, 'bottom-left');
  384. $this->assertInstanceOf('Intervention\Image\Image', $img);
  385. $this->assertInternalType('int', $img->width);
  386. $this->assertInternalType('int', $img->height);
  387. $this->assertEquals($img->width, 300);
  388. $this->assertEquals($img->height, 200);
  389. $this->assertEquals('#ffedcc', $img->pickColor(0, 0, 'hex'));
  390. $this->assertEquals('#ffb42b', $img->pickColor(299, 199, 'hex'));
  391. $img = $this->getTestImage();
  392. $img->resizeCanvas(300, 200, 'bottom');
  393. $this->assertInstanceOf('Intervention\Image\Image', $img);
  394. $this->assertInternalType('int', $img->width);
  395. $this->assertInternalType('int', $img->height);
  396. $this->assertEquals($img->width, 300);
  397. $this->assertEquals($img->height, 200);
  398. $this->assertEquals('#ffd179', $img->pickColor(0, 0, 'hex'));
  399. $this->assertEquals('#ffa600', $img->pickColor(299, 199, 'hex'));
  400. $img = $this->getTestImage();
  401. $img->resizeCanvas(300, 200, 'bottom-right');
  402. $this->assertInstanceOf('Intervention\Image\Image', $img);
  403. $this->assertInternalType('int', $img->width);
  404. $this->assertInternalType('int', $img->height);
  405. $this->assertEquals($img->width, 300);
  406. $this->assertEquals($img->height, 200);
  407. $this->assertEquals('#ffb42a', $img->pickColor(0, 0, 'hex'));
  408. $this->assertEquals('#ffa600', $img->pickColor(299, 199, 'hex'));
  409. // resize relative from center 5px border in magenta
  410. $img = $this->getTestImage();
  411. $img->resizeCanvas(10, 10, 'center', true, 'ff00ff');
  412. $this->assertInstanceOf('Intervention\Image\Image', $img);
  413. $this->assertInternalType('int', $img->width);
  414. $this->assertInternalType('int', $img->height);
  415. $this->assertEquals($img->width, 810);
  416. $this->assertEquals($img->height, 610);
  417. $this->assertEquals('#ff00ff', $img->pickColor(0, 0, 'hex'));
  418. $this->assertEquals('#ff00ff', $img->pickColor(809, 609, 'hex'));
  419. // resize just width
  420. $img = $this->getTestImage();
  421. $img->resizeCanvas(300, null);
  422. $this->assertInstanceOf('Intervention\Image\Image', $img);
  423. $this->assertInternalType('int', $img->width);
  424. $this->assertInternalType('int', $img->height);
  425. $this->assertEquals($img->width, 300);
  426. $this->assertEquals($img->height, 600);
  427. $this->assertEquals('#fffbf2', $img->pickColor(0, 0, 'hex'));
  428. $this->assertEquals('#ffa600', $img->pickColor(299, 599, 'hex'));
  429. // resize just height
  430. $img = $this->getTestImage();
  431. $img->resizeCanvas(null, 200);
  432. $this->assertInstanceOf('Intervention\Image\Image', $img);
  433. $this->assertInternalType('int', $img->width);
  434. $this->assertInternalType('int', $img->height);
  435. $this->assertEquals($img->width, 800);
  436. $this->assertEquals($img->height, 200);
  437. $this->assertEquals('#fefdf9', $img->pickColor(0, 0, 'hex'));
  438. $this->assertEquals('#ffa600', $img->pickColor(799, 199, 'hex'));
  439. // smaller width, larger height
  440. $img = $this->getTestImage();
  441. $img->resizeCanvas(300, 800);
  442. $this->assertInstanceOf('Intervention\Image\Image', $img);
  443. $this->assertInternalType('int', $img->width);
  444. $this->assertInternalType('int', $img->height);
  445. $this->assertEquals($img->width, 300);
  446. $this->assertEquals($img->height, 800);
  447. $this->assertEquals('#000000', $img->pickColor(0, 0, 'hex'));
  448. $this->assertEquals('#000000', $img->pickColor(299, 799, 'hex'));
  449. // larger width, smaller height
  450. $img = $this->getTestImage();
  451. $img->resizeCanvas(900, 200);
  452. $this->assertInstanceOf('Intervention\Image\Image', $img);
  453. $this->assertInternalType('int', $img->width);
  454. $this->assertInternalType('int', $img->height);
  455. $this->assertEquals($img->width, 900);
  456. $this->assertEquals($img->height, 200);
  457. $this->assertEquals('#000000', $img->pickColor(0, 0, 'hex'));
  458. $this->assertEquals('#000000', $img->pickColor(899, 199, 'hex'));
  459. // test negative values (for relative resize)
  460. $img = $this->getTestImage();
  461. $img->resizeCanvas(-200, -200);
  462. $this->assertInstanceOf('Intervention\Image\Image', $img);
  463. $this->assertInternalType('int', $img->width);
  464. $this->assertInternalType('int', $img->height);
  465. $this->assertEquals($img->width, 600);
  466. $this->assertEquals($img->height, 400);
  467. $this->assertEquals('#fffefc', $img->pickColor(0, 0, 'hex'));
  468. $this->assertEquals('#ffa600', $img->pickColor(599, 399, 'hex'));
  469. }
  470. public function testCropImage()
  471. {
  472. $img = $this->getTestImage();
  473. $img->crop(100, 100);
  474. $this->assertInternalType('int', $img->width);
  475. $this->assertInternalType('int', $img->height);
  476. $this->assertEquals($img->width, 100);
  477. $this->assertEquals($img->height, 100);
  478. $this->assertEquals('#ffbe46', $img->pickColor(99, 99, 'hex'));
  479. $img = $this->getTestImage();
  480. $img->crop(100, 100, 650, 400);
  481. $this->assertInternalType('int', $img->width);
  482. $this->assertInternalType('int', $img->height);
  483. $this->assertEquals($img->width, 100);
  484. $this->assertEquals($img->height, 100);
  485. $this->assertEquals('#ffa600', $img->pickColor(99, 99, 'hex'));
  486. }
  487. /**
  488. * @expectedException Intervention\Image\Exception\DimensionOutOfBoundsException
  489. */
  490. public function testCropImageWithoutDimensions()
  491. {
  492. $img = $this->getTestImage();
  493. $img->crop(null, null);
  494. }
  495. /**
  496. * @expectedException Intervention\Image\Exception\DimensionOutOfBoundsException
  497. */
  498. public function testCropImageWithNonNumericDimensions()
  499. {
  500. $img = $this->getTestImage();
  501. $img->crop('a', 'z');
  502. }
  503. public function testLegacyResize()
  504. {
  505. // auto height
  506. $img = $this->getTestImage();
  507. $img->resize(array('width' => '320'));
  508. $this->assertInternalType('int', $img->width);
  509. $this->assertInternalType('int', $img->height);
  510. $this->assertEquals($img->width, 320);
  511. $this->assertEquals($img->height, 240);
  512. // auto width
  513. $img = $this->getTestImage();
  514. $img->resize(array('height' => '240'));
  515. $this->assertInternalType('int', $img->width);
  516. $this->assertInternalType('int', $img->height);
  517. $this->assertEquals($img->width, 320);
  518. $this->assertEquals($img->height, 240);
  519. }
  520. public function testGrabImage()
  521. {
  522. $img = $this->getTestImage();
  523. $img->grab(200);
  524. $this->assertInternalType('int', $img->width);
  525. $this->assertInternalType('int', $img->height);
  526. $this->assertEquals($img->width, 200);
  527. $this->assertEquals($img->height, 200);
  528. $this->assertEquals($img->pickColor(50, 50, 'hex'), '#feedcc');
  529. $this->assertEquals($img->pickColor(140, 20, 'hex'), '#fed891');
  530. $img = $this->getTestImage();
  531. $img->grab(200, 100);
  532. $this->assertInternalType('int', $img->width);
  533. $this->assertInternalType('int', $img->height);
  534. $this->assertEquals($img->width, 200);
  535. $this->assertEquals($img->height, 100);
  536. $this->assertEquals($img->pickColor(50, 25, 'hex'), '#ffeccb');
  537. $this->assertEquals($img->pickColor(180, 40, 'hex'), '#fead15');
  538. $img = $this->getTestImage();
  539. $img->grab(null, 100);
  540. $this->assertInternalType('int', $img->width);
  541. $this->assertInternalType('int', $img->height);
  542. $this->assertEquals($img->width, 100);
  543. $this->assertEquals($img->height, 100);
  544. $this->assertEquals($img->pickColor(30, 30, 'hex'), '#fee5b7');
  545. $this->assertEquals($img->pickColor(95, 20, 'hex'), '#ffbe47');
  546. $img = $this->getTestImage();
  547. $img->grab(array('width' => '100'));
  548. $this->assertInternalType('int', $img->width);
  549. $this->assertInternalType('int', $img->height);
  550. $this->assertEquals($img->width, 100);
  551. $this->assertEquals($img->height, 100);
  552. $this->assertEquals($img->pickColor(30, 30, 'hex'), '#fee5b7');
  553. $this->assertEquals($img->pickColor(95, 20, 'hex'), '#ffbe47');
  554. $img = $this->getTestImage();
  555. $img->grab(array('height' => '200'));
  556. $this->assertInternalType('int', $img->width);
  557. $this->assertInternalType('int', $img->height);
  558. $this->assertEquals($img->width, 200);
  559. $this->assertEquals($img->height, 200);
  560. $this->assertEquals($img->pickColor(30, 30, 'hex'), '#fff9ed');
  561. $this->assertEquals($img->pickColor(95, 20, 'hex'), '#ffe8bf');
  562. }
  563. /**
  564. * @expectedException Intervention\Image\Exception\DimensionOutOfBoundsException
  565. */
  566. public function testGrabImageWithoutDimensions()
  567. {
  568. $img = $this->getTestImage();
  569. $img->grab();
  570. }
  571. public function testFlipImage()
  572. {
  573. $img = $this->getTestImage();
  574. $img->flip('h');
  575. $this->assertInstanceOf('Intervention\Image\Image', $img);
  576. $this->assertEquals('#ffbf47', $img->pickColor(0, 0, 'hex'));
  577. $img = $this->getTestImage();
  578. $img->flip('v');
  579. $this->assertInstanceOf('Intervention\Image\Image', $img);
  580. $this->assertEquals('#fed78c', $img->pickColor(0, 0, 'hex'));
  581. }
  582. public function testRotateImage()
  583. {
  584. $img = $this->getTestImage();
  585. $img->rotate(90);
  586. $this->assertInstanceOf('Intervention\Image\Image', $img);
  587. $this->assertInternalType('int', $img->width);
  588. $this->assertInternalType('int', $img->height);
  589. $this->assertEquals($img->width, 600);
  590. $this->assertEquals($img->height, 800);
  591. $this->assertEquals('#ffbf47', $img->pickColor(0, 0, 'hex'));
  592. $img = $this->getTestImage();
  593. $img->rotate(180);
  594. $this->assertInstanceOf('Intervention\Image\Image', $img);
  595. $this->assertInternalType('int', $img->width);
  596. $this->assertInternalType('int', $img->height);
  597. $this->assertEquals($img->width, 800);
  598. $this->assertEquals($img->height, 600);
  599. $this->assertEquals('#ffa600', $img->pickColor(0, 0, 'hex'));
  600. // rotate transparent png and keep transparency
  601. $img = Image::make('public/circle.png');
  602. $img->rotate(180);
  603. $this->assertInstanceOf('Intervention\Image\Image', $img);
  604. $this->assertInternalType('int', $img->width);
  605. $this->assertInternalType('int', $img->height);
  606. $this->assertEquals($img->width, 50);
  607. $this->assertEquals($img->height, 50);
  608. $checkColor = $img->pickColor(0, 0, 'array');
  609. $this->assertEquals($checkColor['r'], 0);
  610. $this->assertEquals($checkColor['g'], 0);
  611. $this->assertEquals($checkColor['b'], 0);
  612. $this->assertEquals($checkColor['a'], 0);
  613. }
  614. public function testInsertImage()
  615. {
  616. $img = Image::canvas(32, 32, '#ff0000'); // create canvas
  617. $watermark = Image::canvas(16, 16, '#0000ff'); // create watermark
  618. // top-left anchor
  619. $img->insert($watermark, 0, 0, 'top-left');
  620. $this->assertInstanceOf('Intervention\Image\Image', $img);
  621. $this->assertInternalType('int', $img->width);
  622. $this->assertInternalType('int', $img->height);
  623. $this->assertEquals($img->width, 32);
  624. $this->assertEquals($img->height, 32);
  625. $this->assertEquals('#0000ff', $img->pickColor(0, 0, 'hex'));
  626. $this->assertEquals('#ff0000', $img->pickColor(16, 16, 'hex'));
  627. $img->reset();
  628. // top-left anchor coordinates
  629. $img->insert($watermark, 10, 10, 'top-left');
  630. $this->assertInstanceOf('Intervention\Image\Image', $img);
  631. $this->assertInternalType('int', $img->width);
  632. $this->assertInternalType('int', $img->height);
  633. $this->assertEquals($img->width, 32);
  634. $this->assertEquals($img->height, 32);
  635. $this->assertEquals('#ff0000', $img->pickColor(9, 9, 'hex'));
  636. $this->assertEquals('#0000ff', $img->pickColor(10, 10, 'hex'));
  637. $img->reset();
  638. // top anchor
  639. $img->insert($watermark, 0, 0, 'top');
  640. $this->assertInstanceOf('Intervention\Image\Image', $img);
  641. $this->assertInternalType('int', $img->width);
  642. $this->assertInternalType('int', $img->height);
  643. $this->assertEquals($img->width, 32);
  644. $this->assertEquals($img->height, 32);
  645. $this->assertEquals('#ff0000', $img->pickColor(0, 0, 'hex'));
  646. $this->assertEquals('#0000ff', $img->pickColor(23, 15, 'hex'));
  647. $img->reset();
  648. // top anchor coordinates
  649. $img->insert($watermark, 10, 10, 'top');
  650. $this->assertInstanceOf('Intervention\Image\Image', $img);
  651. $this->assertInternalType('int', $img->width);
  652. $this->assertInternalType('int', $img->height);
  653. $this->assertEquals($img->width, 32);
  654. $this->assertEquals($img->height, 32);
  655. $this->assertEquals('#0000ff', $img->pickColor(18, 10, 'hex'));
  656. $this->assertEquals('#ff0000', $img->pickColor(31, 26, 'hex'));
  657. $img->reset();
  658. // top-right anchor
  659. $img->insert($watermark, 0, 0, 'top-right');
  660. $this->assertInstanceOf('Intervention\Image\Image', $img);
  661. $this->assertInternalType('int', $img->width);
  662. $this->assertInternalType('int', $img->height);
  663. $this->assertEquals($img->width, 32);
  664. $this->assertEquals($img->height, 32);
  665. $this->assertEquals('#ff0000', $img->pickColor(15, 0, 'hex'));
  666. $this->assertEquals('#0000ff', $img->pickColor(31, 0, 'hex'));
  667. $img->reset();
  668. // top-right anchor coordinates
  669. $img->insert($watermark, 10, 10, 'top-right');
  670. $this->assertInstanceOf('Intervention\Image\Image', $img);
  671. $this->assertInternalType('int', $img->width);
  672. $this->assertInternalType('int', $img->height);
  673. $this->assertEquals($img->width, 32);
  674. $this->assertEquals($img->height, 32);
  675. $this->assertEquals('#ff0000', $img->pickColor(6, 9, 'hex'));
  676. $this->assertEquals('#0000ff', $img->pickColor(21, 25, 'hex'));
  677. $img->reset();
  678. // left anchor
  679. $img->insert($watermark, 0, 0, 'left');
  680. $this->assertInstanceOf('Intervention\Image\Image', $img);
  681. $this->assertInternalType('int', $img->width);
  682. $this->assertInternalType('int', $img->height);
  683. $this->assertEquals($img->width, 32);
  684. $this->assertEquals($img->height, 32);
  685. $this->assertEquals('#0000ff', $img->pickColor(15, 23, 'hex'));
  686. $this->assertEquals('#ff0000', $img->pickColor(0, 7, 'hex'));
  687. $img->reset();
  688. // left anchor coordinates
  689. $img->insert($watermark, 10, 10, 'left');
  690. $this->assertInstanceOf('Intervention\Image\Image', $img);
  691. $this->assertInternalType('int', $img->width);
  692. $this->assertInternalType('int', $img->height);
  693. $this->assertEquals($img->width, 32);
  694. $this->assertEquals($img->height, 32);
  695. $this->assertEquals('#0000ff', $img->pickColor(25, 31, 'hex'));
  696. $this->assertEquals('#ff0000', $img->pickColor(10, 17, 'hex'));
  697. $img->reset();
  698. // right anchor
  699. $img->insert($watermark, 0, 0, 'right');
  700. $this->assertInstanceOf('Intervention\Image\Image', $img);
  701. $this->assertInternalType('int', $img->width);
  702. $this->assertInternalType('int', $img->height);
  703. $this->assertEquals($img->width, 32);
  704. $this->assertEquals($img->height, 32);
  705. $this->assertEquals('#0000ff', $img->pickColor(31, 23, 'hex'));
  706. $this->assertEquals('#ff0000', $img->pickColor(15, 15, 'hex'));
  707. $img->reset();
  708. // right anchor coordinates
  709. $img->insert($watermark, 10, 10, 'right');
  710. $this->assertInstanceOf('Intervention\Image\Image', $img);
  711. $this->assertInternalType('int', $img->width);
  712. $this->assertInternalType('int', $img->height);
  713. $this->assertEquals($img->width, 32);
  714. $this->assertEquals($img->height, 32);
  715. $this->assertEquals('#0000ff', $img->pickColor(21, 31, 'hex'));
  716. $this->assertEquals('#ff0000', $img->pickColor(5, 18, 'hex'));
  717. $img->reset();
  718. // bottom-left anchor
  719. $img->insert($watermark, 0, 0, 'bottom-left');
  720. $this->assertInstanceOf('Intervention\Image\Image', $img);
  721. $this->assertInternalType('int', $img->width);
  722. $this->assertInternalType('int', $img->height);
  723. $this->assertEquals($img->width, 32);
  724. $this->assertEquals($img->height, 32);
  725. $this->assertEquals('#0000ff', $img->pickColor(15, 31, 'hex'));
  726. $this->assertEquals('#ff0000', $img->pickColor(0, 15, 'hex'));
  727. $img->reset();
  728. // bottom-left anchor coordinates
  729. $img->insert($watermark, 10, 10, 'bottom-left');
  730. $this->assertInstanceOf('Intervention\Image\Image', $img);
  731. $this->assertInternalType('int', $img->width);
  732. $this->assertInternalType('int', $img->height);
  733. $this->assertEquals($img->width, 32);
  734. $this->assertEquals($img->height, 32);
  735. $this->assertEquals('#0000ff', $img->pickColor(10, 21, 'hex'));
  736. $this->assertEquals('#ff0000', $img->pickColor(9, 20, 'hex'));
  737. $img->reset();
  738. // bottom anchor
  739. $img->insert($watermark, 0, 0, 'bottom');
  740. $this->assertInstanceOf('Intervention\Image\Image', $img);
  741. $this->assertInternalType('int', $img->width);
  742. $this->assertInternalType('int', $img->height);
  743. $this->assertEquals($img->width, 32);
  744. $this->assertEquals($img->height, 32);
  745. $this->assertEquals('#0000ff', $img->pickColor(8, 16, 'hex'));
  746. $this->assertEquals('#ff0000', $img->pickColor(8, 15, 'hex'));
  747. $img->reset();
  748. // bottom anchor coordinates
  749. $img->insert($watermark, 10, 10, 'bottom');
  750. $this->assertInstanceOf('Intervention\Image\Image', $img);
  751. $this->assertInternalType('int', $img->width);
  752. $this->assertInternalType('int', $img->height);
  753. $this->assertEquals($img->width, 32);
  754. $this->assertEquals($img->height, 32);
  755. $this->assertEquals('#0000ff', $img->pickColor(18, 21, 'hex'));
  756. $this->assertEquals('#ff0000', $img->pickColor(17, 21, 'hex'));
  757. $img->reset();
  758. // bottom-right anchor
  759. $img->insert($watermark, 0, 0, 'bottom-right');
  760. $this->assertInstanceOf('Intervention\Image\Image', $img);
  761. $this->assertInternalType('int', $img->width);
  762. $this->assertInternalType('int', $img->height);
  763. $this->assertEquals($img->width, 32);
  764. $this->assertEquals($img->height, 32);
  765. $this->assertEquals('#0000ff', $img->pickColor(16, 16, 'hex'));
  766. $this->assertEquals('#ff0000', $img->pickColor(15, 16, 'hex'));
  767. $img->reset();
  768. // bottom-right anchor coordinates
  769. $img->insert($watermark, 10, 10, 'bottom-right');
  770. $this->assertInstanceOf('Intervention\Image\Image', $img);
  771. $this->assertInternalType('int', $img->width);
  772. $this->assertInternalType('int', $img->height);
  773. $this->assertEquals($img->width, 32);
  774. $this->assertEquals($img->height, 32);
  775. $this->assertEquals('#0000ff', $img->pickColor(21, 21, 'hex'));
  776. $this->assertEquals('#ff0000', $img->pickColor(22, 22, 'hex'));
  777. $img->reset();
  778. // center anchor
  779. $img->insert($watermark, 0, 0, 'center');
  780. $this->assertInstanceOf('Intervention\Image\Image', $img);
  781. $this->assertInternalType('int', $img->width);
  782. $this->assertInternalType('int', $img->height);
  783. $this->assertEquals($img->width, 32);
  784. $this->assertEquals($img->height, 32);
  785. $this->assertEquals('#0000ff', $img->pickColor(23, 23, 'hex'));
  786. $this->assertEquals('#ff0000', $img->pickColor(8, 7, 'hex'));
  787. $img->reset();
  788. // center anchor coordinates
  789. $img->insert($watermark, 10, 10, 'center');
  790. $this->assertInstanceOf('Intervention\Image\Image', $img);
  791. $this->assertInternalType('int', $img->width);
  792. $this->assertInternalType('int', $img->height);
  793. $this->assertEquals($img->width, 32);
  794. $this->assertEquals($img->height, 32);
  795. $this->assertEquals('#0000ff', $img->pickColor(31, 31, 'hex'));
  796. $this->assertEquals('#ff0000', $img->pickColor(18, 17, 'hex'));
  797. $img->reset();
  798. }
  799. public function testInsertImageFromResource()
  800. {
  801. $resource = imagecreatefrompng('public/tile.png');
  802. $img = Image::canvas(16, 16)->insert($resource);
  803. $this->assertInstanceOf('Intervention\Image\Image', $img);
  804. $this->assertInternalType('int', $img->width);
  805. $this->assertInternalType('int', $img->height);
  806. $this->assertEquals($img->width, 16);
  807. $this->assertEquals($img->height, 16);
  808. $this->assertEquals('#b4e000', $img->pickColor(0, 0, 'hex'));
  809. $this->assertEquals('#445160', $img->pickColor(15, 15, 'hex'));
  810. }
  811. public function testInsertImageFromBinary()
  812. {
  813. $data = file_get_contents('public/tile.png');
  814. $img = Image::canvas(16, 16)->insert($data);
  815. $this->assertInstanceOf('Intervention\Image\Image', $img);
  816. $this->assertInternalType('int', $img->width);
  817. $this->assertInternalType('int', $img->height);
  818. $this->assertEquals($img->width, 16);
  819. $this->assertEquals($img->height, 16);
  820. $this->assertEquals('#b4e000', $img->pickColor(0, 0, 'hex'));
  821. $this->assertEquals('#445160', $img->pickColor(15, 15, 'hex'));
  822. }
  823. public function testInsertImageFromObject()
  824. {
  825. $obj = new Image('public/tile.png');
  826. $img = Image::canvas(16, 16)->insert($obj);
  827. $this->assertInstanceOf('Intervention\Image\Image', $img);
  828. $this->assertInternalType('int', $img->width);
  829. $this->assertInternalType('int', $img->height);
  830. $this->assertEquals($img->width, 16);
  831. $this->assertEquals($img->height, 16);
  832. $this->assertEquals('#b4e000', $img->pickColor(0, 0, 'hex'));
  833. $this->assertEquals('#445160', $img->pickColor(15, 15, 'hex'));
  834. }
  835. public function testInsertImageFromPath()
  836. {
  837. $img = Image::canvas(16, 16)->insert('public/tile.png');
  838. $this->assertInstanceOf('Intervention\Image\Image', $img);
  839. $this->assertInternalType('int', $img->width);
  840. $this->assertInternalType('int', $img->height);
  841. $this->assertEquals($img->width, 16);
  842. $this->assertEquals($img->height, 16);
  843. $this->assertEquals('#b4e000', $img->pickColor(0, 0, 'hex'));
  844. $this->assertEquals('#445160', $img->pickColor(15, 15, 'hex'));
  845. }
  846. public function testOpacity()
  847. {
  848. // simple image mask
  849. $img = Image::make('public/test.jpg');
  850. $img->resize(32, 32)->opacity(50);
  851. $this->assertInstanceOf('Intervention\Image\Image', $img);
  852. $this->assertInternalType('int', $img->width);
  853. $this->assertInternalType('int', $img->height);
  854. $this->assertEquals($img->width, 32);
  855. $this->assertEquals($img->height, 32);
  856. $checkColor = $img->pickColor(15, 15, 'array');
  857. $this->assertEquals($checkColor['r'], 254);
  858. $this->assertEquals($checkColor['g'], 204);
  859. $this->assertEquals($checkColor['b'], 112);
  860. $this->assertEquals($checkColor['a'], 0.5);
  861. $checkColor = $img->pickColor(31, 31, 'array');
  862. $this->assertEquals($checkColor['r'], 255);
  863. $this->assertEquals($checkColor['g'], 166);
  864. $this->assertEquals($checkColor['b'], 0);
  865. $this->assertEquals($checkColor['a'], 0.5);
  866. }
  867. /**
  868. * @expectedException Intervention\Image\Exception\OpacityOutOfBoundsException
  869. */
  870. public function testOpacityTooHigh()
  871. {
  872. $img = $this->getTestImage();
  873. $img->opacity(101);
  874. }
  875. /**
  876. * @expectedException Intervention\Image\Exception\OpacityOutOfBoundsException
  877. */
  878. public function testOpacityTooLow()
  879. {
  880. $img = $this->getTestImage();
  881. $img->opacity(-1);
  882. }
  883. /**
  884. * @expectedException Intervention\Image\Exception\OpacityOutOfBoundsException
  885. */
  886. public function testOpacityAlphaChar()
  887. {
  888. $img = $this->getTestImage();
  889. $img->opacity('a');
  890. }
  891. public function testMaskImage()
  892. {
  893. // simple image mask
  894. $img = Image::make('public/test.jpg');
  895. $img->resize(32, 32)->mask('public/mask1.png', false);
  896. $this->assertInstanceOf('Intervention\Image\Image', $img);
  897. $this->assertInternalType('int', $img->width);
  898. $this->assertInternalType('int', $img->height);
  899. $this->assertEquals($img->width, 32);
  900. $this->assertEquals($img->height, 32);
  901. $checkColor = $img->pickColor(16, 2, 'array');
  902. $this->assertEquals($checkColor['r'], 254);
  903. $this->assertEquals($checkColor['g'], 230);
  904. $this->assertEquals($checkColor['b'], 186);
  905. $this->assertEquals($checkColor['a'], 0.83);
  906. $checkColor = $img->pickColor(31, 31, 'array');
  907. $this->assertEquals($checkColor['r'], 0);
  908. $this->assertEquals($checkColor['g'], 0);
  909. $this->assertEquals($checkColor['b'], 0);
  910. $this->assertEquals($checkColor['a'], 0);
  911. // use alpha channel as mask
  912. $img = Image::make('public/test.jpg');
  913. $img->resize(32, 32)->mask('public/mask2.png', true);
  914. $this->assertInstanceOf('Intervention\Image\Image', $img);
  915. $this->assertInternalType('int', $img->width);
  916. $this->assertInternalType('int', $img->height);
  917. $this->assertEquals($img->width, 32);
  918. $this->assertEquals($img->height, 32);
  919. $checkColor = $img->pickColor(5, 5, 'array');
  920. $this->assertEquals($checkColor['r'], 0);
  921. $this->assertEquals($checkColor['g'], 0);
  922. $this->assertEquals($checkColor['b'], 0);
  923. $this->assertEquals($checkColor['a'], 0);
  924. $checkColor = $img->pickColor(20, 15, 'array');
  925. $this->assertEquals($checkColor['r'], 254);
  926. $this->assertEquals($checkColor['g'], 190);
  927. $this->assertEquals($checkColor['b'], 69);
  928. $this->assertEquals($checkColor['a'], 1);
  929. // preserve existing alpha channel
  930. $img = Image::make('public/circle.png');
  931. $img->resize(32, 32)->mask('public/mask2.png', true);
  932. $this->assertInstanceOf('Intervention\Image\Image', $img);
  933. $this->assertInternalType('int', $img->width);
  934. $this->assertInternalType('int', $img->height);
  935. $this->assertEquals($img->width, 32);
  936. $this->assertEquals($img->height, 32);
  937. $checkColor = $img->pickColor(5, 5, 'array');
  938. $this->assertEquals($checkColor['r'], 0);
  939. $this->assertEquals($checkColor['g'], 0);
  940. $this->assertEquals($checkColor['b'], 0);
  941. $this->assertEquals($checkColor['a'], 0);
  942. $checkColor = $img->pickColor(15, 15, 'array');
  943. $this->assertEquals($checkColor['r'], 0);
  944. $this->assertEquals($checkColor['g'], 0);
  945. $this->assertEquals($checkColor['b'], 0);
  946. $this->assertEquals($checkColor['a'], 0.8);
  947. }
  948. public function testMaskWithResource()
  949. {
  950. $img = Image::make('public/circle.png');
  951. $resource = imagecreatefrompng('public/mask2.png');
  952. $img->resize(32, 32)->mask($resource, true);
  953. $this->assertInstanceOf('Intervention\Image\Image', $img);
  954. $this->assertInternalType('int', $img->width);
  955. $this->assertInternalType('int', $img->height);
  956. $this->assertEquals($img->width, 32);
  957. $this->assertEquals($img->height, 32);
  958. $checkColor = $img->pickColor(5, 5, 'array');
  959. $this->assertEquals($checkColor['r'], 0);
  960. $this->assertEquals($checkColor['g'], 0);
  961. $this->assertEquals($checkColor['b'], 0);
  962. $this->assertEquals($checkColor['a'], 0);
  963. $checkColor = $img->pickColor(15, 15, 'array');
  964. $this->assertEquals($checkColor['r'], 0);
  965. $this->assertEquals($checkColor['g'], 0);
  966. $this->assertEquals($checkColor['b'], 0);
  967. $this->assertEquals($checkColor['a'], 0.8);
  968. }
  969. public function testMaskWithBinary()
  970. {
  971. $img = Image::make('public/circle.png');
  972. $data = file_get_contents('public/mask2.png');
  973. $img->resize(32, 32)->mask($data, true);
  974. $this->assertInstanceOf('Intervention\Image\Image', $img);
  975. $this->assertInternalType('int', $img->width);
  976. $this->assertInternalType('int', $img->height);
  977. $this->assertEquals($img->width, 32);
  978. $this->assertEquals($img->height, 32);
  979. $checkColor = $img->pickColor(5, 5, 'array');
  980. $this->assertEquals($checkColor['r'], 0);
  981. $this->assertEquals($checkColor['g'], 0);
  982. $this->assertEquals($checkColor['b'], 0);
  983. $this->assertEquals($checkColor['a'], 0);
  984. $checkColor = $img->pickColor(15, 15, 'array');
  985. $this->assertEquals($checkColor['r'], 0);
  986. $this->assertEquals($checkColor['g'], 0);
  987. $this->assertEquals($checkColor['b'], 0);
  988. $this->assertEquals($checkColor['a'], 0.8);
  989. }
  990. public function testMaskWithObject()
  991. {
  992. $img = Image::make('public/circle.png');
  993. $obj = Image::make('public/mask2.png');
  994. $img->resize(32, 32)->mask($obj, true);
  995. $this->assertInstanceOf('Intervention\Image\Image', $img);
  996. $this->assertInternalType('int', $img->width);
  997. $this->assertInternalType('int', $img->height);
  998. $this->assertEquals($img->width, 32);
  999. $this->assertEquals($img->height, 32);
  1000. $checkColor = $img->pickColor(5, 5, 'array');
  1001. $this->assertEquals($checkColor['r'], 0);
  1002. $this->assertEquals($checkColor['g'], 0);
  1003. $this->assertEquals($checkColor['b'], 0);
  1004. $this->assertEquals($checkColor['a'], 0);
  1005. $checkColor = $img->pickColor(15, 15, 'array');
  1006. $this->assertEquals($checkColor['r'], 0);
  1007. $this->assertEquals($checkColor['g'], 0);
  1008. $this->assertEquals($checkColor['b'], 0);
  1009. $this->assertEquals($checkColor['a'], 0.8);
  1010. }
  1011. public function testPixelateImage()
  1012. {
  1013. $img = $this->getTestImage();
  1014. $img->pixelate(20);
  1015. $this->assertInstanceOf('Intervention\Image\Image', $img);
  1016. }
  1017. public function testGreyscaleImage()
  1018. {
  1019. $img = $this->getTestImage();
  1020. $img->greyscale();
  1021. $this->assertInstanceOf('Intervention\Image\Image', $img);
  1022. $this->assertEquals('#adadad', $img->pickColor(660, 450, 'hex'));
  1023. }
  1024. public function testInvertImage()
  1025. {
  1026. $img = $this->getTestImage();
  1027. $img->invert();
  1028. $this->assertInstanceOf('Intervention\Image\Image', $img);
  1029. $this->assertEquals('#000000', $img->pickColor(0, 0, 'hex'));
  1030. }
  1031. public function testBlurImage()
  1032. {
  1033. $img = Image::make('public/tile.png')->blur();
  1034. $this->assertInstanceOf('Intervention\Image\Image', $img);
  1035. $this->assertEquals('#b4e000', $img->pickColor(0, 0, 'hex'));
  1036. $this->assertEquals('#98bc18', $img->pickColor(0, 7, 'hex'));
  1037. }
  1038. public function testFillImage()
  1039. {
  1040. $img = new Image(null, 32, 32);
  1041. $img = $img->fill('fdf5e4');
  1042. $this->assertInstanceOf('Intervention\Image\Image', $img);
  1043. $this->assertEquals('#fdf5e4', $img->pickColor(0, 0, 'hex'));
  1044. $img = $img->fill('#fdf5e4');
  1045. $this->assertInstanceOf('Intervention\Image\Image', $img);
  1046. $this->assertEquals('#fdf5e4', $img->pickColor(0, 0, 'hex'));
  1047. $img = $img->fill('ccc');
  1048. $this->assertInstanceOf('Intervention\Image\Image', $img);
  1049. $this->assertEquals('#cccccc', $img->pickColor(0, 0, 'hex'));
  1050. $img = $img->fill('#ccc');
  1051. $this->assertInstanceOf('Intervention\Image\Image', $img);
  1052. $this->assertEquals('#cccccc', $img->pickColor(0, 0, 'hex'));
  1053. $img = $img->fill(array(155, 155, 155), rand(1,10), rand(1,10));
  1054. $this->assertInstanceOf('Intervention\Image\Image', $img);
  1055. $this->assertEquals('#9b9b9b', $img->pickColor(0, 0, 'hex'));
  1056. $img = new Image(null, 32, 32);
  1057. $img = $img->fill('rgba(180, 224, 0, 0.65)', rand(1,10), rand(1,10));
  1058. $checkColor = $img->pickColor(0, 0, 'array');
  1059. $this->assertEquals(180, $checkColor['r']);
  1060. $this->assertEquals(224, $checkColor['g']);
  1061. $this->assertEquals(0, $checkColor['b']);
  1062. $this->assertEquals(0.65, $checkColor['a']);
  1063. }
  1064. public function testFillImageWithResource()
  1065. {
  1066. $resource = imagecreatefrompng('public/tile.png');
  1067. $img = Image::canvas(32, 32)->fill($resource);
  1068. $this->assertInstanceOf('Intervention\Image\Image', $img);
  1069. $this->assertInternalType('int', $img->width);