-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7a6a33
commit d200c1c
Showing
25 changed files
with
661 additions
and
551 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/.idea | ||
/vendor | ||
/composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,59 @@ | ||
[](https://travis-ci.org/ublaboo/image-storage) | ||
[](https://packagist.org/packages/ublaboo/image-storage) | ||
[](https://packagist.org/packages/ublaboo/image-storage) | ||
[](https://packagist.org/packages/ublaboo/image-storage) | ||
[](https://gitter.im/ublaboo/help) | ||
# Image Storage | ||
|
||
# image-storage | ||
Image storage for Nette framework | ||
|
||
Please see the documentation [here](https://ublaboo.org/image-storage/) | ||
----- | ||
|
||
[](https://travis-ci.org/contributte/image-storage) | ||
[](https://coveralls.io/r/contributte/image-storage) | ||
[](https://packagist.org/packages/contributte/image-storage) | ||
|
||
[](https://packagist.org/packages/contributte/image-storage) | ||
[](https://packagist.org/packages/contributte/image-storage) | ||
[](https://packagist.org/packages/contributte/image-storage) | ||
|
||
## Discussion / Help | ||
|
||
[](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. | ||
|
Oops, something went wrong.