Forums / Developer / Importing images

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

eZ debug

Timing: Jan 19 2025 03:22:10
Script start
Timing: Jan 19 2025 03:22:10
Module start 'content'
Timing: Jan 19 2025 03:22:10
Module end 'content'
Timing: Jan 19 2025 03:22:10
Script end

Main resources:

Total runtime0.2160 sec
Peak memory usage2,048.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0099 588.8359180.8438
Module start 'content' 0.01000.0074 769.6797105.9609
Module end 'content' 0.01740.1986 875.6406534.4844
Script end 0.2160  1,410.1250 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00361.6610200.0002
Check MTime0.00140.6357200.0001
Mysql Total
Database connection0.00140.633710.0014
Mysqli_queries0.163675.74391410.0012
Looping result0.00160.71881390.0000
Template Total0.198291.810.1982
Template load0.00080.355910.0008
Template processing0.197491.396510.1974
Override
Cache load0.00050.237410.0005
Sytem overhead
Fetch class attribute can translate value0.00080.372110.0008
XML
Image XML parsing0.00040.191610.0004
General
dbfile0.00492.2704200.0002
String conversion0.00000.003830.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs