• This topic has 6 replies, 3 voices, and was last updated 3 years ago by Beee.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #11624
    Jack0
    Participant

    Hello folks,
    since I can’t use the default wp upload native method, I’m trying to do watermark on featured images of a certain post type, I think the right solution is creating a custom function that runs when posts will be saved. Do you have any suggestions or examples for doing that?

    #11625
    Bartosz
    Keymaster

    We don’t have such examples unfortunatelly.

    What you can do however it to experiment with IW do_watermark() public method in conjunction with action(s) triggered when setting post thumbnail (it will not be save post, but update_post_meta probably: http://wordpress.stackexchange.com/questions/76693/which-action-hook-can-i-use-when-a-featured-image-has-been-selected)

    The do_watermark() is a public method that requires a couple of parameters, which are handled automatically in the plugin enviroment, but will have to be set properly if you use it manually:

    /**
     * Apply watermark to image.
     *
     * @param	int		$attachment_id	Attachment ID
     * @param	string	$image_path		Path to the file
     * @param	string	$image_size		Image size
     * @param	array	$upload_dir		Upload media data
     * @return	void
     */
    public function do_watermark( $attachment_id, $image_path, $image_size, $upload_dir ) {

    Thats the direction I would follow, untested unfortunatelly.

    #11631
    Jack0
    Participant
    
    function stampa_watermark () {
      $slug = 'foto';
      $post_id = get_the_ID();
      $post_type = get_post_type($post_id);
      $attachment_id = get_post_thumbnail_id( $post_id, 'full' );
      $image_size = 'full';
      $filepath = get_attached_file( $attachment_id );
      $upload_dir = wp_upload_dir();
    
      if ( $slug != $post_type ) {
            return;
        }
      else {
        $IW = new Image_Watermark;
        $IW->do_watermark( $attachment_id, $filepath, $image_size, $upload_dir );
          
        }
    }
    
    add_action( 'updated_postmeta', 'stampa_watermark' );
    
    

    Thanks for the quick reply, something works well, at the moment I’m able to insert the watermark only on full size image because I need to found a solution for get multiple sizes like as do_watermark function on your core. But the big issue is that function works only if I manually update the post on back-end area!

    #11635
    Jack0
    Participant

    I have found a solution, I insert do_watermark(); directly where uploads made, without any callback function or action. Now I need to found a method for apply watermark to some thumbnail size, I’m trying with the following code, but doesn’t works!

    
    $filepath = get_attached_file( $attachment_id );
    $upload_dir = wp_upload_dir();
    $data = wp_get_attachment_metadata( $post_id, false );
    $array_size = array('full' => 1,'vertical' => 2,'orizzontale' => 3, 'quadrata' => 4 );
    $IW = new Image_Watermark;
    
     foreach ( $array_size as $image_size => $active_size ) {
     if ( $active_size === 1 ) {
      switch ( $image_size ) {
       case 'full':
       $filepath = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . $data['file'];
       break;
    
       default:
       if ( ! empty( $data['sizes'] ) && array_key_exists( $image_size, $data['sizes'] ) )
       $filepath = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . dirname( $data['file'] ) . DIRECTORY_SEPARATOR . $data['sizes'][$image_size]['file'];
         else
        // early getaway
        continue 2;
       }
    
         do_action( 'iw_before_apply_watermark' );
    
        // apply watermark
        $IW->do_watermark( $attachment_id, $filepath, $image_size, $upload_dir );
    
         do_action( 'iw_after_apply_watermark' );
       }
    }
    

    it works only with full size and the following code:

    
     $image_size = 'full';
     $filepath = get_attached_file( $attachment_id );
     $upload_dir = wp_upload_dir();
     $IW = new Image_Watermark;
     $IW->do_watermark( $attachment_id, $filepath, $image_size, $upload_dir );
    
    #11638
    Jack0
    Participant

    SOLUTION FOUND

    functions.php

    
    function scaled_image_path($attachment_id, $size = 'thumbnail') {
    $file = get_attached_file($attachment_id, true);
    if (empty($size) || $size === 'full') {
    // for the original size get_attached_file is fine
    return realpath($file);
    }
    if (! wp_attachment_is_image($attachment_id) ) {
    return false; // the id is not referring to a media
    }
    $info = image_get_intermediate_size($attachment_id, $size);
    if (!is_array($info) || ! isset($info['file'])) {
    return false; // probably a bad size argument
    }
    
    return realpath(str_replace(wp_basename($file), $info['file'], $file));
    }
    
    

    uploader_file.php
    I have insert the following code after a set_featured_image()

    
    $upload_dir = wp_upload_dir();
    $IW = new Image_Watermark;
    
    $image_size = 'full';
    $filepath = get_attached_file( $attachment_id );
    $IW->do_watermark( $attachment_id, $filepath, $image_size, $upload_dir );
    
    $image_size = 'verticale';
    $filepath = scaled_image_path($attachment_id,$image_size);
    $IW->do_watermark( $attachment_id, $filepath, $image_size, $upload_dir );
    
    $image_size = 'quadrata';
    $filepath = scaled_image_path($attachment_id,$image_size);
    $IW->do_watermark( $attachment_id, $filepath, $image_size, $upload_dir );
    
    $image_size = 'rettangolare';
    $filepath = scaled_image_path($attachment_id,$image_size);
    $IW->do_watermark( $attachment_id, $filepath, $image_size, $upload_dir );
    
    $image_size = 'thumbnail';
    $filepath = scaled_image_path($attachment_id,$image_size);
    $IW->do_watermark( $attachment_id, $filepath, $image_size, $upload_dir );
    
    $image_size = 'medium';
    $filepath = scaled_image_path($attachment_id,$image_size);
    $IW->do_watermark( $attachment_id, $filepath, $image_size, $upload_dir );	
    
    $image_size = 'large';
    $filepath = scaled_image_path($attachment_id,$image_size);
    $IW->do_watermark( $attachment_id, $filepath, $image_size, $upload_dir );
    
    
    #11644
    Bartosz
    Keymaster

    You can use get_intermediate_image_sizes WordPress function https://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes to get registered image sizes and create a loop to do that, it would be simple and more elegant.

    #24206
    Beee
    Participant

    Thnq Jack0 for looking into this… This helps a lot…

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