• Resolved Chris Huff

    (@brochris)


    I’m trying to open up my plugin so that I can also create extensions for it. My plugin generates an image, so I need to be able to pass the resource of the image to the extension plugin, which will continue to generate the image in some way, and then when it gets done, the main plugin will finish it and save it.

    So I added an action at one point of the main plugin.

    do_action('afift_after_write_text');

    Which works. The action fires, and the function I created in the extension plugin fires when I hook it to that action.

    But now I’m having a hard time passing the image resource to the extension plugin. My best try was to write it to the database first, and then reference that, but it rightly told me that it was a resource when it expected a string. Here’s some (relevant) code.

    Main plugin:

    function auto_featured_image_from_title ($post_id) {
    
        $post = get_post( $post_id );
        $new_featured_img = imagecreatetruecolor($auto_image_width, $auto_image_height);
        update_option('new_featured_img', $new_featured_img);
    
        do_action('afift_after_image_created');
    
        // Save the image
        $newimg = $upload_dir['path'] . '/' . $post_slug . '.jpg';
        imagejpeg( $new_featured_img, $newimg );
        }

    Extension plugin:

    function write_text($post_id){
    
        $post = get_post( $post_id );
        $new_featured_img = get_option('new_featured_img');
        $text = 'Hello, my friend.';
        imagettftext($new_featured_img, 20, 0, 20, 20, $text_color, $font, $text);
        }

    Then, of course, I want the new $new_featured_img, to be magically passed back to the main plugin to be saved. About to pull my hair out. Any help would be greatly appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Ryan

    (@ryanmclaughlin1)

    Chris – take a look at apply_filters, which essentially allows you to pass values back and forth, instead of do_action. it might look something like this:

    function auto_featured_image_from_title ($post_id) {
        // $auto_image_width = something
        // $auto_image_height = something
        $post = get_post($post_id);
        $new_featured_img = imagecreatetruecolor($auto_image_width, $auto_image_height);
    
        $new_featured_img = apply_filters('afift_after_image_created', $new_featured_img, $post_id);
    
        $newimg = $upload_dir['path'] . '/' . $post_slug . '.jpg';
        imagejpeg( $new_featured_img, $newimg );
    }
    function write_text($img, $post_id){
        // $post_id is available if you need it for anything...
        // $text_color = something
        // $font = something
        $text = 'Hello, my friend.';
        return imagettftext($img, 20, 0, 20, 20, $text_color, $font, $text);
    }

    hope it helps!

    add_filter('afift_after_image_created', 'write_text', 10, 2);

    Thread Starter Chris Huff

    (@brochris)

    Thank you so much!!! Exactly what I need to do!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Make an image resource available to another plugin’ is closed to new replies.