/** * @param string $filename * @param string|null $sex * @param bool $unfiltered */ public function generate($filename, $sex = null, $unfiltered = false) { $DS = DIRECTORY_SEPARATOR; $parts = array('background', 'face', 'clothes', 'hair', 'eyes', 'mouth'); if (!in_array($sex, array('male', 'female'))) { $sex = 1 === mt_rand(1, 2) ? 'male' : 'female'; } $img = Image::create($this->size, $this->size); foreach ($parts as $part) { $images = glob(__DIR__ . $DS . 'img' . $DS . $sex . $DS . $part . $DS . '*.{jpg,jpeg,png,gif}', GLOB_BRACE); if (!$unfiltered) { $images = array_filter($images, function ($image) { $path_parts = pathinfo($image); return false === stristr($path_parts['filename'], 'unfiltered-'); }); } $img->merge(Image::open($images[array_rand($images)])); } $img->save($filename); }
/** * Test that dependencies are well generated. */ public function testDependencies() { $this->assertSame(array(), Image::create(100, 100)->getDependencies()); $this->assertSame(array(), Image::create(100, 100)->merge(Image::create(100, 50))->getDependencies()); $this->assertSame(array('toto.jpg'), Image::open('toto.jpg')->merge(Image::create(100, 50))->getDependencies()); $this->assertSame(array('toto.jpg', 'titi.jpg'), Image::open('toto.jpg')->merge(Image::open('titi.jpg'))->getDependencies()); $this->assertSame(array('toto.jpg', 'titi.jpg', 'tata.jpg'), Image::open('toto.jpg')->merge(Image::open('titi.jpg')->merge(Image::open('tata.jpg')))->getDependencies()); }
<?php require_once '../autoload.php'; use Gregwar\Image\Image; /* * Use the cache with a from-scratch image */ echo Image::create(300, 350)->fill('rgb(255, 150, 150)')->circle(150, 150, 200, 'red', true)->write('./fonts/CaviarDreams.ttf', 'Hello world!', 150, 150, 20, 0, 'white', 'center')->jpeg(); echo "\n";
<?php require_once '../autoload.php'; use Gregwar\Image\Image; Image::create(300, 300)->fill('rgb(255, 150, 150)')->circle(150, 150, 200, 'red', true)->write('./fonts/CaviarDreams.ttf', "Hello world!", 150, 150, 20, 0, 'white', 'center')->save('out.jpg', 'jpeg', 95);