Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Here's the deal: My plugin automatically watermarks images from the WP gallery on the fly using the GD library wherever these images appear. So, I have a physical copy (in wp-uploads) of these attachments, and also a watermarked, dynamic copy of the image which I replace the image source on the page using Simple HTML DOM.

So now, I have an image on my page that points to a URL like this:

http://localhost/wp-content/plugins/pluginname/image.php?original=http%3A%2F%2Flocalhost%2Fwp-content%2Fuploads%2F2012%2F09%2FPenguins-300x225.jpg&username=&wmtext=localhost&wmsize=14&wmcolor=%23000000&wmpos=top-center&pin_hoveron=1&pin_hoverimg=http%3A%2F%2Flocalhost%2Fwp-content%2Fplugins%2Fpluginname%2Fadd-to-etc.png&followimg=&pin_on=1&follow_on=1

What I want to do is make the image source APPEAR like it is the original version, but display the dynamic image instead. I've attempted to mess around with rewrite rules, but it never seems to do anything, no matter what I try. The rules get added to .htaccess, but nothing changes. Here's what I was messing around with:

add_action('generate_rewrite_rules', array( $this, 'addRewrites' ) );

public function addRewrites( $content ) {
    global $wp_rewrite;
    $new_rules = array(
        'img/(.*)'      => plugins_url( 'image.php', __FILE__ ) . '?etc=$1',
    );
    $wp_rewrite->non_wp_rules += $new_rules;
}

Now if I visit an image URL or a page with an image, NOTHING happens. =/ I'm a newbie when it comes to rewrite rules, so this is a bit beyond me.

share|improve this question
    
Why not just generate watermarked images statically? Piping images through PHP has kinda poor track record in WP context... –  Rarst Nov 18 '12 at 15:18
    
I could watermark the images while they are being uploaded, but then every image that was uploaded before the plugin was active won't be changed at all. I'm trying to make this plugin a solution for all images. –  Jared Nov 18 '12 at 15:34
add comment

2 Answers 2

I am not sure about complete scope of your requirements, but I would probably:

  1. Register special image size(s) that would be used instead of original.
  2. Tie watermarking to generation of that size.

This way there is no need for dynamic image processing. As for your concern about older images having no watermark - you will be able to (re)generate sizes for them using one of the plugins floating around (that is quite common operation).

share|improve this answer
add comment
up vote 0 down vote accepted

I ended up changing my code to this, which is what I needed:

Hook in:

add_action( 'generate_rewrite_rules', array( $this, 'addRewrites' ) );

Create the rules:

public function addRewrites( $content ) {
    global $wp_rewrite;

    $plugin_path = substr( str_replace( home_url(), '', plugins_url( '', __FILE__ ) ), 1 );
    $upload_path = substr( str_replace( home_url(), '', wp_upload_dir()['baseurl'] ), 1 );

    $new_rules = array(
        $upload_path . '/*(.+\.jpg)$' => $plugin_path . '/image.php?original=$1',
        $upload_path . '/*(.+\.gif)$' => $plugin_path . '/image.php?original=$1',
        $upload_path . '/*(.+\.png)$' => $plugin_path . '/image.php?original=$1',
    );

    $wp_rewrite->non_wp_rules += $new_rules;
}

And this only does images from the uploads folder so it is pretty safe I believe.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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