Jump to content
jacmaes

Webp support

Recommended Posts

Should webp extra image get deleted if you replace the original image? That is: upload an image, thumb is generated, then create webp extra by using $myimge->webpurl, then in the admin you replace the image by uploading with the same name eg. replace img1.jpg with img1.jpg. Original image is replaced, thumb is recreated, but webp stay the same. This is how it should be?

webp.gif

  • Like 1

Share this post


Link to post
Share on other sites

Please file a github issue (with the pw version)

Share this post


Link to post
Share on other sites

I first time used $config->pagefileSecure = true; option in config.php. When I echo the image webp extra like this in the template:

echo "<a href='{$img->webpUrl}'>Click to view image</a> ";

and then click on the link, the image can be viewed in the browser, as expected. But when I set pagefileSecure to true, reload the page and click on the link, the image is downloaded. I can see the warning in Chrome console:

Resource interpreted as Document but transferred with MIME type application/octet-stream: "http://localhost/site/assets/files/1015/nature1.webp". 

Also, headers changed to:

Content-disposition: attachment; filename="nature1.webp"
Content-transfer-encoding: binary
Content-Type: application/octet-stream
 
Is this expected or maybe something wrong with my webserver config (IIS)? I didn't change access permissions on the template and also viewing the image logged as admin user.

Share this post


Link to post
Share on other sites

Hello to all mighty people and Happy New decade start. Wish you all the greatest goodness, joy and happiness. Well, and a lot of great code as well 😉

I've implemented WEBP images to optimize my soon to be released profile and by a lucky coincidence I discovered on my Mac that no images are showing when using the Safari. I thought that Apple would have embraced the idea by now when several other browsers have already done that but it seemed like I was wrong.

I tested the .htaccess approach, however I could not make it working under Safari, so I decided to add a little function to my _functions.php:

//WEBP Image Support
function webp_support($imgURL)
{
    // Check if the browser headers contain image/webp support
    if(strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')) { 

        // If yes, use the webp image url
        return $imgURL->webp;
    
    } else {

        //Else, show the original image url
        return $imgURL;

    }
}

After that, in every image call I do the following:

<?php
$img = $page->images->first // Call for the image location
$image = webp_support($img->size(100,100)) // Used to size an image and convert it to WEBP

//Image call in the markup
echo "<img src='{$image->url}' alt='{$image->description}' >";

That seems to be working fine and show the WEBP format on Chrome, Opera and Firefox under MacOS/Windows/Linux but show the original image version under Safari. I did not test with the Windows version of Safari since it is a long time not updated version and it won't make much sense to support the newer format anyway.

My question for you is if there is a simpler approach than my current to show WEBP images to the supported browsers and fallback to original If not?

  • Like 2

Share this post


Link to post
Share on other sites

For one site I made some time ago I've tried to use the following "official" strategy:

Blogpost from Ryan / May 31, 2019

but, somehow the fallback didn't work on Safari and IE11. I didn't investigate further because I had to deliver the project and until then I've dropped that feature for now.
Your solution looks simple and elegant for me, I couldn't tell the difference against the .htaccess approach (I'm no expert by any mean).

Share this post


Link to post
Share on other sites

@MilenKo

Just a minor issue that caught my eyes: I would "cache" the webp support in eg. config.php so it shouldn't be evaluated on each function call:

$config->isWebpSupported = strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') !== false;

if($config->isWebpSupported) {
  // ...

or use a static variable inside the function to evaluate only once:

function webp_support($imgURL)
{
    static $isWebpSupported = strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') !== false;

    if($isWebpSupported) { 
    	// ...
    } else {
        // ...
    }
}

Note that I used "!== false" because if "image/webp" is at the beginning, the strpos gives 0, and it's false (not sure if it can be at 0 position, but it's a good practice to use like this imo).

