various improvements to image handling .. still some places to fix

This commit is contained in:
Andy Miller 2016-02-02 18:48:39 -07:00
parent 3541ea8ec8
commit c4e72819a6
7 changed files with 67 additions and 12 deletions

View File

@ -255,7 +255,7 @@ trait ParsedownGravTrait
$medium->urlHash($url['fragment']);
}
$excerpt['element'] = $medium->parseDownElement($title, $alt, $class);
$excerpt['element'] = $medium->parseDownElement($title, $alt, $class, true);
} else {
// not a current page media file, see if it needs converting to relative

View File

@ -1,6 +1,7 @@
<?php
namespace Grav\Common\Page\Medium;
use Grav\Common\Utils;
use Grav\Common\Data\Blueprint;
class ImageMedium extends Medium
@ -136,7 +137,15 @@ class ImageMedium extends Medium
*/
public function url($reset = true)
{
$output = preg_replace('|^' . preg_quote(GRAV_ROOT) . '|', '', $this->saveImage());
$pages_dir = self::$grav['locator']->findResource('page://');
$image_path = self::$grav['locator']->findResource('cache://images', true);
$image_dir = self::$grav['locator']->findResource('cache://images', false);
$output = preg_replace('|^' . preg_quote($pages_dir) . '|', '', $this->saveImage());
if (Utils::startsWith($output, $image_path)) {
$output = '/' . $image_dir . preg_replace('|^' . preg_quote($image_path) . '|', '', $output);
}
if ($reset) {
$this->reset();
@ -155,6 +164,7 @@ class ImageMedium extends Medium
if (!$this->image) {
$this->image();
}
return $this;
}
@ -261,6 +271,7 @@ class ImageMedium extends Medium
if ($this->image) {
$this->image();
$this->image->clearOperations(); // Clear previously applied operations
$this->querystring('');
$this->filter();
}

View File

@ -155,7 +155,7 @@ class Medium extends Data implements RenderableInterface
*/
public function querystring($querystring = null, $withQuestionmark = true)
{
if ($querystring) {
if (!is_null($querystring)) {
$this->set('querystring', ltrim($querystring, '?&'));
foreach ($this->alternatives as $alt) {

View File

Before

Width:  |  Height:  |  Size: 153 KiB

After

Width:  |  Height:  |  Size: 153 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 KiB

View File

@ -71,19 +71,63 @@ class MarkdownTest extends \Codeception\TestCase\Test
{
}
public function testSlugRelativeImages()
public function testImages()
{
$this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
// * up and down with anchor ![](../gallery/preset-8.jpg?cropResize=200,200)
// * up more and down more ![](../../blog/focus-and-blur/unslpash_yair_hazout.jpg?cropResize=200,200)
// * just down ![](item2-1/image3.jpg?cropResize=200,200)
// * down more ![](item2-1/item2-1-2/image2.jpg?cropResize=200,200)
$this->assertSame('<p><img src="/02.item2/02.item2-2/sample-image.jpg" /></p>',
$this->parsedown->text('![](sample-image.jpg)'));
$this->assertRegexp('|<p><img src="\/images\/.*-cache-image.jpe?g\?foo=1" \/><\/p>|',
$this->parsedown->text('![](cache-image.jpg?cropResize=200,200&foo)'));
$this->assertRegexp('|<p><img src="\/images\/.*-home-cache-image.jpe?g" \/><\/p>|',
$this->parsedown->text('![](/home-cache-image.jpg?cache)'));
}
public function testImagesSubDir()
{
$this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
$this->assertSame('<p><img src="/subdir/02.item2/02.item2-2/sample-image.jpg" /></p>',
$this->parsedown->text('![](sample-image.jpg)'));
$this->assertRegexp('|<p><img src="\/subdir\/images\/.*-cache-image.jpe?g" \/><\/p>|',
$this->parsedown->text('![](cache-image.jpg?cache)'));
// $this->assertRegexp('|<p><img src="\/subdir\/images\/.*-home-cache-image.jpe?g" \/><\/p>|',
// $this->parsedown->text('![](/home-cache-image.jpg?cache)'));
}
public function testImagesAbsoluteUrls()
{
$this->config->set('system.absolute_urls', true);
$this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
// $this->assertSame('<p><img src="http://testing.dev/images/02.item2/02.item2-2/sample-image.jpg" /></p>',
// $this->parsedown->text('![](sample-image.jpg)'));
$this->assertRegexp('|<p><img src="http:\/\/testing.dev\/images\/.*-cache-image.jpe?g" \/><\/p>|',
$this->parsedown->text('![](cache-image.jpg?cache)'));
$this->assertRegexp('|<p><img src="http:\/\/testing.dev\/images\/.*-home-cache-image.jpe?g" \/><\/p>|',
$this->parsedown->text('![](/home-cache-image.jpg?cache)'));
}
public function testImagesSubDirAbsoluteUrls()
{
$this->config->set('system.absolute_urls', true);
$this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
// $this->assertSame('<p><img src="http://testing.dev/subdir/02.item2/02.item2-2/sample-image.jpg" /></p>',
// $this->parsedown->text('![](sample-image.jpg)'));
// $this->assertRegexp('|<p><img src="http:\/\/testing.dev\/subdir\/images\/.*-sample-image.jpe?g" \/><\/p>|',
// $this->parsedown->text('![](sample-image.jpg?cache)'));
// $this->assertRegexp('|<p><img src="http:\/\/testing.dev\/subdir\/images\/.*-home-image.jpe?g" \/><\/p>|',
// $this->parsedown->text('![](/home-image.jpg?cache)'));
$this->assertSame('<p><a href="http://www.cnn.com">cnn.com</a></p>',
$this->parsedown->text('[cnn.com](http://www.cnn.com)'));
$this->assertSame('<p><a href="https://www.google.com">google.com</a></p>',
$this->parsedown->text('[google.com](https://www.google.com)'));
}