Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dizzystuff committed Jan 6, 2024
1 parent f968f06 commit d46abcf
Show file tree
Hide file tree
Showing 7 changed files with 561 additions and 0 deletions.
53 changes: 53 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
Name: fromholdio-svgenius
---

SilverStripe\Assets\File:
extensions:
- Fromholdio\SVGenius\Extensions\SVGFileExtension
allowed_extensions:
- svg
app_categories:
image:
- svg
image/supported:
- svg
file_types:
svg: 'Scalable Vector Graphic (SVG)'
class_for_file_extension:
svg: 'Fromholdio\SVGenius\Model\SVGImage'

SilverStripe\Admin\LeftAndMain:
extra_requirements_css:
- 'fromholdio/silverstripe-svgenius: client/css/svgenius.css'

---
Name: fromholdio-svgenius-imageformfactory
After: focuspoint
Only:
moduleexists: jonom/focuspoint
---
SilverStripe\AssetAdmin\Forms\ImageFormFactory:
extensions:
- Fromholdio\SVGenius\Extensions\SVGImageFormFactoryExtension

---
Name: fromholdio-svgenius-model
After: '#assetsfieldtypes'
---
SilverStripe\Assets\Storage\DBFile:
supported_images:
- 'image/svg+xml'
- 'image/svg'

---
Name: fromholdio-svgenius-mimevalidator
After: '#mimevalidator'
Only:
moduleexists: silverstripe/mimevalidator
---
SilverStripe\MimeValidator\MimeUploadValidator:
MimeTypes:
svg:
- 'image/svg+xml'
- 'image/svg'
3 changes: 3 additions & 0 deletions client/css/svgenius.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.gallery-item__thumbnail[style*=".svg"] {
background-size: contain;
}
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "fromholdio/silverstripe-svgenius",
"type": "silverstripe-vendormodule",
"description": "First-class SVG handling for Silverstripe CMS & front-end",
"license": "BSD-3-Clause",
"keywords": ["silverstripe", "assets", "svg"],
"homepage": "https://github.com/fromholdio/silverstripe-svgenius",
"support": {
"issues": "https://github.com/fromholdio/silverstripe-svgenius/issues"
},
"authors": [{
"name": "Luke Fromhold",
"homepage": "https://fromhold.io"
}],
"require": {
"silverstripe/framework": "^5.0.0",
"enshrined/svg-sanitize": "^0.16.0",
"meyfa/php-svg": "^0.14.5"
},
"autoload": {
"psr-4": {
"Fromholdio\\SVGenius\\": "src/"
}
},
"extra": {
"expose": [
"client"
]
},
"prefer-stable": true,
"minimum-stability": "dev"
}
18 changes: 18 additions & 0 deletions src/Extensions/SVGFileExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Fromholdio\SVGenius\Extensions;

use SilverStripe\ORM\DataExtension;

class SVGFileExtension extends DataExtension
{
public function IsSVG(): bool
{
return $this->getOwner()->getIsSVG();
}

public function getIsSVG(): bool
{
return $this->getOwner()->getExtension() === 'svg';
}
}
20 changes: 20 additions & 0 deletions src/Extensions/SVGImageFormFactoryExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Fromholdio\SVGenius\Extensions;

use SilverStripe\Core\Extension;
use SilverStripe\Forms\FieldList;

class SVGImageFormFactoryExtension extends Extension
{
public function updateFormFields(FieldList $fields, $controller, $formName, $context)
{
$image = $context['Record'] ?? null;
if ($image?->getIsSVG()) {
$fields->removeByName([
'FocusPoint',
'FocusPointTab'
]);
}
}
}
67 changes: 67 additions & 0 deletions src/Model/SVGImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Fromholdio\SVGenius\Model;

use Fromholdio\SVGenius\SVGeniusTrait;
use SilverStripe\Assets\Image;
use SilverStripe\Assets\Storage\AssetContainer;
use SVG\SVG as SVGParser;

class SVGImage extends Image
{
use SVGeniusTrait;

private static $table_name = 'SVGImage';
private static $singular_name = 'SVG image';
private static $plural_name = 'SVG images';

public function __construct($record = null, $isSingleton = false, $queryParams = [])
{
parent::__construct($record, $isSingleton, $queryParams);
if ($this->File->exists()) {
$this->initSVGFromString($this->File->getString(), false);
}
}

public function onBeforeWrite(): void
{
$sanitisedString = $this->sanitiseSVGString($this->File->getString());
$this->File->setFromString(
$sanitisedString,
$this->File->getFilename()
);
$this->svg = SVGParser::fromString($sanitisedString);
parent::onBeforeWrite();
}


public function getImageTag(): string
{
return parent::getTag();
}

public function Thumbnail($width, $height): AssetContainer
{
return $this->resizeByRatio($width, $height);
}

public function manipulate($variant, $callback): AssetContainer
{
return $this;
}

public function existingOnly(): AssetContainer
{
return $this->setAllowGeneration(false);
}

public function Resampled(): AssetContainer
{
return $this;
}

public function Quality($quality): AssetContainer
{
return $this;
}
}
Loading

0 comments on commit d46abcf

Please sign in to comment.