(untested, haven't written PHP for months :))

  • Like 1

Share this post


Link to post
Share on other sites
2 hours ago, MilenKo said:

I thought that Apple would have embraced the idea by now when several other browsers have already done that but it seemed like I was wrong.

Just in case you haven't come across this site before: https://caniuse.com/#search=webp

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

Thanks @tpr for the static variable and "cache" approaches. I believe that after reading the @teppo detailed instructions about the different approaches, I would start using the <picture></picture> approach for new sites, however would give another try to the automatic webp conversions through .htaccess and code calls in the instructions. I have no doubt that in the next one or two versions, Apple would be having already the WEBP implemented which would simplify the things, however until then, it is good to know the best practices and most important, which way works best for us and users.

Since my profile is fully completed but it is not yet live since I am adding the content now, it won't hurt me to try all the kindly suggested approaches and see for myself which one to keep.

Btw, HAVE AN AMAZING AND PRODUCTIVE 2020 YEAR!

  • Like 1

Share this post


Link to post
Share on other sites
On 1/2/2020 at 6:05 PM, 3fingers said:

For one site I made some time ago I've tried to use the following "official" strategy:

Blogpost from Ryan / May 31, 2019

but, somehow the fallback didn't work on Safari and IE11. I didn't investigate further because I had to deliver the project and until then I've dropped that feature for now.
Your solution looks simple and elegant for me, I couldn't tell the difference against the .htaccess approach (I'm no expert by any mean).

I also had the same trouble with strategy 2. So I switched to strategy 3, wich is a little more effort but better in my opinion. 😉

Regards, Andreas

  • Like 1

Share this post


Link to post
Share on other sites

Hi,

i'm trying to add text-watermarks to images placed in a textarea using a textformatter. It works with jpegs executing the function $image->width(123)->watermark('some text') that replaces the variation with a watermarked one. But the webp variation will be created from the original file and there is no watermark. I played a bit with a hook before imSaveReady but the only thing i could achieve, was a watermark on the original file, but here i don't want it.

Has anyone an idea how i can achieve this (using processwire functions and hooks)?

Share this post


Link to post
Share on other sites

Good day!

I got a strange issue with webp images on one server. The same image works fine on Firefox and does not show (though blinks for a momдnent when trying to open it) in Chrome. I did regenerate it without luck. Then installed Imagick sizer, regenerated - same thing.

Same code works fine on local machine, generating images readable in both browsers (or serving them in correct way), so must be a server issue. But what could it be? Example image used on this page.

Please help!

  • Like 1

Share this post


Link to post
Share on other sites

I can replicate here. There must be something wrong with the image, as it opens in FF and other deskop image viewers on windows. I recreated webp from your original jpg (using online jpg to webp converter) and this time Chronme shows the image. I have no idea what could be wrong, possible Chrome issue?

  • Like 1

Share this post


Link to post
Share on other sites

Hi,

Locally I'm using Processwire 3.0.164 on MAMP 4.1.0. with ImageMagick 6.8.9.

The phpinfo lists WEBP under ImageMagick supported format. Nonetheless, ->webp->url is returning a JPG-URL for any image. Why is that?

Share this post


Link to post
Share on other sites

Do you have enabled the Imagemagick rendering engine in PW?

pw-enable-imagemagick-render-engine.thumb.jpg.ebb5185c24bda7dd70fac8eabfa031ba.jpg

You also may set strictly use the webp variation, also if the jpeg or png source is smaller in filesize. This is not often the case, but its possible.

site/config.php:

$config->webpOptions = array_merge($config->webpOptions, [
    'quality' => 84,
    'useSrcExt' => false,           // Use source file extension in webp filename? (file.jpg.webp rather than file.webp)
    'useSrcUrlOnSize' => false,     // Fallback to source file URL when webp file is larger than source?
    'useSrcUrlOnFail' => true       // Fallback to source file URL when webp file fails for some reason?
]);

 

  • Like 1

Share this post


Link to post
Share on other sites
On 8/21/2020 at 12:51 PM, horst said:

