Share » Forums » Developer » Importing an image attribute (from...

Importing an image attribute (from php) ?

Importing an image attribute (from php) ?

Monday 31 July 2006 7:27:24 am - 8 replies

Author Message

Felipe Jaramillo

Monday 31 July 2006 9:58:35 am

Hi Xavier,

Maybe this helps. The CSV extension available in pubsvn.ez.no has this code for the ezimage attribute:

case 'ezimage':
        {
            $contentObjectID = $contentObjectAttribute->attribute( 'contentobject_id' );
            $contentObjectVersion = $contentObjectAttribute->attribute( 'version' );
            makeImage( $contentObjectID, $contentObjectVersion, $contentObjectAttribute, $attributeContent );
            break;
        }

It calls the makeImage function:

function makeImage( $objectID, $version, &$attribute, $blob )
{
    $img =& imageInit();
    $contentObjectAttributeID = $attribute->attribute( "id" );
    $sys =& eZSys::instance();
    $storage_dir = $sys->storageDirectory();
    $tmpname = tempnam( "/tmp/" , "eZ" );
    $httpFileName = $blob;
    $content =& $attribute->attribute( 'content' );
    if ( trim( $httpFileName ) != "" and ( preg_match( "/http/i", $httpFileName ) ) )
    {
        $fhandler = fopen( $httpFileName, "r");
        if ( $fhandler  )
        {
            if ( $hasImageAltText )
                $content->setAttribute( 'alternative_text', $imageAltText );
            $result = true;

            $sys =& eZSys::instance();
            $storage_dir = $sys->storageDirectory();

            $urlArray = split('/', $httpFileName );
            $arrayCount = count( $urlArray );
            $tempFile = $urlArray[$arrayCount-1];

            $fwriter = fopen( $storage_dir. "/" . $tempFile, "w");
            $contentValue = "";
            do
            {
                $data = fread( $fhandler, 4096 );
                if ( strlen( $data ) == 0)
                {
                    break;
                }
                $contentValue .= $data;
            } while (true);

            fwrite( $fwriter, $contentValue);
            fclose( $fwriter );

            fclose( $fhandler );

            if ( is_object( $content ) )
            {
                $sys =& eZSys::instance();
                $storage_dir = $sys->storageDirectory();
                $imageFile= $storage_dir . "/" . $tempFile;
                $GLOBALS['eZURLImageIsStored'] = 1;
                if ( $hasImageAltText )
                {
                    $content->initializeFromFile( $imageFile, $imageAltText );
                }
                else
                {
                    $content->initializeFromFile( $imageFile );
                }
                if ( $content->isStorageRequired() )
                {
                    $content->store();
                }
                unlink( $imageFile );
            }
        }
    }
}

Regards,

Felipe

Felipe Jaramillo
eZ Certified Extension Developer
http://www.aplyca.com | Bogotá, Colombia

Kristof Coomans

Monday 31 July 2006 11:18:42 pm

Has somebody ever tried using the insertRegularFile method of the content object attribute? eZImage seems to support it.

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Xavier Dutoit

Tuesday 01 August 2006 12:33:14 am

Hi,

Magic Kristoff made it one more time, and indeed the method in ezimagetype seems to override it.

I'll try and let you know.

Fingers crossed...

X+

http://www.sydesy.com

Kristof Coomans

Tuesday 01 August 2006 6:48:53 am

Searching through the source code can hardly be called "magic" :-)

Some resources on ez.no:
http://ez.no/download/ez_publish/changelogs/ez_publish_3_5/general_insertion_interface_for_datatypes
http://ez.no/community/forum/developer/howto_use_ezcontentobjectattribute_setcontent_on_a_simple_text_datatype

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Felipe Jaramillo

Wednesday 16 August 2006 4:31:25 pm

We are looking into this too.

Xavier, did you manage to use the <i>insertRegularFile</i> method?

I wonder how it can be called effectively. We tried something like inside an import script:

// enumerate attributes of the new object to set them
$contentObjectAttributes =& $contentObjectVersion->contentObjectAttributes();			  foreach ($contentObjectAttributes as $attribute)
			{
if ($data_type_string=='ezimage') 
{
$attribute->insertRegularFile($contentObject, $contentObject->attribute( 'current_version' ), $contentObject->CurrentLanguage, $attribute, $parsedItems[$i][$TagName]);
}

Where:
$parsedItems[$i][$TagName] = image file name

But this doesn't seem to do anything. Can anyone confirm if this looks right?

On the other hand, we have had success doing massive image object imports with the Image Batch Upload Script
http://ez.no/community/contribs/import_export/image_batch_uploadscript_from_local_harddrive

It requires a little tweaking to work with current versions of eZ. The tricky part is knowing how to relate the image, or publish it under the appropriate object. Eg. if the object has a text attribute called "12345.gif", then when importing the image "12345.gif", one should fetch the object whose image attribute is "12345.gif", get it's node id and publish the image using that node_id as parent_node_id. The problem seems to be that fetching a tree with attribute filter is straightforward in template code, but not that clear how to properly code that function in the extension.

Felipe

Felipe Jaramillo
eZ Certified Extension Developer
http://www.aplyca.com | Bogotá, Colombia

Betsy Gamrat

Wednesday 16 August 2006 8:48:46 pm

Here is code that I used to import images into eZ 3.7.5. It is a very, very, simple implementation, but it works.

<b>$images is an array of image filenames</b>

foreach ($images as $k => $v)
{
        addImage($parentNodeID,$v['filename']);
}

<b>addImage: This is the code that updates the attributes</b>

        foreach (array_keys($contentObjectAttributes) as $key)
        {
                $contentObjectAttribute =& $contentObjectAttributes[$key];
                $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
                $attributeName = $contentClassAttribute->attribute('name');
                if ($attributeName=='Image')
                {
                         $cli->output( ' Image detected ' );
                         saveImage (IMAGE_DIR . $filename, $contentObjectAttribute);
                         $cli->output( 'Image: '.$filename );
                }

<b>saveImage: This is the code that stores the image in the system</b>

function saveImage( $sourceImage, &$contentObjectAttribute )
{
        $content =& $contentObjectAttribute->content();
        if ( is_object( $content ) )
        {
                $content->initializeFromFile( $sourceImage );
                if ( $content->isStorageRequired() )
                {
                        $content->store();
                }
        }
}

Piotr Switkowski

Tuesday 21 November 2006 11:48:56 am

I tried the code sent by Betsy in 3.8. One small change is needed in saveImage, the
$content->store() method require argument, so it should be changed to $content->store($contentObjectAttribute). But it really works !!

Bjarte Lunde

Tuesday 20 February 2007 5:09:18 pm

I'm sorry, but is something missing in Betsy's addimage code?

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

36 542 Users on board!

Forums menu