Jump to content

Tagging multiple images


fbg13
 Share

Recommended Posts

Hello!

I just found processwire yesterday (actually i heard about it before, but never looked into it) and installed it to play around.

I'm really impressed, as a guy who only used wordpress i like it very much and i tried to replicate one of my sites in processwire and so far it seems doable, the taxonomies will be a little tricky i think.

Now to the main point, i've read a blog post here giving some tips, one tip was to use only one image field and to group images by tags.

The problem: there doesn't seem to be a way to add a tag to multiple images at once, making the process inconvenient.

Would it be possible to add a prompt for defining a tag before selecting the images and the uploaded images will have that tag.

Link to comment
Share on other sites

Hi welcome to the forums,

I never dived into this, because I prepare my images before uploading and all other is done automagically. There are also solutions like somas Images Manager, and others too. But I don't know what or how these tools work out in this regard.

But just a question: "Would you be able to add IPTC keywords locally to your images before uploading?" If yes, then I can give you a codesnippet that imports all keywords into image tags, and (optionally) another IPTC field into the image description, on uploading.

Locally programs can be Adobe Bridge, IrfanView, and many others.

Link to comment
Share on other sites

7 minutes ago, horst said:

Would you be able to add IPTC keywords locally to your images before uploading?

I probably could, but i don't think this will improve my work flow.

But still, please share the code.

Link to comment
Share on other sites

Ok. :)

 

// code snippet that belongs into the site/ready.php file:

// a hook that reads IPTC keywords on images upload and populates them into the images tags field
$wire->addHookBefore("InputfieldFile::fileAdded", function(HookEvent $event) {

    $inputfield = $event->object;                      // handle to the image field
    if(!$inputfield instanceof InputfieldImage) {      // we need an images field, not a file field
        return;                                        // early return
    }

    if(version_compare(wire('config')->version, '2.8.0', '<')) {
        $p = $inputfield->value['page'];               // get the page, PW < 2.8
    } else {
        $p = $inputfield->attributes['value']->page;   // get the page, PW >= 2.8 | 3.0 (or only from 3.0.17+ ??)
    }

    $image = $event->argumentsByName('pagefile');      // get the image

    // check for IPTC data
    $additionalInfo = array();
    $info = @getimagesize($image->filename, $additionalInfo); // read image markers
    if($info !== false && is_array($additionalInfo) && isset($additionalInfo['APP13'])) { // APP13 is the IPTC marker
        $iptc = iptcparse($additionalInfo['APP13']);
        // IPTC field 025 = keywords collection
        if(is_array($iptc) && isset($iptc['2#025']) && is_array($iptc['2#025']) && count($iptc['2#025']) > 0) {
            $tags = array();
            foreach($iptc['2#025'] as $k => $v) {
                if(empty($v)) continue;
                $tags[] = trim(strtolower($v));
            }
            $p->get($inputfield->name)->trackChange('tags');  // prepare page to keep track for changes
            $image->tags = implode(' ', $tags);               // add the tags list as string
            $p->save($inputfield->name);                      // save the page images field
        }
    }
});

 

EDIT:

code was modified to detect $page also with PW version >= 2.8 | 3.0

EDIT 2:

Now it is working for all PW versions again. Had have the wrong HookEvent used here (addHokkAfter, but must be: addHookBefore!)

  • Like 4
Link to comment
Share on other sites

Oh, you were right. WIth PW 3, p was false. I have updated the snippet and the page is now detected. But unfortunately, the tags don't get saved, what puzzles me a bit. ???

I try to find out what may have changed here and will update the code then.

We have had changes to the images field with PW 3.17 / 3.18. Maybe that's why it is different now.

Link to comment
Share on other sites

@horst i changed to v2.7 to test it and changed the code to add the tag if the file name matches a string:

//$wire->addHookAfter("InputfieldFile::fileAdded", function(HookEvent $event) {
$wire->addHookBefore("InputfieldFile::fileAdded", function(HookEvent $event) {

    $log = wire('log');
    $inputfield = $event->object;                     // handle to the image field
    if(!$inputfield instanceof InputfieldImage) {     // we need an images field, not a file field
        return;                                       // early return
    }
    $p = $inputfield->value['page'];                  // get the page
    $image = $event->argumentsByName('pagefile');     // get the image

	if(strpos($image->filename, 'string'))
	{
		$log->error('match');
		$p->get($inputfield->name)->trackChange('tags');  // prepare page to keep track for changes
		$image->tags = 'sss';              // add the tags list as string
		$p->save($inputfield->name);       // save the page images field
	}else{
		$log->error('no match');
	}

});

But the tag is not added, am I missing something?

The error log show that the string matches.

 

Link to comment
Share on other sites

I have updated the code in the first post of it to also work with PW 3. But now I also get the tags not saved. I have asked on GitHub why this is not working anymore: https://github.com/ryancramerdesign/ProcessWire/issues/1920

I will investigate a bit more, but also hope that someone reads it on Github and can answer it. I have tested the code on a site with PW 2.7.2 and it worked for me before I posted it. I also will test your version on that site and report back.

Link to comment
Share on other sites

Now I found out that the Tags get saved for me also under PW 3, but the form fields (Tags, Descriptions) got not populated via AJAX, so when the page finally gots saved in the page editor, the previously saved values get blanked out again.

I HAVE USED THE WRONG HOOK! Damn! It has to be addHookBefore and not addHookAfter.

I changed the code in the first post so that it now is working with PW 2 and PW 3.

Sorry for the confusion. (I copied the function out of an old module into a $wire->hook for site/ready.php. Hereby I must have accidently changed to the wrong hook event. Damn!)

  • Like 5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...