Importing images

Author Message

Eirik Alfstad Johansen

Tuesday 27 January 2004 1:21:26 am

Hi,

I was wondering if anyone had any experience importing images into content object attributes. I'm importing a list of data into content objects, and one of the fields that needs to be stored is an image.

I've tried using the saveImage() function in the 2.2 -> 3.x import contribution (http://www.ez.no/community/contributions/import_export/import_script_for_data_import_from_2_2_to_3_x) but I can't get it to work. All it does is store the image in the [storage_dir]/original/image folder, and display an emtpy img tag for the object when viewed.

So, if anyone have any classes/function for image import lying around, I would be very grateful!

Thanks in advance !

Sincerely,

Eirik Johansen

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Georg Franz

Tuesday 27 January 2004 2:47:05 am

Hi Eirik,

i had the same problem.

Do you want to import the images in 3.2x or 3.3? (3.3 is using the new image system).

Try - at your own risk ;-) - (backup - backup - backup ...) this code in importimage.php:

function addImage( &$image )
{
    global $databasePrefix;
    $db =& eZDB::instance();
	$db->setIsSQLOutputEnabled( false );
	//fetch folder class
	$class =& eZContentClass::fetch( 5 );

    unset( $contentObject );
    $imageID = $image['ID'];
    
    $imageName = $image['Name'];
    
    if (trim ($imageName) == "")
    	$imageName = "No Name";
    
    $imageDescription = $image['Description'];
    $imageCreatedTime = $image['Created'];
    $imageFileName = $image['FileName'];
    $imageOriginalFileName = $image['OriginalFileName'];
    $imageCaption = $image['Caption'];
    $imageUserID = $image['UserID'];

	echo "ID: ".$image['ID']." $imageName / $imageFileName\n";

    if ( $databasePrefix != "" )
    {
        $remoteUserID = $databasePrefix . "_" . "user_" . $imageUserID;
        $remoteID = $databasePrefix . "_" . "image_" . $imageID;
        $remoteParentIDPrefix =  $databasePrefix . "_";
        $unassignedImageFolderID = $databasePrefix . "_" . "image_unassigned_folder";
    }
    else
    {
        $remoteUserID =  "user_" . $imageUserID;
        $remoteID = "image_" . $imageID;
        $remoteParentIDPrefix = "";
        $unassignedImageFolderID = "image_unassigned_folder";
    }

	$result = $db->arrayQuery( "SELECT * from ezcontentobject WHERE remote_id = '$remoteID'" ) ;
    
    if ( $result == NULL )
    {

	    $remoteParentNodeArray = $db->arrayQuery( "SELECT * from eZImageCatalogue_ImageCategoryLink WHERE ImageID = '$imageID'" );
	
	    $assignedNodes = array();
	    if ( $remoteParentNodeArray != null )
	    {
	        // Find all parent node
	        foreach ( $remoteParentNodeArray as $remoteParentNode )
	        {
	            $remoteParentID = $remoteParentIDPrefix . "image_category_" . $remoteParentNode['CategoryID'];
	            // Find parent node id
	            $parentNodeIDArray = $db->arrayQuery( "SELECT ezcontentobject_tree.node_id FROM ezcontentobject, ezcontentobject_tree
	                                                   WHERE ezcontentobject.remote_id = '$remoteParentID' AND ezcontentobject.id = ezcontentobject_tree.contentobject_id" );
	            $parentNodeID = $parentNodeIDArray[0]['node_id'];
	            $assignedNodes[] = $parentNodeID;
	        }
	    }
	    else
	    {
	        // For those unassigned images, import to main image folder.
	        $parentNodeIDArray = $db->arrayQuery( "SELECT ezcontentobject_tree.node_id FROM ezcontentobject, ezcontentobject_tree
	                                               WHERE ezcontentobject.remote_id = '$unassignedImageFolderID' AND ezcontentobject.id = ezcontentobject_tree.contentobject_id" );
	        $parentNodeID = $parentNodeIDArray[0]['node_id'];
	        $assignedNodes[] = $parentNodeID;
	    }
	
	    // Find current user id
	    $userIDArray = $db->arrayQuery( "SELECT id FROM ezcontentobject WHERE remote_id = '$remoteUserID'" );
	
	    if ($userIDArray == NULL)
			$userID = NULL;
		else
	    	$userID = $userIDArray[0]['id'];
	
	
	    // If no exist user, set it to administrator.
	    if ( $userID == null )
	        $userID = 14;
	
	    if ( $userID != null )
	    {
	        // Create object by user id in section 1
	        $contentObject =& $class->instantiate( $userID, 1 );
	        $contentObject->setAttribute('remote_id', $remoteID );
	        $contentObject->setAttribute( 'name', $imageName );
			
			$first = 1;
	        foreach ( $assignedNodes as $assignedNode )
	        {
	            $nodeAssignment =& eZNodeAssignment::create( array(
	                                                         'contentobject_id' => $contentObject->attribute( 'id' ),
	                                                         'contentobject_version' => $contentObject->attribute( 'current_version' ),
	                                                         'parent_node' => $assignedNode,
	                                                         'sort_field' => 2,
	                                                         'sort_order' => 1,
	                                                         'is_main' => $first
	                                                         )
	                                                     );
	            $nodeAssignment->store();
	            $first = 0;
	        }
	
	        $version =& $contentObject->version( 1 );
	        $version->setAttribute( 'modified', $imageCreatedTime );
	        $version->setAttribute( 'created', $imageCreatedTime );
	        $version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT );
	        $version->store();
	
	        $contentObjectID = $contentObject->attribute( 'id' );
	        $contentObjectAttributes =& $version->contentObjectAttributes();
	
	        $contentObjectAttributes[0]->setAttribute( 'data_text', $imageName );
	        $contentObjectAttributes[0]->store();
	
	        $inputData = "<?xml version=\"1.0\"?>";
	        $inputData .= "<section>";
	        $inputData .= "<paragraph>";
	        $inputData .= $imageCaption;
	        $inputData .= "</paragraph>";
	        $inputData .= "</section>";
	
	        include_once( "kernel/classes/datatypes/ezxmltext/handlers/input/ezsimplifiedxmlinput.php" );
	        $dumpdata = "";
	        $simplifiedXMLInput = new eZSimplifiedXMLInput( $dumpdata, null, null );
	        $inputData = $simplifiedXMLInput->convertInput( $inputData );
	        $description = $inputData[0]->toString();
	        $contentObjectAttributes[1]->setAttribute( 'data_text', $description );
	        $contentObjectAttributes[1]->store();
	
	        $contentObjectAttribute =& $contentObjectAttributes[2];
	
	        saveImage( $imageFileName, $imageOriginalFileName, $imageCaption, $contentObjectAttribute );
	        $contentObjectAttributes[2]->store();
	
	        include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
	        $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObjectID,
	                                                                                 'version' => 1 ) );
	        $contentObject->setAttribute('modified', $imageCreatedTime );
	        $contentObject->setAttribute('published', $imageCreatedTime );
	        $contentObject->store();
	    }
	}
	else
	{
		echo "Already imported\n";
		//var_dump($result);
		echo "\n\n";	
	}
}

