Skip to content

Instantly share code, notes, and snippets.

@tomazzaman
Created April 7, 2015 19:30
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomazzaman/e03effaa2a450aef0283 to your computer and use it in GitHub Desktop.
Save tomazzaman/e03effaa2a450aef0283 to your computer and use it in GitHub Desktop.
Watermarking uploads in WordPress with Imagemagick
function register_watermarked_size() {
add_image_size( 'watermarked', 550, 550, true ); // This is what should be uploaded
}
add_action( 'init', 'register_watermarked_size' );
class Watermark_Image {
// The attachment meta array
public $meta = array();
// The upload dir (including year and month)
private $upload_dir = '';
// The path to the uploaded and unprocessed file
private $uploaded_file_path = '';
public function __construct($meta) {
$this->meta = $meta;
$this->generate();
}
public function get_meta() {
return $this->meta;
}
public function generate() {
$time = substr( $this->meta['file'], 0, 7 );
$this->upload_dir = wp_upload_dir( $time );
$filename = $this->meta['sizes']['watermarked']['file'];
$this->uploaded_file_path = trailingslashit( $this->upload_dir['path'] ) . $filename;
$this->watermark_image();
}
private function watermark_image() {
$image_resource = new Imagick( $this->uploaded_file_path );
$image_resource->blurImage( 20, 10 );
$watermark_resource = new Imagick( get_stylesheet_directory() . '/images/codeable-logo.png' );
$image_resource->compositeImage( $watermark_resource, Imagick::COMPOSITE_DEFAULT, 100, 250 );
$this->save_image( $image_resource );
}
private function save_image( $image_resource ) {
$image_data = pathinfo( $this->uploaded_file_path );
$watermarked_filename = $image_data['filename'] . '-watermarked.' . $image_data['extension'];
$watermarked_file_path = str_replace( $image_data['basename'], $watermarked_filename, $this->uploaded_file_path );
if ( ! $image_resource->writeImage( $watermarked_file_path ) )
return $image_data['basename'];
unlink( $this->uploaded_file_path ); // Because that file isn't referred to anymore
$this->meta['sizes']['watermarked']['file'] = $watermarked_filename;
}
}
function generate_watermarked_image( $meta ) {
$watermark_image = new Watermark_Image( $meta );
$meta = $watermark_image->get_meta();
return $meta;
}
add_filter( 'wp_generate_attachment_metadata', 'generate_watermarked_image' );
function register_watermarked_size() {
add_image_size( 'watermarked', 550, 550, true ); // This is what should be uploaded
}
add_action( 'init', 'register_watermarked_size' );
function generate_watermarked_image( $meta ) {
$time = substr( $meta['file'], 0, 7); // Extract the date in form "2015/04"
$upload_dir = wp_upload_dir( $time ); // Get the "proper" upload dir
$filename = $meta['sizes']['watermarked']['file'];
$meta['sizes']['watermarked']['file'] = watermark_image( $filename, $upload_dir );
return $meta;
}
add_filter( 'wp_generate_attachment_metadata', 'generate_watermarked_image' );
function watermark_image( $filename, $upload_dir ) {
$original_image_path = trailingslashit( $upload_dir['path'] ) . $filename;
$image_resource = new Imagick( $original_image_path );
$image_resource->blurImage( 20, 10 );
$watermark_resource = new Imagick( get_stylesheet_directory() . '/images/codeable-logo.png' );
$image_resource->compositeImage( $watermark_resource, Imagick::COMPOSITE_DEFAULT, 100, 250 );
return save_watermarked_image( $image_resource, $original_image_path );
}
function save_watermarked_image( $image_resource, $original_image_path ) {
$image_data = pathinfo( $original_image_path );
$new_filename = $image_data['filename'] . '-watermarked.' . $image_data['extension'];
$watermarked_image_path = str_replace($image_data['basename'], $new_filename, $original_image_path);
if ( ! $image_resource->writeImage( $watermarked_image_path ) )
return $image_data['basename'];
unlink( $original_image_path ); // Because that file isn't referred to anymore
return $new_filename;
}
@drewbaker
Copy link

I have a StackOverflow question about how to improve this here: http://stackoverflow.com/questions/34377231/wordpress-blur-image-on-upload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment