Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AndiLeni committed Feb 5, 2023
1 parent 67beee1 commit 336fdb7
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 17 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog


## [1.0.0] - 05.02.2023

### Changed
- initial release
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Friends Of REDAXO
Copyright (c) 2023 Andreas Lenhardt

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
1 change: 0 additions & 1 deletion README.de.md

This file was deleted.

13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# rex_repo_template
REDAXO AddOn english readme
# media_negotiator

please use in your classes a namespace as
Addon für Redaxo welches dem Media Manager Addon einen Effekt für Content Negotiation hinzufügt. Siehe https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation

```
namespace FriendsOfRedaxo\[AddOnNameWithoutSpacesAndAsCamelCase]
```

in order to use non deprecated features please don't use PlugIns anymore
Anleitung:
Dem Media Profil den Effekt "Negotiate image format" hinzufügen.
Danach wird automatisch eines der folgenden Formate ausgeliefert: avif, webp, jpg (in dieser Reihenfolge).
43 changes: 42 additions & 1 deletion boot.php
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
<?php // Boot code
<?php

function negotiateFormat($ep)
{

// rex_media_manager object
$subject = $ep->getSubject();

// check if the requested image has the 'negotiator' effect
$set_effects = $subject->effectsFromType($subject->getMediaType());
$set_effects = array_column($set_effects, 'effect');
$type = $subject->getMediaType();

// if not, skip
if (!in_array('negotiator', $set_effects)) {
return $subject;
}

// if yes, set cache path
if (in_array($type, ['avif'])) {
$possible_types = rex_server('HTTP_ACCEPT', 'string', '');
$types = explode(',', $possible_types);


if (in_array('image/avif', $types)) {
$subject->setCachePath($subject->getCachePath() . 'avif-');
} elseif (in_array('image/webp', $types)) {
$subject->setCachePath($subject->getCachePath() . 'webp-');
} else {
$subject->setCachePath($subject->getCachePath() . 'jpg-');
}
}

return $subject;
}


rex_extension::register('MEDIA_MANAGER_BEFORE_SEND', "negotiateFormat");

if (rex_addon::get('media_manager')->isAvailable()) {
rex_media_manager::addEffect(rex_effect_negotiator::class);
}
34 changes: 34 additions & 0 deletions lib/rex_effect_negotiator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

class rex_effect_negotiator extends rex_effect_abstract
{

public function getName()
{
return "Negotiate image format";
}

public function execute()
{
// get image mime types which are accepted by the requesting browser
$possible_types = rex_server('HTTP_ACCEPT', 'string', '');
$types = explode(',', $possible_types);

// instantiate image formatter class from media manager
$re = new rex_effect_image_format();
// pass current image to formatter
$re->media = $this->media;

// set convert_to extension
if (in_array('image/avif', $types)) {
$re->params['convert_to'] = 'avif';
} elseif (in_array('image/webp', $types)) {
$re->params['convert_to'] = 'webp';
} else {
$re->params['convert_to'] = 'jpg';
}

// change format
$re->execute();
}
}
13 changes: 7 additions & 6 deletions package.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package: addon_key
version: '0.0.0'
author: 'Friends Of REDAXO'
supportpage: https://github.com/FriendsOfREDAXO/addon_key
package: media_negotiator
version: 1.0.0
name: Media Negotiator
author: Andreas Lenhardt

requires:
redaxo: '^5.13.0'
redaxo: ^5.13.0
php:
version: '>=7.3, <9'
version: "^8.0"

0 comments on commit 336fdb7

Please sign in to comment.