From: Andrey Kutejko Date: Sat, 27 Apr 2013 12:31:34 +0000 (+0300) Subject: image processor X-Git-Tag: 0.5~292 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=e958542706bddfceb81a6b99524289ad3b2568ae;p=ipf.git image processor --- diff --git a/.gitignore b/.gitignore index e5fa1cf..8fc2f03 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor /composer.phar +/t/project/htdocs/media/upload/thumbs diff --git a/ipf/image/processor.php b/ipf/image/processor.php new file mode 100644 index 0000000..37d3c8f --- /dev/null +++ b/ipf/image/processor.php @@ -0,0 +1,43 @@ +calls[] = array($name, $args); + return $this; + } + + private function play($image) + { + foreach ($this->calls as $call) { + list($method, $args) = $call; + $image = call_user_func_array(array($image, $method), $args); + } + return $image; + } + + public function execute($sourceUrl, $directory, $root=null) + { + if ($root === null) + $root = IPF::get('upload_path') . DIRECTORY_SEPARATOR; + + $destinationUrl = IPF_Utils::insertDirectory($sourceUrl, $directory); + $path = $root . $destinationUrl; + if (!is_file($path)) { + IPF_Utils::makeDirectories(dirname($path)); + $image = IPF_Image::load($root . $sourceUrl); + $image = $this->play($image); + $image->save($path); + } + return $destinationUrl; + } +} + diff --git a/t/ImageProcessorTest.php b/t/ImageProcessorTest.php new file mode 100644 index 0000000..d364db2 --- /dev/null +++ b/t/ImageProcessorTest.php @@ -0,0 +1,35 @@ +root = IPF::get('upload_path'); + IPF_Utils::removeDirectories($this->root . '/thumbs'); + } + + protected function tearDown() + { + IPF_Utils::removeDirectories($this->root . '/thumbs'); + } + + public function testRecording() + { + $url = IPF_Image_Processor::create() + ->thumbnailCrop(250, 125, 0.2, 0.2) + ->fit(200, 200) + ->execute('ipflogo.gif', 'thumbs'); + $this->assertEquals('thumbs/ipflogo.gif', $url); + $this->assertFileExists($this->root . '/thumbs/ipflogo.gif'); + } + + public function testBackground() + { + $url = IPF_Image_Processor::create() + ->thumbnailFill(250, 125, 0x0F0000FF) + ->execute('ipflogo.gif', 'thumbs'); + $this->assertEquals('thumbs/ipflogo.gif', $url); + $this->assertFileExists($this->root . '/thumbs/ipflogo.gif'); + } +} + diff --git a/t/project/htdocs/media/upload/ipflogo.gif b/t/project/htdocs/media/upload/ipflogo.gif new file mode 100644 index 0000000..b06c9bd Binary files /dev/null and b/t/project/htdocs/media/upload/ipflogo.gif differ