eZ Community » Forums » Developer » remove an Image programmatically in...
expandshrink

remove an Image programmatically in ezpublish

remove an Image programmatically in ezpublish

Saturday 13 June 2015 6:57:04 pm - 8 replies

I have an Object in ezPublish which has an image attribute.this attribute has an image as value that I want to remove it programmatically in PHP. ( the value and not attribute itself )

any idea how to do it ?

P.S: I am using ezpublish 4.7

Sunday 14 June 2015 9:28:28 pm

Have a look at, for example: 

https://github.com/mugoweb/eep/bl...modules/contentobject/index.php#L309

 

There are a few issues you have to deal with:

* permissions within ezpublish

* making sure the image files themselves are removed

Sunday 14 June 2015 9:33:18 pm

Quote from Doug Plant :

Have a look at, for example: 

https://github.com/mugoweb/eep/bl...modules/contentobject/index.php#L309

 

There are a few issues you have to deal with:

* permissions within ezpublish

* making sure the image files themselves are removed

Thanks Doug. 

but the line you are referring to in the example code is a function which removes the whole object and not just one image attribute.

Sunday 14 June 2015 9:53:56 pm

Sorry! That should teach me to multitask on a Sunday!

 

The useful API function is FromString() which exists on a datamap object, which is called from within:

https://github.com/mugoweb/eep/blob/master/lib/AttributeFunctions.php#L531

 

The value is a path to the new image file. I don't know what happens if you pass in an empty path, in particular, if the old image and all its aliases are removed. If that is not automatic, the only tip I would offer is that the filepath, within the var folder, uses the attribute-value id.

Monday 15 June 2015 3:03:20 pm

Hello,

I have found a solution to Amir's question. 

After extensive work with Amir (outside the forum) via HipChat and TeamViewer we have solved Amir's problem.

Due to a version specific BC break with ezimagetype class (somewhere) between 4.7 and 2014.11 we had extended trouble and thus more debugging my original solution (which worked out of the box for BC with 2014.11).

Brookins Consulting is nearly complete developing a small example snippet of eZ Publish Legacy PHP code which does the work requested and a much larger companion ez php command line script which acts as a more complex and flexible example of how to do what Amir asked about and almost all other 'most common' use cases.

Feel free to save your time and life force and ignore this thread till we publish our solution(s) for the whole eZ Community to study, learn from, use and share! We think it will be well worth the wait.

Please stand by big-smile.gif Emoticon

Cheers,
Heath

Tuesday 16 June 2015 7:33:13 am

Hello Amir and Doug,

First I want to apologize for my late followup to this thread with answers to share for everyone. I really was working on the the problem behind the scenes before answering this thread properly.
 

Command line script

We are very excited to announce the public release of our new eZ Publish Legacy extension script, bcremoveobjectimageattributecontent!

https://github.com/brookinsconsulting/bcremoveobjectimageattributecontent

This solution provides a useful tool to remove content object image attribute content that works right out of the box.

What we like about this tool was that it seems to be unique, we could find no previous public reference to another asking this same specific question, providing these answers or an tool to do this job.

Another reason we like this tool is that it is flexible to accommodate a number of different use cases.
It can update multiple content object's with multiple unique image attributes (identifiers) current version, all versions (in the system), or just create a new version.

It also supports extended execution debug output mode with various levels (0 -> 6) of debug output. It also supports a test only mode that can be used first to see what changes could be made without modifying any part of the database or filesystem.

That said, we (BC) can still see room for additional features to be added to the solution (node subtree support, object-exclusion support, class based inclusion / exclusion support, etc) but this solved the immediate needs and provides a basis for others with a specific interest to collaborate, extend and discuss further.

We are open to your feature requests. If you want something different, just ask happy.gif Emoticon

 

Example solution snippet

The following is a complete example of the source code required to remove a content object image attribute content (and remove the related meta-data and image file from disk) the best way possible for nearly any version of eZ Publish Legacy.

