Forums / Developer / Import Image Into Existing Class

Import Image Into Existing Class

Author Message

Alex Jones

Thursday 05 February 2004 7:03:10 am

I need to add the ability to include an image in my import script (http://ez.no/community/forum/developer/importing_update_entry_if_it_already_exists) and after looking around the site I have found a few different scripts, that seem to share the same base. Before I begin to incorporate parts of these scripts into my import script, I would like to hear of any other ways to do create this functionality. Also, has anyone experienced any problems with the image import scripts listed in the Contributions section? If so what were the problems, and how did you get past them?

Thanks!

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Wenyue Yu

Friday 06 February 2004 2:52:30 am

Hi,

It depends on which eZ publish are you using. If you are using eZ publish 3.1 or 3.2, you can refer to import scripts listed in the Contributions section and find out how to import image. If you are using eZ publish 3.3, you need to use following saveImage function:

function saveImage( $sourceImage, $originalImageFileName, $caption, &$contentObjectAttribute )
{
    $content =& $contentObjectAttribute->content();
    // $content is now an instance of eZImageAliasHandler
    if ( is_object( $content ) )
    {
        $content->initializeFromFile( $sourceImage, $caption, $originalImageFileName );
        if ( $content->isStorageRequired() )
        {
            $content->store();
        }
    }
}

Regards,
wenyue

Alex Jones

Friday 06 February 2004 7:03:48 am

Wenyue, thank you for the code. I am using 3.3, so this hsould be very helpful!

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Alex Jones

Friday 06 February 2004 1:09:39 pm

I am not able to get this working. What should the values for the variables look like? So you know, I am trying to include them in the script I posted in another thread (http://ez.no/community/forum/developer/importing_update_entry_if_it_already_exists). I have tried several ways to add it, all to no avail. The function you posted is inserted in the script, here is the code I am using to try to call the function:

if ($contentClassAttribute->attribute("name") == "Image")
  {
    saveImage ('/PATH_TO_SITE/ez/design/admin/images/logo.gif', 'logo.jpg', 'The logo', &$contentObjectAttribute);
  }

At the moment I am using the eZ logo for testing purposes. I am sure that I am missing sometihng obvious, but I can't put my finger on it.

Thanks for the help!

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Alex Jones

Friday 06 February 2004 1:13:01 pm

I have also tried to use a full URL instead of the document path, but that didn't work. I would prefer to avoid using HTTP and just pull the images off of a different part of the filesystem.

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Alex Jones

Tuesday 10 February 2004 5:18:03 am

Anyone?

Please? :)

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Alex Jones

Friday 13 February 2004 8:41:54 am

I am still stuck on this issue and would truly appreciate a nudge in the right direction. I have tried the code Wenyue was kind enough to post, as well as the code at the bottom of the New Image information (http://ez.no/ez_publish/download/changelogs/ez_publish_3_3/new_image_system) but, they both present the same problem. I have no idea what form the $sourceImage path should be in, and where the source image should be stored. Should it be relative to the import script? Relative the the server's root, a full URL?

I am banging my head against a wall on this, and it seems like there is an easy answer...

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Alex Jones

Friday 13 February 2004 8:58:32 am

Actually, I just discovered that the image is stored when I use a path from the server's root (not Web root). <b>But</b> the image is not associated with the entry, even though it is included in the right(?) spot within my import script. Confusing, but it's progress...

Alex

FYI: Here is the code I am using within my import script.

[The Function]

function saveImage( $sourceImage, $originalImageFileName, $caption, &$contentObjectAttribute )
{
  $content =& $contentObjectAttribute->content();
  // $content is now an instance of eZImageAliasHandler
  if ( is_object( $content ) )
  {
    $content->initializeFromFile( $sourceImage, $caption, $originalImageFileName );
    if ( $content->isStorageRequired() )
    {
      $content->store();
    }
  }
}

[Attribute Import]

if ($contentClassAttribute->attribute("name") == "Image")
  {
    saveImage ('/PATH_FROM_SERVER_ROOT/image.jpg', 'image.jpg', 'Image Caption Text', &$contentObjectAttribute);
  }

I do have an image object within the class, it is named 'Image'.

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Alex Jones

Monday 16 February 2004 8:08:01 am

Does anyone know why the image isn't associated with the proper entry, even though it is imported into the system?

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Alex Jones

Tuesday 17 February 2004 9:17:25 am

Some more information:

Below are two versions of the data_text for the ezcontentobject_attribute associated with the proper contentobject_id. The first is the one that is created by the import code (above) and is broken. The second was created when I edited the same document via the online editor and added the image manually,

<b>From Import (broken)</b>

<?xml version="1.0" encoding="UTF-8"?>
<ezimage serial_number="1"
         is_valid=""
         filename="klte10009."
         suffix=""
         basename="klte10009"
         dirpath="var/plain/storage/images/knives_for_immediate_delivery/fixed_blade/klte10009/3709-1-eng-US"
         url="var/plain/storage/images/knives_for_immediate_delivery/fixed_blade/klte10009/3709-1-eng-US/klte10009."
         original_filename=""
         mime_type=""
         width=""
         height=""
         alternative_text=""
         alias_key="1293033771"
         timestamp="">
  <original attribute_id=""
            attribute_version=""
            attribute_language="" />
</ezimage>

<b>From Online Editor (working)</b>

<?xml version="1.0" encoding="UTF-8"?>
<ezimage serial_number="1"
         is_valid="1"
         filename="klte10009.jpg"
         suffix="jpg"
         basename="klte10009"
         dirpath="var/plain/storage/images/knives_for_immediate_delivery/fixed_blade/klte10009/3709-2-eng-US"
         url="var/plain/storage/images/knives_for_immediate_delivery/fixed_blade/klte10009/3709-2-eng-US/klte10009.jpg"
         original_filename="d&apos;holder3.jpg"
         mime_type="image/jpeg"
         width="162"
         height="173"
         alternative_text=""
         alias_key="1293033771"
         timestamp="1077036435">
  <original attribute_id=""
            attribute_version=""
            attribute_language="" />
  <alias name="reference"
         filename="klte10009_reference.jpg"
         suffix="jpg"
         dirpath="var/plain/storage/images/knives_for_immediate_delivery/fixed_blade/klte10009/3709-2-eng-US"
         url="var/plain/storage/images/knives_for_immediate_delivery/fixed_blade/klte10009/3709-2-eng-US/klte10009_reference.jpg"
         mime_type="image/jpeg"
         width="162"
         height="173"
         alias_key="-1689502181"
         timestamp="1077036457"
         is_valid="1" />
  <alias name="ce_index"
         filename="klte10009_ce_index.jpg"
         suffix="jpg"
         dirpath="var/plain/storage/images/knives_for_immediate_delivery/fixed_blade/klte10009/3709-2-eng-US"
         url="var/plain/storage/images/knives_for_immediate_delivery/fixed_blade/klte10009/3709-2-eng-US/klte10009_ce_index.jpg"
         mime_type="image/jpeg"
         width="160"
         height="170"
         alias_key="1367709008"
         timestamp="1077036457"
         is_valid="1" />
</ezimage>

Several things stand out:
1. The working version contains much more information.
2. The <i>filename</i> in the broken version doesn't include the extension (jpg), <i>is_valid</i>, <i>suffix</i> and many other fields are empty.
3. No Aliases were created.

Where is the disconnect?

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Alex Jones

Thursday 19 February 2004 7:37:10 am

Anyone?

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Bård Farstad

Friday 20 February 2004 12:43:53 am

Your code looks correct. Have you checked read/write permissions on both the source and the destination folder? Mabye the user you the the import script as doesn't have proper permissions?

--bård

Documentation: http://ez.no/doc

Alex Jones

Friday 20 February 2004 8:47:38 am

I've double-checked the permissions, and everythingis set up properly. The user account that I log in with (via SSH) has the rights to execute the import script, index.php and is allowed to read/write from/to both the source files and the destination directory. An odd thing I have noticed is that the first time I run the import script now, the destination folder is created where it should be but the image itself is not written to that folder. If I run the script again with the exact same data, the image is added to the directory, though the entry within eZ publish still does not recognize that it is there. Also, there is only one image put into that directory, none of the variatoions are created.

Really odd.

Alex

Oh, and the information listed for the image in the contentobject_attribute table is still incomplete as noted above.

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Alex Jones

Friday 20 February 2004 9:24:33 am

FYI, the full script may be viewed at: http://kelvin.agrknives.com/cuttingedge/highlight.php

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Paul Forsyth

Sunday 22 February 2004 10:00:35 am

 

Antek Goldstein

Sunday 14 March 2004 3:08:10 am

If I read kernel code correctly, it works as follows (concerning initializeFromFile($filename, $caption, $origfile))
- checks if $filename exists, fails if not,
- sets $origfile to $filename if the former is null,
- finds out MIME type of $origfile
- generates new filename from MIME type
and so on. Looks like MIME type check fails in this case, perhaps $filename exists, but $origfile doesn't?
Or is it in wrong directory? Try omitting $origfile.

Nathan Mann

Tuesday 13 April 2004 7:15:55 pm

Alex,

I tried Antek's suggestion, and it seems to have worked.

Here is my code:

function saveImage( $sourceImage, &$contentObjectAttribute )
{
  $content =& $contentObjectAttribute->content();
  // $content is now an instance of eZImageAliasHandler
  if ( is_object( $content ) )
  {
    $content->initializeFromFile( $sourceImage );
    if ( $content->isStorageRequired() )
    {
      $content->store();
    }
  }
}

and

if ($contentClassAttribute->attribute("identifier") == "picture")
{
  $image_name = $ImportData[2].".jpg";
  saveImage ("/PATH_FROM_SERVER_ROOT/$image_name", &$contentObjectAttribute);
}

As you can see, I also had no need for the caption, so that has been removed as well.

This should work with your code, as my script is a modified version of yours and Paul's (thank you both!).

Nathan

Alex Jones

Wednesday 14 April 2004 5:56:34 am

Nathan, thanks for posting! I had solved the problem previously, and neglected to post an update here. Sorry! Now that the site is almost complete (we've done a quiet launch, and are about to announce the site), I will try to post a complete version of my import script. Part of the issue I ran into was due to a problem in the kernel code, which the fine folks at eZ systems (Wenyue!) were very helpful in tracking down. I believe the issue has been solved in an update, so it shouldn't plague others. :)

For those of you pondering it, the support contracts are well worth it. Wenyue saved me a lot of time and ended my frustrations.

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>