Importing an image attribute (from php) ?

Author Message

Xavier Dutoit

Monday 31 July 2006 7:27:24 am

Hi all,

Back from holiday, back to the ez things...

I want to import content (importxml contrib is going to make it). However, I want to import an image and put it into the image attribute.

I don't understand how to make it from php. Basically, what I have is a file /tmp/import/picture.jpg and I want to put it into the logo attribute (type image) of my class.

Any help more than welcome.

As a side note, it would be very useful to have an abstract method load on the base attribute class that has as parameter an associative array.

Then each attribute would override it and the integer attribute would put it into data_int, string into data_text, xml block add whatever "<section>" thing needed around, image upload the file, store it where needed and so on.

Does it exist already and I'm missing something ?

X+

http://www.sydesy.com

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?

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.