Coder Social home page Coder Social logo

images's Introduction

Build Status

Deprecated

Tento balíček se už dále vyvíjet nebude, budou maximálně opravy chyb. Použijte proto velmi podobný balíček https://github.com/contributte/imagist, který je bohatější.

Instalace

Composer:

composer require webchemistry/images

config:

extensions:
    images: WebChemistry\Images\DI\ImagesExtension

Konfigurace

enable: yes
defaultImage: null
wwwDir: %wwwDir%
assetsDir: assets
modifiers: []
aliases: []
hashResolver: WebChemistry\Images\Resolvers\HashResolver ## Vysvětleno níže
namespaceResolver: WebChemistry\Images\Resolvers\NamespaceResolver ## Vysvětleno níže
registerControl: yes ## Zaregistruje UploadControl
registerType: yes ## Zaregistruje doctrine typ 'image' 
safeLink: %productionMode% ## metoda link() se zotavuje z chyb a loguje tyto chyby do tracy, web nespadne do 500 kvůli chybnému obrázku

Skladba cesty k obrázků

%wwwDir%/%assetsDir%/namespace/resize/image.png %wwwDir%/%assetsDir%/namespace/original/image.png

namespace/ - Má na starosti třída namespaceResolver resize/ - Má na starosti třída hashResolver

Tvorba aliasů

Aliasy umožnují snadnou modifikací obrazků

Použití jednoho modifieru

local:
    aliases:
      myAlias: "resize:12,50"

Více modifierů

local:
    aliases:
      myAlias: "resize:12,50,exact|sharpen"

Použití polí

cloudinary:
    aliases:
      myAlias: "border:[width: 4, color: #553311]"

Použití proměnných

local:
    aliases:
      resizeExact: "resize:$1,$2,exact"
      resize: "resize:$1,$2,$3"
      resizeSquare: "resize:$1,$1,exact"

Vlastní modifiery

V konfiguraci stačí zaregistrovat loader

local:
    modifiers:
      - ModifiersLoader

vytvořit třídu a přidávat modifiery

class ModifiersLoader implements WebChemistry\Images\Modifiers\ILoader {
    
    public function load(WebChemistry\Images\Modifiers\ModifierContainer $modifierContainer) {
        $modifierContainer->addModifier('custom', function (ModifierParam $param, $foo) {
            // zpracovani obrazku $param->getImage()
        });
    }

}

a použití

local:
    aliases:
      custom: "custom:param1"

Ukladaní obrázků

$upload - Instance Nette\Utils\Upload $location - Cesta obrázku uložená v řetězci $storage - Instance WebChemistry\Images\IImageStorage

Nette upload

// vytvorime zdroj pro obrazek
$resource = $storage->createUploadResource($upload);
// nebo z cesty
$resource = $storage->createLocalResource($location);

// pridame namespace
$resource->setNamespace('namespace');

// ulozime
$result = $storage->save($resource);

// zobrazime url adresu
echo $storage->link($result);

Před nahráním obrázku ho můžeme upravit

$resource->setAlias("custom");

// Kombinace více aliasů
$resource->setAliases(["custom", "custom2"]);

$id = $resource->getId(); // Ziskání id
// nebo
$id = (string) $resource;

Obrázek se uloží v namespace/original/obrazek.jpg

Získávání obrázků

$id Identifikátor ziskány z uloženeho obrázku viz sekce ukládání obrázků

$resource = $storage->createResource($id);

$link = $storage->link($resource);

Kopírování obrázků

$id Identifikátor ziskány z uloženeho obrázku viz sekce ukládání obrázků

$resource = $storage->createResource($id);
$dest = $storage->createResource("namespace/obrazek.jpg"); 

// Muzeme zmodifikovat
$dest->setAlias("custom");

$storage->copy($resource, $dest);

Zkopíruje se jen originální obrázek a v případně se zmodifikuje.

Přesouvání obrázků

$id Identifikátor ziskány z uloženeho obrázku viz sekce ukládání obrázků

$resource = $storage->createResource($id);
$dest = $storage->createResource("namespace/obrazek.jpg"); 

// Muzeme zmodifikovat
$dest->setAlias("custom");

$storage->move($resource, $dest);

Odstranění obrázků

$id Identifikátor ziskány z uloženeho obrázku viz sekce ukládání obrázků

$resource = $storage->createResource($id);

