phpwordpressgd

Merging images with GDLibrary


I hope you can help me. I am trying to write a function for wordpress that will merge an uploaded image with another image saved to the theme directory. Essentially, watermarking. I'm nearly there, but I am having trouble with the GD imagecreatefrompng(); method, which should work doesn't. I've tried imagecreatefromjpeg and imagecreatefromgif, and those also don't seem to work. Any idea what I am doing wrong?

add_action('after_setup_theme','themename_bw_size');
function themename_bw_size() {
  add_image_size('themename-bw-image', 500, 500, true);
}

add_filter('wp_generate_attachment_metadata','themename_bw_filter');
function themename_bw_filter($meta) {
  $file = wp_upload_dir();
  $file = trailingslashit($file['path']).$meta['sizes']['themename-bw-image']['file'];
  list($orig_w, $orig_h, $orig_type) = @getimagesize($file);
  $image = wp_load_image($file);


  // This works. Such work.
  $stamp = imagecreatetruecolor( 100, 100);
  imagefilledrectangle($stamp, 0, 0, 99, 69, 0x0000FF);
  imagefilledrectangle($stamp, 9, 9, 90, 60, 0xFFFFFF);
  imagestring($stamp, 5, 20, 20, 'GDLibrary', 0x0000FF);
  imagestring($stamp, 3, 20, 40, 'Sometimesworks', 0x0000FF);

  // This doesn't work. Oh such want.
  //$stamp = imagecreatefrompng(get_stylesheet_directory() . '/images/test.png);


  imagecopymerge($image, $stamp, 150, 150, 0, 0, 200, 200, 100); //have to play with these numbers for it to work for you, etc.

  switch ($orig_type) {
    case IMAGETYPE_GIF:
      imagegif( $image, $file );
      break;
    case IMAGETYPE_PNG:
      imagepng( $image, $file );
      break;
    case IMAGETYPE_JPEG:
      imagejpeg( $image, $file );
      break;
  }
  return $meta;
}

Solution

  • resolved this. get_stylesheet_directory() needed "/assets/". Strange but true.