Browse Source

Upgrade imageFromBlob()

Mark Scherer 10 years ago
parent
commit
35d47a91c5
2 changed files with 42 additions and 1 deletions
  1. 24 1
      src/View/Helper/HtmlHelper.php
  2. 18 0
      tests/TestCase/View/Helper/HtmlHelperTest.php

+ 24 - 1
src/View/Helper/HtmlHelper.php

@@ -29,7 +29,30 @@ use Cake\View\View;
  */
 class HtmlHelper extends CoreHtmlHelper {
 
-    /**
+	/**
+	 * Display image tag from blob content.
+	 * Enhancement for HtmlHelper. Defaults to png image
+	 *
+	 * Options:
+	 * - type: png, gif, jpg, ...
+	 *
+	 * @param string $content Data in binary form
+	 * @param array $options Attributes
+	 * @return string HTML image tag
+	 */
+	public function imageFromBlob($content, array $options = []) {
+		$options += ['type' => 'png'];
+		$mimeType = 'image/' . $options['type'];
+
+		$text = 'data:' . $mimeType . ';base64,' . base64_encode($content);
+
+		return $this->formatTemplate('image', [
+			'url' => $text,
+			'attrs' => $this->templater()->formatAttributes($options, ['block', 'link'])
+		]);
+	}
+
+	/**
      * Creates a reset HTML link.
 	 * The prefix and plugin params are resetting to default false.
      *

+ 18 - 0
tests/TestCase/View/Helper/HtmlHelperTest.php

@@ -1,6 +1,7 @@
 <?php
 namespace Tools\TestCase\View\Helper;
 
+use Cake\Core\Plugin;
 use Tools\View\Helper\HtmlHelper;
 use Tools\TestSuite\TestCase;
 use Cake\View\View;
@@ -15,6 +16,11 @@ use Cake\Routing\Router;
  */
 class HtmlHelperTest extends TestCase {
 
+	/**
+	 * @var \Tools\View\Helper\HtmlHelper
+	 */
+	protected $Html;
+
 	public function setUp() {
 		parent::setUp();
 
@@ -25,6 +31,18 @@ class HtmlHelperTest extends TestCase {
 	}
 
 	/**
+	 * HtmlHelperTest::testImageFromBlob()
+	 *
+	 * @return void
+	 */
+	public function testImageFromBlob() {
+		$folder = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS;
+		$content = file_get_contents($folder . 'hotel.png');
+		$is = $this->Html->imageFromBlob($content);
+		$this->assertTrue(!empty($is));
+	}
+
+	/**
 	 * Tests
 	 *
 	 * @return void