将图像与GDLibrary合并

时间:2016-05-18 17:57:21

标签: php wordpress gd

我希望你能帮助我。我正在尝试为wordpress编写一个函数,它将上传的图像与保存到主题目录的另一个图像合并。基本上,水印。我差不多了,但是我在使用GD imagecreatefrompng()时遇到了麻烦;方法,应该工作不。我已经尝试过imagecreatefromjpeg和imagecreatefromgif,这些似乎也不起作用。知道我做错了吗?

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;
}

1 个答案:

答案 0 :(得分:0)

解决了这个问题。 get_stylesheet_directory()需要“/ assets /”。奇怪但真实。