Support » Plugin: WP-Stateless - Google Cloud Storage » What if I use add_filter(‘wp_generate_attachment_metadata’,’my_func’, 10, 2)?

  • I have custom made convertion of the image upon upload to Media Library.

    It adds another “png” image as a watermark over the original one.

    Moreover, it converts .jpg to .webp image. – makes a copy and then exec() the command “cwebp”, after that imagedestory() and finally wp_update_attachment_metadata($id, $meta).

    I tried to enable “Dynamic Image Support” – Upload image thumbnails generated by your theme and plugins that do not register media objects with the media library.

    But it uploads only the original one image within the sizes, but the modified original image with watermark, and also new created image same filename but with .webp extension is not uploaded.

    The code is below:
    // vodeni žig
    function water_mark($meta, $id){

    if(!isset($meta[‘sizes’])) {
    return $meta[‘full’];
    }

    $upload_path = wp_upload_dir();
    $path = $upload_path[‘basedir’];

    // media upload direktorij
    if(isset($path)){
    $file = trailingslashit($upload_path[‘basedir’].’/’).$meta[‘file’];
    //$target = trailingslashit($upload_path[‘basedir’].’/’).$meta[‘sizes’][‘slider-thumb’][‘file’];
    $water_path = trailingslashit($upload_path[‘basedir’].’/’).’watermark.png’;
    }else{
    $file = trailingslashit($upload_path[‘path’]).$meta[‘file’];
    //$target = trailingslashit($upload_path[‘path’]).$meta[‘sizes’][‘slider-thumb’][‘file’];
    $water_path = trailingslashit($upload_path[‘path’]).’watermark.png’;
    }

    //list($istaknuta_w, $istaknuta_h, $istaknuta_type) = @getimagesize($target);

    // dimenzije fotografije
    list($orig_w, $orig_h, $orig_type) = @getimagesize($file);
    if($orig_w!=’630′ && $orig_h!=’355′){

    // učitaj vodeni žig i dimenzije
    $watermark = imagecreatefrompng($water_path);
    list($wm_width, $wm_height, $wm_type) = @getimagesize($water_path);

    // url fotografije
    $image = wp_load_image($file);

    // kopija i zamjena full veličine
    imagecopy($image, $watermark, $orig_w – ($wm_width + 10), $orig_h – ($wm_height + 10), 0, 0, $wm_width, $wm_height);

    // spremi
    switch ($orig_type) {
    //case IMAGETYPE_GIF:
    //imagegif($image, $file );
    //break;
    case IMAGETYPE_PNG:
    imagepng($image, $file );
    // webp 2018
    $slika_ekstenzija_png = preg_replace(‘/\\.[^.\\s]{3,4}$/’, ”, $file);
    exec(“cwebp “.$file.” -o “.$slika_ekstenzija_png.”.webp”);
    exec(“cwebp “.$slika_ekstenzija_png.”-630×355.png”.” -o “.$slika_ekstenzija_png.”-630×355.webp”);
    break;
    case IMAGETYPE_JPEG:
    imagejpeg($image, $file, 80);
    // webp 2018
    $slika_ekstenzija_jpg = preg_replace(‘/\\.[^.\\s]{3,4}$/’, ”, $file);
    exec(“cwebp “.$file.” -o “.$slika_ekstenzija_jpg.”.webp”);
    // ak je webp prevelki, briši ga
    exec(“cwebp “.$slika_ekstenzija_jpg.”-630×355.jpg”.” -o “.$slika_ekstenzija_jpg.”-630×355.webp”);
    break;
    }

    imagedestroy($watermark);
    imagedestroy($image);

    wp_update_attachment_metadata($id, $meta);
    return $meta;

    }else{

    switch ($orig_type) {
    case IMAGETYPE_PNG:
    $slika_ekstenzija_png = preg_replace(‘/\\.[^.\\s]{3,4}$/’, ”, $file);
    exec(“cwebp “.$file.” -o “.$slika_ekstenzija_png.”-630×355.webp”);
    break;
    case IMAGETYPE_JPEG:
    $slika_ekstenzija_jpg = preg_replace(‘/\\.[^.\\s]{3,4}$/’, ”, $file);
    exec(“cwebp “.$file.” -o “.$slika_ekstenzija_jpg.”-630×355.webp”);
    break;
    }

    // return
    wp_update_attachment_metadata($id, $meta);
    return $meta;

    }
    }
    add_filter(‘wp_generate_attachment_metadata’,’water_mark’, 10, 2);

    Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Maybe something to do with class-utility.php?

    /* Get metadata in case if method is called directly. */
    if( current_filter() !== ‘wp_generate_attachment_metadata’ && current_filter() !== ‘wp_update_attachment_metadata’ ) {
    $metadata = wp_get_attachment_metadata( $attachment_id );
    }

    Does this line skips the add_filter() in my functions.php, which makes a copy or originall image, then to the full-size image adds watermark, and converts the specific one to webp as well as full-size one?

    Thanks!

    Even maybe better to modify the dynamic-image-support.php?

    Does it work with exec()?

    Like:
    exec(“gsutil cp file gs://bucket > /dev/null 2>&1”);
    or
    exec(“gsutil cp file gs://bucket”);

    Anyone tested?

    Not working.

    So, how to make this plugin to:
    a) upload to bucket the new image with watermar added, same filename and extension .jpg, after we have uploaded it to WP Media Library?
    b) the same, just extension is .webp

    I believe it has to be easy, somehow because the plugin works great.

    Thanks!

    On the fly does not suppose to find newly created image as filename.webp, after the uploaded to media, so “on the fly” compatibility does not work either?
    – regarding the above code added to the functions.php

    Requirements: SSH on Linux OS with webp library installed (apt-get)

    Plugin Author Alim

    (@alimuzzamanalim)

    Hi @fritex,

    Thanks for contacting us. Here is a couple of solution I can think of.

    1. You can use this action to sync your WEBP to GCS.

    
    // Generate google storage path from local path.
    $gs_name = apply_filters('wp_stateless_file_name', $local_file_path);
    // sync the file.
    do_action('sm:sync::syncFile', $gs_name, $local_file_path, true);
    

    2. You can try to add your WEBP size to the attachment meta size array. Then WP Stateless should automatically sync the image. As it will think of them as default wp sizes.

    Dear Alim, thanks for reply and provided information.

    Will try.

    Morover, regarding your 1st statement, where should I put this code? To my functions.php or I should modify the plugin files?

    The add_filter(‘wp_generate_attachment_metadata’,’water_mark’, 10, 2); code is somehow skipped, or the plugin just fires and uploads images, and this filter is somehow done after it, right?
    That’s the way why the plugin cannot recognize the .webp, event the modified .jpg with watermark, to upload them?

    Regarding your 2nd statement, I do not know if there is possibility for WordPress similar like add_image_size() to convert to .webp, so the size of this image with .webp extension would be automatically added to the sizes array() which you are saying, right?

    I do not think I know how to do it, so I will have to Google around about the action and/or filter from the Official Documentation, how to add it as webp size to the attachment meta size array while on upload.

    Kindly, if there are any other ideas or possible solutions, appreciate and thank you very much for provided answers and help!

    Basically, the original image after upload is modified and on it is placed the watermark.png logo image, and the new one is created – the same filename.
    After it, this new one with watermark on it is converted to webp.

    So, after we upload some image to WP Media Librabry, my code above does next:
    a) creates a new image from the uploaded one
    b) adds the “watermark logo” .png to the specific position of the uploaded image (full-size, original)
    c) this new image with watermark logo is saved (overrides/overwrites the uploaded one – original)
    d) the same image is converted to .webp image
    e) so at the end we have the modified original uploaded image with watermark on it and the .webp version of it too (full sized both)

    Sorry for too much text, but as I did read it again, it did not sound so good for me to understand what the code does and what I wanted as an explanation of it.

    Your suggestions help me a lot!

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