Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Context, Clean ups and tests #9

Merged
merged 11 commits into from
Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
18 changes: 11 additions & 7 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/.editorconfig export-ignore
/.php_cs export-ignore
/.github export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
/psalm.xml.dist export-ignore
/tests export-ignore
/.editorconfig export-ignore
/.php-cs-fixer.dist.php export-ignore
/art export-ignore
/docs export-ignore
/UPGRADING.md export-ignore
14 changes: 14 additions & 0 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
blank_issues_enabled: false
contact_links:
- name: Ask a question
url: https:/:vendor_name/:package_name/discussions/new?category=q-a
about: Ask the community for help
- name: Request a feature
url: https:/:vendor_name/:package_name/discussions/new?category=ideas
about: Share ideas for new features
- name: Report a security issue
url: https:/:vendor_name/:package_name/security/policy
about: Learn how to notify us for sensitive bugs
- name: Report a bug
url: https:/:vendor_name/:package_name/issues/new
about: Report a reproducable bug
1 change: 0 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

version: 2
updates:

- package-ecosystem: "github-actions"
directory: "/"
schedule:
Expand Down
51 changes: 25 additions & 26 deletions .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,30 @@ name: dependabot-auto-merge
on: pull_request_target

permissions:
pull-requests: write
contents: write
pull-requests: write
contents: write

jobs:
dependabot:
runs-on: ubuntu-latest
if: ${{ github.actor == 'dependabot[bot]' }}
steps:

- name: Dependabot metadata
id: metadata
uses: dependabot/[email protected]
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

- name: Auto-merge Dependabot PRs for semver-minor updates
if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}}
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

- name: Auto-merge Dependabot PRs for semver-patch updates
if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}}
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
dependabot:
runs-on: ubuntu-latest
if: ${{ github.actor == 'dependabot[bot]' }}
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/[email protected]
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

- name: Auto-merge Dependabot PRs for semver-minor updates
if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}}
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

- name: Auto-merge Dependabot PRs for semver-patch updates
if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}}
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.idea
.php_cs
.php_cs.cache
Expand All @@ -10,5 +11,3 @@ phpunit.xml
psalm.xml
vendor
.php-cs-fixer.cache
.DS-Store
.DS_Store
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
],
'single_trait_insert_per_statement' => true,
])
->setFinder($finder);
->setFinder($finder);
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ All notable changes to `conlect\image-iiif` will be documented in this file

## 0.2.0 - 2018-11-18

- stable pre-release allows passing config as array
- stable pre-release allows passing config as array