Do you have enabled the Imagemagick rendering engine in PW?

That was it. Thank you so much.

Share this post


Link to post
Share on other sites

For anyone interested in using Cloudflare CDN and a processwire webp output strategy:

 

Share this post


Link to post
Share on other sites

Hello everybody,

i am currently working on a project where i want to implement the webp support in the code (<picture>...).
But what I'm already busy with all this weekend, that PW for whatever reason doesn't always generate a webp image?

In the first screenshot (which is reproducible) no webp image is generated.

webp-problem.thumb.png.24686674aa3068496fa1ec36b5ffc1ac.png

But if I edit this original picture and change parts of it, then make a jpg out of it and upload it, it works.

webp-ok.thumb.png.d9453323a54e903e9c988678d38ab530.png

Unfortunately, this is not only the case with this picture, but with other pictures this has already happened to me on Friday 😞

Help what could be the reason for this?

-----------

The server specification from my hosting provider
ProcessWire 3.0.165
PHP version 7.4.10

gd module version 2.1.1
WebP Support - enabled

imagick module version 3.4.4
but NO entry in the supported formats!
(that's why I did not enable IMagick Image Sizer)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By fruid
      I'm using custom fields for images (as mentioned here: processwire.com/blog/posts/pw-3.0.142) for a specific images-field, the images of which need to have an HTML-caption. It works fine so far. Now I'm trying to import pages that use this field. I first exported some pages of that kind to see what the spreadsheet would look like. It seems to not separate the captions for each images but instead put all images in one cell and all captions in another.
      How could I go about importing these pages and assign the captions to the images accordingly?
    • By longtom
      I'm creating this site where a 'motherpage' renders all subpages.  I switched on frontend editing to make editing as easy as possible to end-users. 
      I'm stuck on adding images to the subpages.  The frontend editor suggests to add images to the motherpage (that has no images field on it, nor should it), so it asks for another page to add the images to/from. 
      Is there a way to make the frontend-editor context aware so that it looks for images in the correct subpage? 
      I already tried the 'type C' approach and added <edit field="body" page="id">, but that doesn't seem to do the trick. 
    • By Rossie
      Hi Everyone,
      I wish to display a gallery of images from multiple pages on the site.  These images have custom fields created through page reference called 'furniture_list_type'.  Each image now has a radio button which has been selected.
      In  the example code below all images appear from the "gallery20", however the selector  "gallery20.furniture_list_type=3390" does not have any effect.  "3390" is the id of the page reference "chair" selected through the page reference.  I wish only images selected as chair to show.
      Hope someone can help with this.
       
      Thanks,
       
      Calum
       
      $imagePages = $pages->find("template=makers-child, gallery20.furniture_list_type=3390") ;                     foreach($imagePages as $p) {     echo "<ul>";     foreach($p->gallery20 as $image) {         echo "<li><img src='{$image->url}'>{$image->furniture_list_type}</li>";     }     echo "</ul>";      }
    • By daniel_puehringer
      Hey,

      so we all know about SEO and the importance of performance. Basically we do it, because if no one finds the website we just built, it´s frustrating. We all try to write clean markup, css and js code and most might have a webpack/gulp/whatever pipeline to minimize css&js.
      But when thinking about it, optimizing your pipeline might save you a few (hundreds) of kb, compared to loading an image with 1 mb that´s literally nothing and frankly just ridiculous.

      Don´t get me wrong, frontend pipelines are great and should be used, but let´s shift your "I will optimize the shit out of that 3 css lines" focus to something different: try to serve images as fast as possible, this is where the performance boost really happens.

      I´m no pro on processwire so far, but I built a very easy to use picture element, which some of you could find interesting:

      1. the picture comes with 3 different sizes: one for mobile (keep in mind the double dpi, therefore width of 828px), one for tablet and one for desktop
      2. the picture generates a webp version and the original file extension as a fallback
      3. the filesize of each element is rendered within the "data" attribute
      4. lazy loading(sooo important!!!) is done via the native 'loading="lazy"' attribute.


      Please try it out and see the difference 🙂

      I posted this so others can easily optimize their images, but I would also like to hear your suggestions in making it better. Maybe you could decrease the rendering time or maybe you have some easy improvements.

      Please let me know.

      Greetings from Austria!


       
      <picture> <source data="<?php echo($oElement->repeater_image->width(828)->webp->filesize);?>" media="(max-width: 414px)" srcset="<?php echo($oElement->repeater_image->width(828)->webp->url) ?> 2x" type="image/webp"> <source data="<?php echo($oElement->repeater_image->width(828)->filesize) ?>" media="(max-width: 414px)" srcset="<?php echo($oElement->repeater_image->width(828)->url) ?> 2x" type="image/<?php echo($oElement->repeater_image->ext)?>"> <source data="<?php echo($oElement->repeater_image->width(767)->webp->filesize) ?>" media="(min-width: 415) and (max-width: 767px)" srcset="<?php echo($oElement->repeater_image->width(767)->webp->url) ?> 2x" type="image/webp"> <source data="<?php echo($oElement->repeater_image->width(767)->filesize) ?>" media="(min-width: 415) and (max-width: 767px)" srcset="<?php echo($oElement->repeater_image->width(767)->url) ?> 2x" type="image/<?php echo($oElement->repeater_image->ext)?>"> <source data="<?php echo($oElement->repeater_image->webp->filesize) ?>" media="(min-width: 768px)" srcset="<?php echo($oElement->repeater_image->webp->url) ?>" type="image/webp"> <source data="<?php echo($oElement->repeater_image->filesize) ?>" media="(min-width: 768px)" srcset="<?php echo($oElement->repeater_image->url) ?>" type="image/<?php echo($oElement->repeater_image->ext)?>"> <img data="<?php echo($oElement->repeater_image->filesize) ?>" class="img-fluid" loading="lazy" src="<?php echo($oElement->repeater_image->url) ?>" alt="<?php echo($oElement->repeater_image->description) ?>" type="image/<?php echo($oElement->repeater_image->ext)?>"> </picture>
    • By picarica
      so hello i am trying to combine my whole ass gallery with custom created watermark, i wanted to combine my images with simple text little opaque but that seemed harder and i couldnt make it work, well i cant make this work either, how are you handling watermarks? i would prefer if it would be plain PHP no imagemagick or some custom plugins scripts, but if neccesary i wont deny.
      so here's my code
      $pa = $pages->find("template=basic-page|art_gallery, images.tags!=''"); /* $pa = $pages->find("has_parent!=2,id!=2|7,status<".Page::statusTrash.",include=all"); */ echo "<div BRUUH class='row gtr-50 gtr-uniform js-filter' id='gal' >"; foreach ($pa as $p) { foreach($p->images as $image) { $obrazok = $image->url; $image = imagecreatefromjpeg($obrazok); $frame = imagecreatefromjpeg($pages->get('/settings/')->watermark->url); /* echo $image; echo $frame; */ # If you know your originals are of type PNG. imagecopymerge($image, $frame, 0, 0, 0, 0, 50, 50, 100); # Output straight to the browser. $img = imagepng($image); $options = array('quality' => 70, 'upscaling' => true, 'cropping' => 'center', 'sharpening'=>'medium'); $thumb = $img->size(400, 300, $options); $large = $img->size(1200, 0, $options); echo "<div class='$image->tags' class='col-4'>"; echo "<span style='overflow:hidden;'class='image fit'>"; echo "<a href='$large->url'>"; echo "<img class='uk-transform-origin-top-right' uk-scrollspy='cls: uk-animation-fade; repeat: false' src='$thumb->url' alt='$image->tags'>"; echo "</a>"; echo "</span>"; echo "</div>"; } };  
×
×
  • Create New...