Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #3457
    Tim Scullin
    Participant

    Hey there,

    I have the add front end image watermark option selected, but it wont add the watermark if I add from a front end script. If I add from the admin the watermark is added..

    You say ‘This functionality works only if uploaded images are processed using WordPress native upload methods’.. What are the ‘native upload methods’ for front end theme uploads?

    My current upload code:

    $upload_dir = wp_upload_dir(); // Set upload folder
    $image_data = file_get_contents($image_url); // Get image data
    $filename   = $seoTitle; // Create image file name
    
    // Check folder permission and define file location
    if( wp_mkdir_p( $upload_dir['path'] ) ) {
        $file = $upload_dir['path'] . '/' . $filename;
    } else {
        $file = $upload_dir['basedir'] . '/' . $filename;
    }
    
    // Create the image  file on the server
    file_put_contents( $file, $image_data );
    
    // Check image file type
    $wp_filetype = wp_check_filetype( $filename, null );
    
    // Set attachment data
    $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_title'     => sanitize_file_name( $filename ),
        'post_content'   => '',
        'post_status'    => 'inherit'
    );
    
    // Create the attachment
    $attach_id = wp_insert_attachment( $attachment, $file, $new_post_id );
    
    // Include image.php
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    
    // Define attachment metadata
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
    
    // Assign metadata to attachment
    wp_update_attachment_metadata( $attach_id, $attach_data );
    
    // And finally assign featured image to post
    set_post_thumbnail( $new_post_id, $attach_id );
    

    Thanks for you help,
    Tim

    #3458
    Bartosz
    Keymaster

    Tim, try to modify your code to use WordPress wp_handle_upload (http://codex.wordpress.org/Function_Reference/wp_handle_upload) or media_handle_upload (http://codex.wordpress.org/Function_Reference/media_handle_upload) functions instead.

    Image Watermark hooks into those to apply watermark to uploaded images. Besides, this would also be more secure method.

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