Gregwar / Image

A PHP library to handle images
MIT License
1k stars 190 forks source link

Applying same operation duplicates itself #126

Closed garygreen closed 7 years ago

garygreen commented 8 years ago

If you do $image->resize(100, 100) subsequently $image->resize(200, 200); it will apply both resize operations to the array.

My use case is I basically want to clone an existing image instance, and then apply a different resize (keeping all the existing operations and settings). If I do:

$image->negate()->resize(100, 100);
$new = clone $image;
$new->resize(200, 200);

The $new instance has both the resize operations for 100x100 and 200x200 when it should only have one, as the last resize() operation should have overridden it.

Also it duplicates the hash, so it doesn't properly detect to save a new image.

Gregwar commented 7 years ago

I think the behaviour is correct here Why don't you write:

<?php
$image->negate();
$new = clone $image;
$image->resize(100, 100);
$new->resize(200, 200);
garygreen commented 7 years ago

@Gregwar this isn't a problem limited to clone. If you do multiple calls to ->resize it shouldn't attempt to resize multiple times - it should overwrite the existing resize call imo.

This is a problem when you want to overwrite the resize method but can't stop the existing resize one being called (i.e. it's done from non userland code).

I do agree cloning all the operations is expected, which it will still do, but it just allows easier overriding of operations on the cloned object.

The other option would be some kind of clearOperations function something like:

$image->negate();
$image->resize(200, 200); // This cannot be changed.
$new = $image->clone();
$new->clearOperations(['resize', 'negate']);
$new->resize(200, 200);
garygreen commented 7 years ago

Hmm then again I supose it doesn't make sense for things like ->line and other stuff, you may want to do operations multiple times. Maybe need to rethink how doing this