]> git.andy128k.dev Git - ipf.git/commitdiff
image processor
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 27 Apr 2013 12:31:34 +0000 (15:31 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 27 Apr 2013 12:31:34 +0000 (15:31 +0300)
.gitignore
ipf/image/processor.php [new file with mode: 0644]
t/ImageProcessorTest.php [new file with mode: 0644]
t/project/htdocs/media/upload/ipflogo.gif [new file with mode: 0644]

index e5fa1cf7614bdaee3934f52649eb84212c9bd076..8fc2f03f72fe6db4a517e7411eb4fe765c3530e7 100644 (file)
@@ -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 (file)
index 0000000..37d3c8f
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+class IPF_Image_Processor
+{
+    private $calls = array();
+
+    public static function create()
+    {
+        return new IPF_Image_Processor;
+    }
+
+    public function __call($name, $args)
+    {
+        $this->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 (file)
index 0000000..d364db2
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+
+class Image_Processor_Test extends PHPUnit_Framework_TestCase
+{
+    protected function setUp()
+    {
+        $this->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 (file)
index 0000000..b06c9bd
Binary files /dev/null and b/t/project/htdocs/media/upload/ipflogo.gif differ