0

I’d like to generate images suitable to insert into metadata (like Open Graph or twitter:image). Basically I want to create a collage based on posts for each tag. So let’s say I had the tag “cats” which had 30 posts, 10 of which had a “featured image” of cats in them.

If someone shares http://example.org/tag/cats to twitter I want to add metadata to point to a 1024x512 image that is a collage of some of those featured images. And ideally this is am image that would update at some period, so in a year when I have 10 more available images it’s kept up to date.

Questions:

  1. Do plugins to aggregate featured images exist in this way?
  2. Are there native WordPress functions to manipulate images, or plugins to add this functionality?
  3. I want to keep such images up to date, how do I do this without bogging down load on my server?
  4. Is saving these images into the default uploads folder even a good idea? Will there be pitfalls if I am changing these images on the fly if I am also using a CDN?
  • 1
    Your question is quite broad. Third party plugin questions are considered off topic, so (1) and part of (2) are off topic. To quickly answer (3): Use a scheduled event to generate the image (once a day should suffice), alternatively you could create the image once a post in the tag has been published. (4) is also too broad for this Q&A (in my opinion). Short answer: depends, I'd probably go the default uploads folder way. – kero Feb 6 at 17:53
3
+250

Ignoring our off-topic request for a plugin, the good news is: yes, this is possible. On the other hand, it is quite some work, so I will give just an outline here.

WordPress does have built-in image manipulation. The default one is Image Magick. WordPress' image editor class, however, only offers a subset of what Image Magick is actually capable of. This means that you will have to pierce through the class and use the PHP commands directly to do what you want.

First you will have to register an image size

add_action( 'init', 'wpse358037_register_collage_size' ); 
function wpse358037_register_collage_size() {
  add_image_size( 'collage', 1024, 512, true ); 
  }

Second, when a tag is created you want to set a featured image for it, composed from one or more featured images of posts with that tag. There are several ways to do this, but the handiest may be hooking into the set_object_terms action. This fires at the end of wp_set_object_terms, a function that is executed whenever a post taxonomy is created or updated. Assuming that you only want to update the collage image when a new post/picture has been added, this is the perfect hook to create/update the collage without putting a burden on your server. You create the collage image and attach it as metadata to the tag. Pseudocode:

add_action( 'set_object_terms', 'wpse358037_register_collage_size', 10, 6 );
function wpse358037_register_collage_size ($object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids) {
  if (... some sort of condition to match only post tags ...) {
    // if there are multiple tags, you may have to loop throug them
    foreach ($tags as $tag) {
      $images_to_be_used = ... depending on tag and you criteria make array of images
      $collage_image_path = wpse358037_collage_image ($tag, $images_to_be_used);
      // save the path to the resulting image as metadata to the tag
      add_term_meta ($tag_ID, 'collage-image', $collage_image_path, false) ;
      }
    }
  }

Finally you'll have to do the actual image action for which Image Magick even has a native function called mosaic:

 function wpse358037_collage_image ($tag, $images_to_be_used) {
   // loop through all $images_to_be_used doing this
     $collage_image[i] = new Imagick( $path_to_image);
   // now you have an array of images ready to be handled by Image Magick
     .. do your mosaic magic and save the result in $collage_image_final_result
   // all that is left now is saving the image and return the path so it can be saved as metadata
   $path = .. wherever you want to save it (upload_dir + name + '.jpg')
   $collage_image_final_result->writeImage ($path);
   return $path;
   }

As you can see, there's still quite a lot of development work to be done, but hopefully this outline will help you get what you want.

| improve this answer | |
1

Are there native WordPress functions to manipulate images

Yes, there's is the WP_Image_Editor class for manipulating images. But I don't think it is able to combine images. Unless the WP_Image_Editor_GD or WP_Image_Editor_Imagick subclasses support that somehow.

You can use WP to query the posts in tag and get the featured images. Then use plain PHP to combine the images into a collage. E.g. https://stackoverflow.com/questions/10397842/php-create-one-image-from-images

I want to keep such images up to date, how do I do this without bogging down load on my server?

You could use WP Cron with (daily) schedule as @kero suggested to update the og image. Another way could be to hook custom function save_post action and update the image when a new post is published and has tag(s) and a featured image set.

With cron you may want to disable the default WP cron and schedule a real cronjob on your server so that the image update function runs even when there are no visitors on your site.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.