$storage->delete($id);

Odstraní se jak originální obrázek, tak i jeho modifikace.

Modifikace obrázků

$id Identifikátor ziskány z uloženeho obrázku viz sekce ukládání obrázků

  1. Uložením
$resource = $storage->createResource($id);
$resource->setAlias("custom");
$storage->save($resource);

Uloží se do namespace/custom/obrazek.jpg

  1. Získáním adresy
$resource = $storage->createResource($id);
$resource->setAlias("custom");
echo $storage->link($resource);

Uloží se do namespace/custom/obrazek.jpg

Šablony

Zobrazení obrázku

{img 'image.jpg'}
<img n:img="'image.jpg'">

Zobrazení s použitím modifikátorů obrázků

{img 'image.jpg', custom}
<img n:img="'image.jpg', custom">

{* Kombinace dvou aliasů *}
{img image.jpg, custom, custom1}

{* Použití proměnných v aliasu *}
{img $resource, customVariables(15,15,exact)}

Dávkování obrázků

$batch = $storage->createBatch();

$entity->image = $batch->save($resource);
$this->em->persist($entity);

$entity2->image = $batch->save($resource2);
$this->em->persist($entity2);

$batch->flush();
$this->em->flush();

Formuláře

Automatickou registraci provede extenze. S touto komponentou odpadá povinnost vytvoření třídy pro obrázek.

$form->addImageUpload('image', 'Obrazek')
    ->setRequired()
    ->setNamespace('namespace');

$form->onSuccess[] = function ($form, array $values) use ($storage) {
    $storage->save($values['image']);
};    

Pro náhledový obrázek a input pro odstranění obrázků:

$form->addImageUpload('image', 'Obrázek')
    ->setDelete('Odstranit obrázek')
    ->setNamespace('namespace');
    
$form->onSuccess[] = function ($form, array $values) use ($storage) {
	$image = $values['image'];
	if ($image->getDelete()) {
		$storage->delete($image->getDelete());
	}
	if ($image->getUpload()) {
    	$resource = $storage->save($image->getUpload());
	} else {
		$resource = $image->getDefaultValue();
	}
};

Doctrine typ

Automatickou registraci provede extenze. Položku pro obrázek lze vytvořit přes anotaci typ image:

class Entity {
    
    /**
     * @ORM\Column(type="image")
     */
    protected $image;

}

nullable=true změna obrázku z povinného na nepovinný

Uložení nového obrázku, bere jen instaci IFileStorage nebo NULL v případě nastaveného nullable v anotaci Column

$form->onSuccess[] = function ($form, $values) {
    $en = new Entity();
    $en->image = $this->storage->save($values->image);
    
    $this->em->persist($en);
    $this->em->flush();
};

Získání obrázku

$en = $this->em->getRepository(Entity::class)->find(1);
if ($en->image !== NULL) { // V pripade nullable
    $link = $this->storage->link($en->image);
}

images's People

Contributors

cactucs avatar chapcz avatar elendirx avatar ivorius avatar janmikes avatar krekos avatar martenb avatar martkcz avatar paveljurasek avatar tg666 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

images's Issues

How to save stream

Hi,

in some previous version was possible to use following code to store image from external source.

How can I achieve it with the current version, please?

$this->imageStorage->saveContent(file_get_contents('http://img.youtube.com/vi/' . $id . '/0.jpg'), $id.'.jpg', 'video');

Image modification in latte

Hi,
I was unable to find a way to modify image in-place in latte template.
In 2.x I was able to write {img $path, '200x', 'exact'}. Is there a way to do the same in 3.x?

Update documentation

Info about how to change checkbox's label "Delete this image?" would be nice.

This is what i tried and didnt work for me:

$image = $form->addImageUpload("image", "image")
            ->setDefaultValue($entity->getImage());

        $checkbox = $image->getCheckbox();
        $checkbox::$labelContent = "test";

If i do this: Checkbox::$labelContent = "test"; before adding input, it works. Though what is the best place to globally change this value for all inputs, bootstrap.php?

DigitalOcean Spaces with CDN

Hello,
I would like to use it in my projects with DO Spaces (AWS S3 compatible). With standard origin endpoint is is perfectly working, but i need to get it works with CDN endpoint. Is here any way to transform url of getting image? Something from xxx.ams3.digxxx to xxx.ams3.cdn.digixxx?

PS: It is possible to set root (or default namespace prefix)?

Thanks.

fixOrientation modifier on FileResource

Hello,
I need fixOrientation modifier on FileResource, but it is only for ITransferResource.

I tried to create custom ModifiersLoader, but i don't know, how to get IImageStorage to get path to file for exif_read_data... I tried to inject it, but it fails with

Circular reference detected for services: images.template.facade, images.storage, images.resourceMetaFactory, images.modifiers.

Thx for help...

When using multiple uploadcontrols in form, only last one is processed

Hello, i need to use multiple uploadcontrols in one form (not possible to use multiupload, i need to differ images), but only last one is processed.

Following code:

$form->addImageUpload("test1", "test1")
    ->setRequired();

$form->addImageUpload("test2", "test2")
    ->setRequired();

$form->addImageUpload("test3", "test3")
    ->setRequired();

See result of xdebug breakpoint in process function (without modifing values), tried same image 3 times. I tested even with 2 or more images, same result, always only last one is uploaded with imagestorage.

Tried both stable and @dev version, same result.

A mistake in docu

There is a mistake in the documentation.

extensions:
    images: WebChemistry\Images\DI\ImagesExtension

Correct is Extension, because in the file you have got these lines:

<?php
namespace WebChemistry\Images\DI;
use Nette;
use WebChemistry\Images\ImageStorageException;
class Extension extends Nette\DI\CompilerExtension {

nette/forms v3.0.3 compatibility

With the latest version of nette/forms (v3.0.3) is not possible use \WebChemistry\Images\Controls\AdvancedUploadControl. It throws error: Call to undefined method WebChemistry\Images\Resources\StateResource::getSize() because of this change: nette/forms@69fb18e (automatically sets rule :fileSize according PHP setting).

How can I help with fix this?

$form->setDefaults(...) from presenter action does not work on image upload

Working example:

$form>addImageUpload("image", "Image")
    ->setDefaultValue("xxx.jpg");

Example that does not work:

// MyPresenter.php
public function actionEdit()
{
    $this["myForm"]->setDefaults([
        "image" => "xxx.jpg",
    ]);
}

I am not sure if it is intended to not work (unable to make it work because of $form->setDefaults() implementation or it is just a bug.

Keep image quality

Is there any way, to keep image quality and ignore Image compression?

Mkdir fails silently

Mkdir fails silently when creating nested directories with PHP7 on Ubuntu 14.

How to get width and height of the resource?

Hi,

is there any way of how to get image size (width and height) from the resource?

This is what I was using previously:

$bigImage->getNetteImageClass()->getWidth();

Bellow you can find a snippet of my current code:

$resource = $this->imageStorage->createResource('news/' . $dbImage->id . "-" . $dbImage->name);
$bigImage = $this->imageStorage->link($resource);

// how can I get width and height of the $bigImage here?

Cheers

Aktualizace pro Nette 4?

Ahoj, našel by se někdo šikovný, kdo by tento jinak naprosto skvělý doplněk pro práci s obrázky přepsal pro Nette4?

Uložení obrázku v console command

Ahoj, zkouším uložit obrázky z XML feedu přes commannd z console

$resource = $this->imageStorage->createLocalResource($imagePath);
$resource->setNamespace($namespace);
$imagePath = $this->imageStorage->save($resource);

vše proběhne bez chyb ale obrázek se fyzicky neuloží (nevyhodí to ani žádnou chybu)
jediné co jsem pak našel v logu že není oprávnění. Pokud ukládám soubor přes formulář tak vše funguje jak má. Je možné nějak nastavit jinou konfiguraci pro konzoli?

výpis z konzole

[2019-04-10 17-54-46] PHP Warning: mkdir(): Permission denied in /var/www/clients/client2/web29/web/vendor/webchemistry/images/src/Storages/LocalStorage.php:275  @  CLI (PID: 23280): /var/www/clients/client2/web29/web/bin/console app:yourNewStyleImport -vvv 
[2019-04-10 17-54-46] PHP Warning: imagejpeg(www/images/assets/product/original/2b24ee1588836128c4f9ba9542b0f8ba.jpg): failed to open stream: Permission denied in /var/www/clients/client2/web29/web/vendor/nette/utils/src/Utils/Image.php:536  @  CLI (PID: 23280): /var/www/clients/client2/web29/web/bin/console app:yourNewStyleImport -vvv 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.