Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #4287
    nic mare
    Participant

    I already use a nice script which grabs me youtube screenshots with a CPT called “videos” and a field with the youtube id. what it does it calls the youtube image and saves it to the media library of WP and inserts the image as a featured image to the CPT:

    /* GRAB YOUTUBE SCREENSHOT: */
    
    function set_youtube_as_featured_image($post_id) { 
    	$slug = 'videos';
    	if ( $slug != $_POST['post_type'] ) {
            return;
        }
      if(  has_post_thumbnail($post_id) ) return;
    
      $ytid = get_post_meta($post_id, 'youtube_id', true);
    
      if ( $ytid ) {
    
        // getting thumb url from video url
        
        $youtube_thumb_url = 'http://i1.ytimg.com/vi/' . $ytid . '//cdn.dfactory.co/0.jpg';
    
        // download and save thumb
        $get = wp_remote_get( $youtube_thumb_url );
        $mime_type = wp_remote_retrieve_header( $get, 'content-type' );
        if ( ! substr_count($mime_type, 'image') ) return false;
        $name = 'youtube-thumb-post-' . $post_id . '.jpg';
        $bits = wp_upload_bits( $name, '', wp_remote_retrieve_body( $get ) );
        if ( $bits['error'] ) return false;
    
        // save attachment post, and setting as post thumbnails
        $thumb_data = array(
          'post_title'=> 'Youtube Preview', 'post_mime_type' => $mime_type
        );    
        $thumbnail_id  = wp_insert_attachment( $thumb_data, $bits['file'], $post_id );
        if ( $thumbnail_id ) {
          require_once(ABSPATH . 'wp-admin/includes/image.php');
          $metadata = wp_generate_attachment_metadata( $thumbnail_id, $bits['file'] );
          
          if (wp_update_attachment_metadata( $thumbnail_id,  $metadata )) {
    		return update_post_meta($post_id, '_thumbnail_id', $thumbnail_id);
    	  }
        }
    
      }
    }
    
    add_action('save_post', 'set_youtube_as_featured_image');

    But the uploaded images don’t get watermarked!
    Your Watermark Plugin just works as long as i go to media, select the images and apply the watermark.
    but i like to have it automatically! you write, it should happen as long you use the default wordpress api functions. so i use them but the watermarking still does not work? what do i miss here?
    Thank you!

    #5922
    nic mare
    Participant

    found a solution. add these two lines:
    $IW = new Image_Watermark;
    $IW->do_watermark($bits[‘file’]);

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.