Issues with wordpress watermark

Question

I’m adding a watermark to a WordPress featured image of a post submitted by me by clicking on a button on the front end which is visible only to the person who submitted that post. That button calls a function that applies the watermark, but I’m having these issues:

1.the watermark is being applied only to the full image of the post and not the other sizes, I’m passing only the full image id, how can i apply the watermark to the other sizes?

2.I don’t have any cache plugin and I also disabled caching in wp-config but when I click the button to apply the watermark, I’m unable to see the result right away. It takes longer for the poster to see the watermark on the image, The only way I’m seeing it quickly after being applied is if I use an incognito browser or another device, How can I solve this?

Here is the function I’m using :

function water_mark($im) {   
    
    $id=$im;
    $meta = wp_get_attachment_metadata( $id, $unfiltered = false );

    if(!isset($meta['sizes'])) {
        return $meta;
    }   

    $upload_path = wp_upload_dir();     
    $path = $upload_path['basedir'];

    //handle the different media upload directory structures
    if(isset($path)){       
        $file = trailingslashit($upload_path['basedir'].'/').$meta['file'];
        $water_path = trailingslashit($upload_path['basedir'].'/').'watermark.png';
    }else{
        $file = trailingslashit($upload_path['path']).$meta['file'];
        $water_path = trailingslashit($upload_path['path']).'watermark.png';
    }

    //list original image dimensions
    list($orig_w, $orig_h, $orig_type) = @getimagesize($file);

    //load watermark - list its dimensions
    $watermark = imagecreatefrompng($water_path);
    list($wm_width, $wm_height, $wm_type) = @getimagesize($water_path); 
    //if your watermark is a transparent png uncomment below
    //imagealphablending($watermark, 1);

    //load fullsize image
    $image = wp_load_image($file);
    //if your watermark is a transparent png uncomment below
    //imagealphablending($image, 1);

    //greyscale image
    //imagefilter($image, IMG_FILTER_GRAYSCALE);

    //create merged copy
    //if your watermark is a transparent png uncomment below
    //imagecopy($image, $watermark, $orig_w - ($wm_width + 10), $orig_h - ($wm_height + 10), 0, 0, $wm_width, $wm_height);

    //if your watermark is a transparent png comment out below
    imagecopymerge($image, $watermark, $orig_w - ($wm_width + 10), $orig_h - ($wm_height + 10), 0, 0, $wm_width, $wm_height, 70);

    //save image backout
    switch ($orig_type) {
        case IMAGETYPE_GIF:
            imagegif($image, $file );
            
            break;
        case IMAGETYPE_PNG:
            imagepng($image, $file, 10 );
            
            break;
        case IMAGETYPE_JPEG:
            imagejpeg($image, $file, 95);
            
            break;
    }
    
    $image->save();
    imagedestroy($watermark);
    imagedestroy($image);

    //return metadata info
    wp_update_attachment_metadata($id, $meta);
    return $meta;

}

function small($is) {   
    
    $id=$is;
    $meta = wp_get_attachment_metadata( $id, $unfiltered = false );

    if(!isset($meta['sizes'])) {
        return $meta;
    }   

    $upload_path = wp_upload_dir();     
    $path = $upload_path['basedir'];

    //handle the different media upload directory structures
    if(isset($path)){       
        $file = trailingslashit($upload_path['basedir'].'/').$meta['file'];
        $water_path = trailingslashit($upload_path['basedir'].'/').'watermark.png';
    }else{
        $file = trailingslashit($upload_path['path']).$meta['file'];
        $water_path = trailingslashit($upload_path['path']).'watermark.png';
    }

    //list original image dimensions
    list($orig_w, $orig_h, $orig_type) = @getimagesize($file);

    //load watermark - list its dimensions
    $watermark = imagecreatefrompng($water_path);
    list($wm_width, $wm_height, $wm_type) = @getimagesize($water_path); 
    //if your watermark is a transparent png uncomment below
    //imagealphablending($watermark, 1);

    //load fullsize image
    $image = wp_load_image($file);
    //if your watermark is a transparent png uncomment below
    //imagealphablending($image, 1);

    //greyscale image
    //imagefilter($image, IMG_FILTER_GRAYSCALE);

    //create merged copy
    //if your watermark is a transparent png uncomment below
    //imagecopy($image, $watermark, $orig_w - ($wm_width + 10), $orig_h - ($wm_height + 10), 0, 0, $wm_width, $wm_height);

    //if your watermark is a transparent png comment out below
    imagecopymerge($image, $watermark, $orig_w - ($wm_width + 10), $orig_h - ($wm_height + 10), 0, 0, $wm_width, $wm_height, 70);

    //save image backout
    switch ($orig_type) {
        case IMAGETYPE_GIF:
            imagegif($image, $file );
            
            break;
        case IMAGETYPE_PNG:
            imagepng($image, $file, 10 );
            
            break;
        case IMAGETYPE_JPEG:
            imagejpeg($image, $file, 95);
            
            break;
    }

    imagedestroy($watermark);
    imagedestroy($image);

    //return metadata info
    wp_update_attachment_metadata($id, $meta);
    return $meta;
}

I apologize if there have been any spelling mistakes as English is not my first language.

Thank you and happy holidays

0
devin 4 weeks 0 Answers 3 views 0

Leave an answer