在 ezpublishs 中以编程方式删除图像


remove an Image programmatically in ezpublish

>我在ezPublish中有一个对象,它有一个图像属性。此属性有一个图像作为值,我想在 PHP 中以编程方式删除它。(图像值而不是属性本身)

知道怎么做吗?

对于 2013 年 10 月 15 日之前的 eZ 发布(旧版)版本或 git 标签,包括 eZ Publish 4.7,"deleteStoredObjectAttribute"方法 - 要求 - 在以下提交之前在 eZ 发布旧版版本中传递一个非空的第二个参数值:

请参阅:https://github.com/ezsystems/ezpublish-legacy/commit/61aaa002c00ccfb86b3e02856b319f54b3405ef8

这包括 eZ Publish 4.7,因此包括问题作者的特定用例。这就是为什么这个答案比其他所有答案都更准确。

如果没有第二个非空参数,别名图像文件将从文件系统中删除,但是......图像别名信息(别名引用、元数据等)仍将与内容对象属性内容(数据库存储 XML)一起存在。

如果没有第二个非参数,图像将显示为仍然部分存在于内容对象中,但图像预览(IE:图像路径的使用,系统的元数据)将显示损坏的图像,从而表示原始内容对象属性内容的损坏/不完整副本。

为了获得最大的向后兼容性,最好始终传递一个非空的第二个参数,因为 2013 年 10 月 15 日之后的 eZ Publish Legacy 版本甚至不会以任何方式使用第二个参数。

以下是删除内容对象图像属性内容(并从磁盘中删除相关的元数据和图像文件)所需的源代码的完整示例,这是几乎任何版本的 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 );
    }
}

根据eZImageType::customObjectAttributeHTTPAction(),你应该使用eZImageType::deleteStoredObjectAttribute()

该方法的实现显示了如何在内部完成它。该方法不会删除属性本身,只会删除外部数据(图像、别名、将 xml 设置为空)。