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.

eZ debug

Timing: Jan 18 2025 22:22:33
Script start
Timing: Jan 18 2025 22:22:33
Module start 'layout'
Timing: Jan 18 2025 22:22:33
Module start 'content'
Timing: Jan 18 2025 22:22:34
Module end 'content'
Timing: Jan 18 2025 22:22:34
Script end

Main resources:

Total runtime0.8690 sec
Peak memory usage4,096.0000 KB
Database Queries57

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0086 587.7969152.6094
Module start 'layout' 0.00860.0045 740.406339.4141
Module start 'content' 0.01320.8546 779.8203575.5391
Module end 'content' 0.86780.0012 1,355.359420.5000
Script end 0.8690  1,375.8594 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00330.3806160.0002
Check MTime0.00130.1545160.0001
Mysql Total
Database connection0.00070.078010.0007
Mysqli_queries0.817894.1066570.0143
Looping result0.00070.0832550.0000
Template Total0.835896.220.4179
Template load0.00200.226020.0010
Template processing0.833995.954220.4169
Template load and register function0.00010.009710.0001
states
state_id_array0.00100.120410.0010
state_identifier_array0.00170.198520.0009
Override
Cache load0.00150.1771280.0001
Sytem overhead
Fetch class attribute can translate value0.00060.066420.0003
Fetch class attribute name0.00100.109950.0002
XML
Image XML parsing0.00090.101520.0004
class_abstraction
Instantiating content class attribute0.00000.002960.0000
General
dbfile0.00090.1083220.0000
String conversion0.00000.001640.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
3content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
3content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
6content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
1content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 17
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs