Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
markgoodyear committed Mar 8, 2015
0 parents commit b2dfbee
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## OS X
.DS_Store
.AppleDouble
.LSOverride
Icon
._*
.Spotlight-V100
.Trashes

## Windows
Thumbs.db
ehthumbs.db
Desktop.ini
$RECYCLE.BIN/

# Web/PHPstorm
.idea
7 changes: 7 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2015 Mark Goodyear.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Retinafy plugin for Craft.

Retinafy will only work on images with `@2x` in the filename. The `@2x` signifies its intended use.

By default, Retinafy generates the 1x version of the 2x version uploaded. The 1x version is used as the image tags `src` attribute, and the uploaded version is the `srcset` 2x version. For example, if your image is to be displayed at 250x250px, upload a 500x500px version, and Retinafy will handle the rest.

**Example output**:

```html
<img src="[generated_image_at_250x250].png"
srcset="[uploaded_image_at_500x500].png 2x">
```

## Usage

### As a Craft variable
Retinafy can be used as a Craft variable:

```twig
<img src="{{ craft.retinafy.image(assetFieldName) }}">
```

### As a Twig filter
Retinafy can also be used as a Twig filter:
```twig
<img src="{{ assetFieldName | retinafy }}">
```
## Options
Retinafy allows passing in a custom image transform:

```twig
{# As a Craft variable #}
<img src="{{ craft.retinafy.image(assetFieldName, 'transformHandle') }}">
{# As a Twig filter #}
<img src="{{ assetFieldName | retinafy('transformHandle') }}">
```
When passing in a transform, Retinafy will use the generated image as the image tags `src`, and then determine if the uploaded image can be used as a 2x version. If the image is large enough, it will create the 2x version of the transform and add in the required `srcset` markup.

# License
Retinafy is licensed under the [MIT license](/LICENSE.md).
59 changes: 59 additions & 0 deletions retinafy/RetinafyPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
namespace Craft;

class RetinafyPlugin extends BasePlugin
{

/**
* Adds the Twig extension.
*
* @return \Twig_Extension
*/
public function addTwigExtension()
{
Craft::import('plugins.retinafy.twigextensions.RetinafyTwigExtension');

return new RetinafyTwigExtension();
}

/**
* Returns the name of the plugin.
*
* @return string
*/
public function getName()
{
return 'Retinafy';
}

/**
* Returns the plugin’s version number.
*
* @return string
*/
public function getVersion()
{
return '1.0.0';
}

/**
* Returns the plugin developer’s name.
*
* @return string
*/
public function getDeveloper()
{
return 'Mark Goodyear';
}

/**
* Returns the plugin developer’s URL.
*
* @return string
*/
public function getDeveloperUrl()
{
return 'http://markgoodyear.com';
}

}
107 changes: 107 additions & 0 deletions retinafy/services/Retinafy_RetinafyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
namespace Craft;

class Retinafy_RetinafyService extends BaseApplicationComponent
{

/**
* Determin which image to create.
*
* @param \Craft\AssetFileModel $image
* @param string $transformHandle
* @return void
*/
public function retinaService(AssetFileModel $image, $transformHandle)
{
// Return original image (with transform if specified) if not intended for @2x.
if (!$this->is2xFile($image))
{
return $this->generateMarkup($image->getUrl(isset($transformHandle) ? $transformHandle : false));
}

// If transform specified, create 2x of it.
if (isset($transformHandle))
{
$this->create2xImage($image, $transformHandle);

return;
}

// No transform handle. Uploaded image is the 2x version, so we create the 1x version.
$this->create1xImage($image);
}

/**
* Check if image has @2x suffix.
*
* @param \Craft\AssetFileModel $image
* @return bool
*/
protected function is2xFile(AssetFileModel $image)
{
return preg_match('/@2x\\.\\w+$/', $image->filename) === 1;
}

/**
* Create the 1x image.
*
* @param AssetFileModel $image
* @return void
*/
protected function create1xImage(AssetFileModel $image)
{
$markup = $image->getUrl($this->getTransform($image, true)) . '" srcset="' . $image->getUrl() . ' 2x"';

$this->generateMarkup($markup);
}

/**
* Create the 2x image.
*
* @param \Craft\AssetFileModel $image
* @param string $transformHandle
* @return void
*/
protected function create2xImage(AssetFileModel $image, $transformHandle)
{
$originalWidth = (int) $image->getWidth(false);
$transformWidth = (int) $image->getWidth($transformHandle);

// Markup for the specified transform.
$markup = $image->getUrl($transformHandle);

// If original width is bigger than the 2x size for the specified transform, add the srcset.
if ($originalWidth > $transformWidth * 2)
{
$markup .= '" srcset="' . $image->getUrl($this->getTransform($image)) . ' 2x';
}

$this->generateMarkup($markup);
}

/**
* Get transform.
* @param \Craft\AssetFileModel $image
* @param bool $is2x - If the image we pass in is the 2x version
* @return array
*/
protected function getTransform(AssetFileModel $image, $is2x = false)
{
return [
'mode' => 'fit',
'width' => $is2x ? round($image->width / 2) : $image->width * 2
];
}

/**
* Echo out the markup.
*
* @param string $markup
* @return void
*/
protected function generateMarkup($markup)
{
echo $markup;
}

}
43 changes: 43 additions & 0 deletions retinafy/twigextensions/RetinafyTwigExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace Craft;

Use Twig_Extension;
use Twig_SimpleFilter;

class RetinafyTwigExtension extends Twig_Extension {

/**
* Returns the name of the extension.
*
* @return string
*/
public function getName()
{
return 'Retinafy';
}

/**
* Returns a list of filters to add to the existing list.
*
* @return array
*/
public function getFilters()
{
return [
new Twig_SimpleFilter('retinafy', [ $this, 'retinafy' ])
];
}

/**
* Retinafy Twig filter.
*
* @param \Craft\AssetFileModel $image
* @param string $transformHandle
* @return void
*/
public function retinafy(AssetFileModel $image, $transformHandle = null)
{
craft()->retinafy_retinafy->retinaService($image, $transformHandle);
}

}
19 changes: 19 additions & 0 deletions retinafy/variables/RetinafyVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace Craft;

class RetinafyVariable
{

/**
* Echo out src and srcset markup.
*
* @param \Craft\AssetFileModel $image
* @param string $transformHandle
* @return void
*/
public function image(AssetFileModel $image, $transformHandle = null)
{
craft()->retinafy_retinafy->retinaService($image, $transformHandle);
}

}

0 comments on commit b2dfbee

Please sign in to comment.