## 1.0.0 -
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) Conlectio LLC
Copyright (c) jeremytubbs

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
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
[![Tests](https:/conlect/image-iiif/actions/workflows/run-tests.yml/badge.svg?branch=main)](https:/conlect/image-iiif/actions/workflows/run-tests.yml)
[![Total Downloads](https://img.shields.io/packagist/dt/conlect/image-iiif.svg?style=flat-square)](https://packagist.org/packages/conlect/image-iiif)


# Image IIIF

This package implements the [IIIF Image API 3.0](https://iiif.io/api/image/3.0/), it is unopinionated about implementation and many of the `MUST` features are not included because it does not include an actual implementation only the means to create one. I consider it a bring your own framework solution for implementing Image API 3.0 with PHP. The package utilizes the [Intervention Image](http://image.intervention.io/) package for manipulations. I have provided Intervention filters for each of the 5 IIIF parameters that can be used independently of the `$factory()->load()->withParameters()` pipeline methods.

**Supports all Image Request Parameters:**
- Region (full || square || x,y,w,h || pct:x,y,w,h)
- Size (full || max || w, || ,h || pct:n || w,h || !w,h)
- Rotation (n || !n)
- Quality (color || gray || bitonal || default)
- Format (jpg || tif || png || gif || webp)

Supports the `info.json` response for an identifier.
- Region (full || square || x,y,w,h || pct:x,y,w,h)
- Size (full || max || w, || ,h || pct:n || w,h || !w,h)
- Rotation (n || !n)
- Quality (color || gray || default)
- Format (jpg || tif || png || gif || webp)

Supports the `info.json` response for an identifier.

#### Laravel image route example:

Expand Down
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
"authors": [
{
"name": "Jeremy Tubbs",
"email": "[email protected]",
"homepage": "https://conlect.io",
"email": "[email protected]",
"homepage": "https://jeremytubbs.com",
"role": "Developer"
}
],
"require": {
"php": ">=5.5.9",
"hassankhan/config": "^2.1",
"intervention/image": "^2.5"
"php": "^8.0.2",
"hassankhan/config": "^3.0.1",
"intervention/image": "^2.7.2"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"larapack/dd": "^1.1",
"phpunit/phpunit": "^9.3"
"phpunit/phpunit": "^9.5"
},
"suggest": {
"ext-gd": "to use GD library based image processing.",
Expand All @@ -48,7 +48,6 @@
"vendor/bin/phpunit --coverage-html coverage"
],
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes"

},
"config": {
"sort-packages": true
Expand Down
15 changes: 9 additions & 6 deletions config/iiif.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

return [

'base_url' => 'https://example.test',

'driver' => 'gd', // or imagick

'allow_upscaling' => true,

'tile_width' => 1024,

'quality' => 90,
Expand All @@ -13,8 +17,8 @@
'tif' => 'image/tiff',
'png' => 'image/png',
'gif' => 'image/gif',
'jp2' => 'image/jp2',
'pdf' => 'application/pdf',
// 'jp2' => 'image/jp2',
// 'pdf' => 'application/pdf',
'webp' => 'image/webp',
],

Expand All @@ -25,13 +29,12 @@
'qualities' => [
'color',
'gray',
'bitonal',
// 'bitonal',
'default',
],

'supports' => [
'baseUriRedirect', // The base URI of the service will redirect to the image information document.
'canonicalLinkHeader', //
'cors',
'baseUriRedirect',
"jsonldMediaType",
],
];
5 changes: 4 additions & 1 deletion src/ImageIIIF.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ public function hasValidParameters(array $parameters)

public function info($prefix = 'iiif', $identifier)
{
// Optional - maxWidth, maxHeight, maxArea
// sizes - prefered w,h pairs
// rights - CC license
return [
'@context' => 'http://iiif.io/api/image/2/context.json',
'@context' => 'http://iiif.io/api/image/3/context.json',
'id' => $this->config['base_url'] . '/' . $prefix . '/' . $identifier,
'type' => 'ImageService3',
'protocol' => 'http://iiif.io/api/image',
Expand Down
32 changes: 25 additions & 7 deletions src/Validators/RegionValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

namespace Conlect\ImageIIIF\Validators;

use function array_filter;

use Conlect\ImageIIIF\Exceptions\BadRequestException;
use Conlect\ImageIIIF\Validators\Contracts\ValidatorInterface;

use function count;
use function explode;
use function in_array;
use function str_starts_with;

class RegionValidator extends ValidatorAbstract implements ValidatorInterface
{
public function valid($value)
{
$options = explode(',', $value);

if (in_array($options[0], ['full', 'square'])) {
return true;
}

if (str_contains($options[0], ':') && ! str_starts_with($options[0], 'pct:')) {
return $this->valueException($value);
if (count($options) == 1) {
return $this->validateFullOrSquare($options[0]);
}

if (count($options) !== 4) {
Expand All @@ -28,10 +31,25 @@ public function valid($value)
}

if (str_starts_with($options[0], 'pct:')) {
$options[0] = substr($options[0], 4);
$options[0] = \substr($options[0], 4);
}

if (4 === count(array_filter($options, 'is_numeric'))) {
$validator = new ValidatorShared();
foreach ($options as $option) {
$validator->floatingPointValidator($option);
}

return true;
}


return $this->valueException($value);
}

protected function validateFullOrSquare($value)
{
if (in_array($value, ['full', 'square'])) {
return true;
}

Expand Down
13 changes: 12 additions & 1 deletion src/Validators/SizeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Conlect\ImageIIIF\Validators;

use Conlect\ImageIIIF\Exceptions\BadRequestException;
use Conlect\ImageIIIF\Exceptions\NotImplementedException;
use Conlect\ImageIIIF\Validators\Contracts\ValidatorInterface;

class SizeValidator extends ValidatorAbstract implements ValidatorInterface
Expand All @@ -21,17 +22,27 @@ public function valid($value)
{
$startValue = $value;

if (str_starts_with($value, '^') && $this->config['allow_upscaling'] === false) {
throw new NotImplementedException("Upscaling is not allowed.");
}

if (strpos($value, '^pct:') !== false) {
$value = preg_replace('/^\^pct:/', '', $value);

if ((int)$value == 0) {
throw new BadRequestException("Size $startValue is invalid.");
}
if ((int)$value >= 1) {
return true;
}
}

if (strpos($value, 'pct:') !== false) {
$value = preg_replace('/^pct:/', '', $value);
$value = preg_replace('/pct:/', '', $value);

if ((int)$value == 0 || (int)$value > 100) {
throw new BadRequestException("Size $startValue is invalid.");
}
if ((int)$value >= 1 && (int)$value <= 100) {
return true;
}
Expand Down
24 changes: 24 additions & 0 deletions src/Validators/ValidatorShared.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Conlect\ImageIIIF\Validators;

use Conlect\ImageIIIF\Exceptions\BadRequestException;

class ValidatorShared
{
public function floatingPointValidator($value)
{
if (is_float($value + 0)) {
// if less than zero and doesn't start with a zero
if ($value + 0 < 1 && ! str_starts_with($value, '0')) {
throw new BadRequestException('Region values less than one require a leading zero.');
}
// option should not have an extra trailing zero
if (str_ends_with($value, '0')) {
throw new BadRequestException('Region values should not contain a trailing zero.');
}
}

return true;
}
}
2 changes: 0 additions & 2 deletions tests/FormatValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public function it_validates_format()
$this->assertTrue($formatValidator->valid('tif'));
$this->assertTrue($formatValidator->valid('png'));
$this->assertTrue($formatValidator->valid('gif'));
$this->assertTrue($formatValidator->valid('jp2'));
$this->assertTrue($formatValidator->valid('pdf'));
$this->assertTrue($formatValidator->valid('webp'));
}

Expand Down
Loading