// The following two variables are filled with dummy values.
// You will need to change the contents of these variables to match
// your actual use case identifiers (Content Object ID / Class Attribute Identifier)
$objectID = 42;
$objectImageAttributeIdentifier = 'profile_image';
 
$object = eZContentObject::fetch( $objectID );
$objectID = $object->attribute( 'id' );
$objectCurrentVersion = $object->attribute( 'current_version' );
$objectDataMap = $object->attribute( 'data_map' );
 
if ( isset( $objectDataMap[ $objectImageAttributeIdentifier ] ) )
{
    $objectImageAttribute = $objectDataMap[ $objectImageAttributeIdentifier ];
    if ( $objectImageAttribute->attribute( 'has_content' ) )
    {
        $objectImageDataType = $objectImageAttribute->dataType();
        $objectImageDataType->deleteStoredObjectAttribute( $objectImageAttribute, $objectCurrentVersion );
        eZContentCacheManager::clearContentCacheIfNeeded( $objectID );
    }
}

 

eZ Publish API Compatibility Between Different Versions

For versions of eZ Publish (Legacy) prior to Oct 15, 2013, or git tag, 'v2014.03.1' including eZ Publish 4.7 the 'deleteStoredObjectAttribute' method -**requires**- a non-null second argument value be passed in versions of eZ Publish Legacy before the following commit:

See: https://github.com/ezsystems/ezpublish-legacy/commit/61aaa002c00ccfb86b3e02856b319f54b3405ef8

This includes eZ Publish 4.7 and thus the question author's specific use case. Which is why this answer is more accurate than all others.

Without this second non-null parameter, the aliases image files would be deleted from the filesystem but ... the image aliases information (alias references, meta data, etc) would still exist with the content object attribute content (database storage xml).

Without the second non-parameter the image would appear as still partially existing in the content object but the image preview, (IE: usage of the image path, meta data by the system) would display a broken image and thus then represent a corrupt / incomplete copy of the original content object attribute content.

For maximum Backwards Compatibility, it's best to always pass a non-null second parameter since versions of eZ Publish Legacy beyond 10/15/2013 do not even use the second parameter in any way.

 

Problems with using the fromString method

As Doug suggests with most datatypes (like ezstring), it is possible to replace content object attribute content using the fromString method directly or indirectly through the use of the abstracted 'eZContentFunctions::updateAndPublishObject' method.

The problem with using a solution based on fromString with an ezimage based datatype is that the fromString is that it normally does not work so well. During our extended sessions surrounding this thread we found that it simply failed to provide for the features required.

Here is the ezimagetype class, 'fromString' method: https://github.com/ezsystems/ezpublish-legacy/blob/master/kernel/classes/datatypes/ezimage/ezimagetype.php#L614

Which calls the ezimagealiashandler class, 'initializeFromFile' method: https://github.com/ezsystems/ezpublish-legacy/blob/master/kernel/classes/datatypes/ezimage/ezimagealiashandler.php#L1135

Notice that if the fromString input is an empty string (as previously suggested to try by a few folks, not just Doug) a error log entry is generated and the content object content is not modified: https://github.com/ezsystems/ezpublish-legacy/blob/master/kernel/classes/datatypes/ezimage/ezimagealiashandler.php#L1151

This is why at this time we would not recommend using the ezimage datatype fromString method with an empty string input.

We still want to thank Doug for stepping up and engaging in this forum thread.

Note: This thread is cross posted on StackOverflow: http://stackoverflow.com/questions/30821695/remove-an-image-programmatically-in-ezpublish/30849633#30849633

If anyone has an further questions on this topic I would be happy to answer your questions.

I hope this helps!

Cheers,
Heath

Tuesday 16 June 2015 8:28:52 pm

Thanks Heath - I'm awestruck. You BC guys have really raised the bar on the support forums. 

Thursday 18 June 2015 12:14:13 am

Hello Doug,

Thank you for your kind words. We are honored by your praise.

Cheers,
Heath

Thursday 18 June 2015 2:06:53 am

really Thanks Heath.

you guys have moved support in ez ecosystem to another level happy.gif Emoticon

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from