Skip to content

Commit

Permalink
Moved to Contributte
Browse files Browse the repository at this point in the history
  • Loading branch information
Vojta Mareš authored and Milan Felix Šulc committed Nov 1, 2018
1 parent e7a6a33 commit d200c1c
Show file tree
Hide file tree
Showing 25 changed files with 661 additions and 551 deletions.
156 changes: 156 additions & 0 deletions .docs/README.md
@@ -0,0 +1,156 @@
# Image Storage

Image storage for Nette framework

---

## Content

- [Usage - how to register & configure](#usage)
- [Images - how to work with them](#images)
- [Storing image](#storing-image)
- [Transforming image](#transforming-image)
- [Deleting image](#deleting-image)
- [Friendly URL](#friendly-url)

## Usage

Register extension:
```yml
extensions:
imageStorage: Contributte\ImageStorage\DI\ImageStorageExtension
```

Configure extension:
```yml
imageStorage:
data_path: %wwwDir%/../public/data # Filesystem location
data_dir: data # Relative path
algorithm_file: sha1_file # Algorithm to take image prefix directory from
algorithm_content: sha1 # ...
quality: 85 # Default wuality when cropping
default_transform: fit # Default crop transformation
noimage_identifier: noimage/03/no-image.png # No-image image
friendly_url: false # Create friendly URLs?
```

## Images

## Storing image

You are saving files within particular namespaces (eg 'avatars').
For better filesystem optimization, target files are saved in `<namespace>/<hash>/<file.ext>`,
where the hash is made from first 2 characters of sha1 hash (or some other configured hashing algorithm) of target file.
Therefore there won't be thousands of files in one directory,
but files will be distributed under that hash-named directories.


```php

<?php declare(strict_types = 1);

namespace Your\App\Presenters;

use Contributte\ImageStorage\ImageStoragePresenterTrait;;
use Nette\Application\UI\Presenter;

class ImageStoragePresenter extends Presenter
{

// Add $imageStorage to templates (in order to use macros)
use ImageStoragePresenterTrait;

public function createComponentUpload()
{
$form->addUpload('upload', '');
}

public function uploadSucceeded($form, $values)
{
// You can save image from upload
$image = $this->imageStorage->saveUpload($values->upload, 'images');
dump($image);

// Or directly image content
$image2 = $this->imageStorage->saveContent(
file_get_contents($values->upload->getTemporaryFile()),
'foobar.png',
'images'
);
}

}

```

## Transforming image - resizing, cropping

You simply pass a **size** parameter to either Latte macro or Latte n:macro or directly ImageStorage.
Or **crop** measures or **quality** or **transformation flag**.
Or some of these **combined**. You can also combine the transformation flags with `+` sign.

In model:
```php
<?php declare(strict_types = 1);

// Original
$img = $this->imageStorage->fromIdentifier('images/ed/kitty.jpg');
dump($img->getPath()); // System path to image file

// Resized etc
$img = $this->imageStorage->fromIdentifier(['images/ed/kitty.jpg', '20x20']);
```

In [Latte](https://latte.nette.org/) template:
```latte
{var $identifier = 'images/ed/kitty.jpg'}
{img $identifier}
```
## Deleting image

Once you want to delete an image,
you should delete all other transformed images made from the original one.

From string identifier:
```php
<?php declare(strict_types = 1);

$img = 'images/ed/kitty.jpg';
$this->imageStorage->delete($img);
```

From object:
```php
<?php declare(strict_types = 1);

$img_object = $imageStorage->fromIdentifier('images/ed/kitty.jpg');
$this->imageStorage->delete($img_object);
```

## Friendly URL

The transformed image name does not look to much friendly (eg `/avatars/kitty.200x200.fit.jpg`).
You can change the method of creating links to images in configuration file so the link will look `/avatars/200x200.fit/kitty.jpg` which is much more friendly when downloading the image.

If you don't want to make links to image in this format:
```html
<img src="/data/images/ed/kitty.100x200.exact.q85.jpg">
```

But like this:
```html
<img src="/data/images/ed/100x200.exact.q85/kitty.jpg?_image_storage">
```

1) Add a configuration to imageStorage extension in your config.neon:
```yml
imageStorage:
friendly_url: TRUE
```

2) Alter your `.htaccess` file:
```htaccess
# Images Storage conversion with directory suffix
RewriteCond %{QUERY_STRING} _image_storage
RewriteRule ^(\w+)/(\w+)/(\w+)/([^/]+)/(.+)\.(.+) $1/$2/$3/$5.$4.$6 [L]
```
20 changes: 20 additions & 0 deletions .editorconfig
@@ -0,0 +1,20 @@
# EditorConfig is awesome: http://EditorConfig.org

# Top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

# JS / PHP
[*.{js,php,phpt}]
charset = utf-8
indent_style = tab
indent_size = 4

# Composer
[{composer.json}]
indent_style = space
indent_size = 2
11 changes: 11 additions & 0 deletions .gitattributes
@@ -0,0 +1,11 @@
# Not archived
.docs export-ignore
tests export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
LICENSE export-ignore
README.md export-ignore
phpstan.neon export-ignore
ruleset.xml
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
/.idea
/vendor
/composer.lock
52 changes: 45 additions & 7 deletions .travis.yml
@@ -1,13 +1,51 @@
language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.1
- 7.2

before_install:
# turn off XDebug
- phpenv config-rm xdebug.ini || return 0

install:
- composer install --no-interaction --prefer-source
# Composer
- travis_retry composer install --no-progress --prefer-dist

script:
- vendor/bin/tester -p php tests/
# Nette/Tester
- composer run-script tester

jobs:
include:
- stage: Quality Assurance
php: 7.1
script:
- composer run-script qa

- stage: Test Coverage
php: 7.1
script:
- composer run-script coverage
after_script:
- wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar
- php coveralls.phar --verbose --config tests/.coveralls.yml

- stage: Phpstan
php: 7.1
script:
- composer run-script phpstan-install
- composer run-script phpstan

allow_failures:
- stage: Test Coverage
- stage: Phpstan

after_failure:
# Print *.actual content
- for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done

sudo: false

cache:
directories:
- $HOME/.composer/cache
6 changes: 3 additions & 3 deletions LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)
MIT License

Copyright (c) 2016
Copyright (c) 2018 Contributte

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 All @@ -18,4 +18,4 @@ 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.
SOFTWARE.
63 changes: 56 additions & 7 deletions README.md
@@ -1,10 +1,59 @@
[![Build Status](https://travis-ci.org/ublaboo/image-storage.svg?branch=master)](https://travis-ci.org/ublaboo/image-storage)
[![Latest Stable Version](https://poser.pugx.org/ublaboo/image-storage/v/stable)](https://packagist.org/packages/ublaboo/image-storage)
[![License](https://poser.pugx.org/ublaboo/image-storage/license)](https://packagist.org/packages/ublaboo/image-storage)
[![Total Downloads](https://poser.pugx.org/ublaboo/image-storage/downloads)](https://packagist.org/packages/ublaboo/image-storage)
[![Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/ublaboo/help)
# Image Storage

# image-storage
Image storage for Nette framework

Please see the documentation [here](https://ublaboo.org/image-storage/)
-----

[![Build Status](https://img.shields.io/travis/contributte/image-storage.svg?style=flat-square)](https://travis-ci.org/contributte/image-storage)
[![Code coverage](https://img.shields.io/coveralls/contributte/image-storage.svg?style=flat-square)](https://coveralls.io/r/contributte/image-storage)
[![Licence](https://img.shields.io/packagist/l/contributte/image-storage.svg?style=flat-square)](https://packagist.org/packages/contributte/image-storage)

[![Downloads this Month](https://img.shields.io/packagist/dm/contributte/image-storage.svg?style=flat-square)](https://packagist.org/packages/contributte/image-storage)
[![Downloads total](https://img.shields.io/packagist/dt/contributte/image-storage.svg?style=flat-square)](https://packagist.org/packages/contributte/image-storage)
[![Latest stable](https://img.shields.io/packagist/v/contributte/image-storage.svg?style=flat-square)](https://packagist.org/packages/contributte/image-storage)

## Discussion / Help

[![Join the chat](https://img.shields.io/gitter/room/contributte/contributte.svg?style=flat-square)](http://bit.ly/ctteg)

## Install

```sh
$ composer require contributte/image-storage
```
## Versions

| State | Version | Branch | PHP |
|---------|------------|----------|---------|
| dev | dev-master | `master` | `>=7.1` |
| stable | `^1.1.13` | `master` | `>=7.1` |

## Overview

- [Usage - how to register & configure](https://github.com/contributte/image-storage/tree/master/.docs#usage)
- [Images - how to work with them](https://github.com/contributte/image-storage/tree/master/.docs#image)
- [Storing image](https://github.com/contributte/image-storage/tree/master/.docs#storing-image)
- [Transforming image](https://github.com/contributte/image-storage/tree/master/.docs#transforming-image)
- [Deleting image](https://github.com/contributte/image-storage/tree/master/.docs#deleting-image)
- [Friendly URL](https://github.com/contributte/image-storage/tree/master/.docs#friendly-url)

## Maintainers

<table>
<tbody>
<tr>
<td align="center">
<a href="https://github.com/paveljanda">
<img width="150" height="150" src="https://avatars1.githubusercontent.com/u/1488874?s=150&v=3">
</a>
</br>
<a href="https://github.com/paveljanda">Pavel Janda</a>
</td>
</tr>
<tbody>
</table>

-----

Thank you for testing, reporting and contributing.

0 comments on commit d200c1c

Please sign in to comment.