eZ Community » Forums » Developer » Import Image Into Existing Class
expandshrink

Import Image Into Existing Class

Import Image Into Existing Class

Thursday 05 February 2004 4:03:10 pm - 17 replies

I need to add the ability to include an image in my import script (http://ez.no/community/forum/deve...g_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

Friday 06 February 2004 11: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

Modified on Friday 06 February 2004 11:57:24 am by Wenyue Yu

Friday 06 February 2004 4:03:48 pm

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

Alex

Friday 06 February 2004 10: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/deve...g_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

Modified on Friday 06 February 2004 10:10:20 pm by Alex Jones

Friday 06 February 2004 10: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

Modified on Monday 09 February 2004 4:30:22 pm by Alex Jones

Tuesday 10 February 2004 2:18:03 pm

Anyone?

Please? happy.gif Emoticon

Alex

Modified on Thursday 12 February 2004 11:38:11 pm by Alex Jones

Friday 13 February 2004 5:41:54 pm

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/...ogs/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

Friday 13 February 2004 5:58:32 pm

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'.

Modified on Friday 13 February 2004 9:32:01 pm by Alex Jones

Monday 16 February 2004 5:08:01 pm

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

Alex

Tuesday 17 February 2004 6:17:25 pm

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

Thursday 19 February 2004 4:37:10 pm

Anyone?

Alex

Friday 20 February 2004 9: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

Friday 20 February 2004 5:47:38 pm

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.

Modified on Friday 20 February 2004 5:54:31 pm by Alex Jones

Friday 20 February 2004 6:24:33 pm

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

Alex

Modified on Friday 20 February 2004 6:24:52 pm by Alex Jones

Sunday 22 February 2004 7:00:35 pm

 

Modified on Sunday 22 February 2004 10:01:31 pm by Paul Forsyth

Sunday 14 March 2004 12:08:10 pm

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.

Wednesday 14 April 2004 4:15:55 am

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

Wednesday 14 April 2004 2:56:34 pm

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. happy.gif Emoticon

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

Modified on Wednesday 14 April 2004 2:57:16 pm by Alex Jones

expandshrink

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

36 542 Users on board!

Forums menu

Proudly Developed with from