function saveImage( $imageFileName, $originalImageFileName, $caption, &$contentObjectAttribute )
{
    include_once( "lib/ezutils/classes/ezdir.php" );
    $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
    $version = $contentObjectAttribute->attribute( "version" );

    include_once( "kernel/common/image.php" );
    include_once( "kernel/classes/datatypes/ezimage/ezimage.php" );
    $image =& eZImage::create( $contentObjectAttributeID , $version );

    $image->setAttribute( "contentobject_attribute_id", $contentObjectAttributeID );
    $image->setAttribute( "version", $version );
    $image->setAttribute( "filename", $imageFileName );
    $image->setAttribute( "original_filename", $originalImageFileName );

    $mimeObj = new eZMimeType();
    $mime = $mimeObj->mimeTypeFor( false, $originalImageFileName );
    $image->setAttribute( "mime_type", $mime );
    $image->setAttribute( "alternative_text", $caption );
    $image->store();

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

    $ori_dir = $storage_dir . '/' . "original/image";
    $ref_dir = $storage_dir . '/' . "reference/image";
    if ( !file_exists( $ori_dir ) )
    {
        eZDir::mkdir( $ori_dir, 0777, true);
    }
    if ( !file_exists( $ref_dir ) )
    {
        eZDir::mkdir( $ref_dir, 0777, true);
    }

    $source_file = $storage_dir . "/catalogue/" . $imageFileName;
    $target_file = $storage_dir . "/original/image/" . $imageFileName;
    $reference_file = $storage_dir . "/reference/image/" . $imageFileName;
    copy($source_file, $target_file );

}

PS.: I don't know if my code is still working, as I can remember - I used it for ez 3.3.1 beta (or something). And some things were changed in the past.

Kind regards,
Emil.

Best wishes,
Georg.

--
http://www.schicksal.com Horoskop website which uses eZ Publish since 2004

Georg Franz

Tuesday 27 January 2004 3:00:20 am

by the way,

take care at using the importfolder.php if you are having a large tree structure (article categories / image categories etc.).

you must write a "getTree"-class / function to get the right placement of the categories.

Kind regards,
Emil.

Best wishes,
Georg.

--
http://www.schicksal.com Horoskop website which uses eZ Publish since 2004

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