Jump to content
fbg13

Tagging multiple images

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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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 3

Share this post


Link to post
Share on other sites
15 minutes ago, fbg13 said:

@horst $p seems to be false for me

 

Why? This code is running for me over different PW versions from 2.3 till today. (at least 2.7.2)

What PW version are you on?

Share this post


Link to post
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.

Share this post


Link to post
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.

 

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By humanafterall
      I'm using some Custom fields for images: 
      https://processwire.com/blog/posts/pw-3.0.142/#custom-fields-for-files-images

      When I save the page, and return the fields are blank. When I re-add the text to the fields and save again then the fields save as expected.

      I know this is stated as being quite experimental but it's super useful feature I'd love to get working correctly.
      I have fields that are CKEditor fields but have overidden this on the image specific template. I've also tried it with regular text fields and I get the same bug.
      (currently using Processwire 3.0.155)
      **UPDATE**
      I've found this issue is specific to editing on pages using the PageTable fieldtype. The fields are not saving when I save the page in the PageTable.
    • By Robin S
      Displays image tags overlaid on the thumbnail using customisable colours. This makes it easier to see which images have which tags without needing to open the edit pane for individual images or changing to the list view.
      Screenshot

      Usage
      Enable tags for one or more image fields. Install the Image Thumbnail Tags module. Optionally configure colours for any of your tags.
       
      https://github.com/Toutouwai/ImageThumbnailTags
      https://modules.processwire.com/modules/image-thumbnail-tags/
    • By EyeDentify
      Hello dear PW gurus.

      I have stumbled over a strange error that i all of sudden got when trying to upload an image to a images field on a page.
      There where images allready stored in the field that i wanted to keep, but during the upload the error apear and after that all images are gone from the field and i can´t upload any, i just get the error every time.
      I am running ProcessWire 3.0.153 dev.
      Update:
      After looking in the assets folder i find the folder for the page and the image files seems to be there including the ones i tried to upload when the error occured.
      But they don´t show up in the images field in the page editor.
       
      The error reported:
       
      SQLSTATE[01000]: Warning: 1265 Data truncated for column 'ratio' at row 1 And here is a screenshot of the event:

      The TracyDebugger Error reporting:

      I hope you fine folks could point me in a direction.
      But it seems our old pal set_time_limit() is back.

      Regards, EyeDentify
    • By Peter Knight
      If an image is added to a site via the rich text field, do we have a way to apply a custom wrapper or mini template to it?
      IE I want to apply some markup srcset to some user added images. I can only seem to achieve this when the image markup is in the template itself.
      https://github.com/rolandtoth/MarkupSrcSet/wiki
      I can probably achieve this with RepeaterMatrix and by creating a specific Images field (and images template) but outside of that ?
      Thanks
×
×
  • Create New...