<?php $data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl' . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr' . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r' . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg=='; $data = base64_decode($data); require_once '../Image.php'; use Gregwar\Image\Image; Image::fromData($data)->save('out.jpg');
public function testSaveImage() { $imagePathToSave = __DIR__ . '/' . uniqid() . '.png'; $imageUrl = 'http://upload.wikimedia.org/wikipedia/commons/3/31/Red-dot-5px.png'; // $imageRawBase64 = base64_encode(file_get_contents($imageUrl)); $imageRawBase64 = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='; if (!is_writable(dirname($imagePathToSave))) { $this->markTestSkipped('Directory not writable'); } /** * @var \OAuth\Common\Service\ServiceInterface|\PHPUnit_Framework_MockObject_MockObject $service */ $service = $this->getMock('\\OAuth\\Common\\Service\\AbstractService', ['httpRequest', 'request', 'getAuthorizationUri'], [], '', false); $service->expects($this->any())->method('httpRequest')->willReturn(base64_decode($imageRawBase64)); $extractor = new Extractor(FieldsValues::construct([Extractor::FIELD_IMAGE_URL => $imageUrl])); $extractor->setService($service); $this->assertFalse(is_file($imagePathToSave)); $extractor->saveImage($imagePathToSave, 10, 10); $this->assertTrue(is_file($imagePathToSave)); $fileContents = file_get_contents($imagePathToSave); unlink($imagePathToSave); /** @noinspection PhpUndefinedMethodInspection */ $this->assertEquals(base64_encode(Image::fromData(Image::fromData(base64_decode($imageRawBase64))->resize(10, 10)->get())->get('png')), base64_encode($fileContents)); }
/** * Testing creating image from data. */ public function testData() { $data = file_get_contents(__DIR__ . '/files/monalisa.jpg'); $output = $this->output('mona.jpg'); $image = Image::fromData($data); $image->save($output); $this->assertTrue(file_exists($output)); $i = imagecreatefromjpeg($output); $this->assertSame(771, imagesx($i)); $this->assertSame(961, imagesy($i)); }
/** * {@inheritdoc} */ public function getImageRawData($width = false, $height = false) { if (!$this->supportsImageUrl()) { throw new Exception('Image url is not supported by "' . basename(get_class($this)) . '" class!'); } if (!$this->getImageUrl()) { throw new Exception('Image url is empty'); } $rawImage = $this->service->httpRequest($this->getImageUrl(), [], [], 'GET'); $image = Image::fromData($rawImage); if ($width or $height) { /** @noinspection PhpUndefinedMethodInspection */ $image->resize($width ? $width : null, $height ? $height : null); } try { return $image->get(); } catch (\Exception $e) { throw new Exception('Exception occurred during saving image: ' . $e->getMessage(), $e->getCode(), $e); } }