• Resolved Jek-fdrv

    (@jek-fdrv)


    Hi there, i use very custom build, and i create the src set list of images my self via php. I would like to use your nice plugin to control when and how to resize, generate webp image and if it should has lazy load or not my self in php. Example:

    In the page tempalte:
    get_right_image($image_id, '300px');

    In functions.php

    function get_post_image($image_id, $size) {
      $popular_device_sizes = [1440, 921, 769, 390];
      foreach($popular_device_sizes as $img_size) {
        $srcst[] = generate_ShortPixel($image_id, $img_size);
      }
      return '
      <img src="'.$srcst[0]['src'].'" 
    	srcset="'.implode(', ', $srcst).'" >';

    }`
    So do you have a function some like generate_ShortPixel ?

    This is only very roughly code example, not the real one.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Pedro

    (@petredobrescu)

    Hello @jek-fdrv,

    I believe we need to clarify one thing first: the plugin needs to send the images to the API in order to get them optimized or generate WebPs etc. This is not done locally on your server, therefore it needs some processing. In other words, you won’t be able to do this on the fly and there’s not a direct function to achieve this from the code, mostly because there’s a lot of logic around what to process, when to process it, etc.

    If your code is that custom, maybe you can think about using other tools that we have, such as:
    * our adaptive images JS library: https://shortpixel.com/ai-js
    * our adaptive images API: https://shortpixel.com/adaptive-images-api
    * the PHP client library: https://github.com/short-pixel-optimizer/shortpixel-php
    * directly the reducer API: https://shortpixel.com/api-docs

    Alternatively, if you can describe in more detail what you want to achieve and if you are using WordPress at all in your code, we could point you in the right direction and/or tool to use.

    All the best

    Thread Starter Jek-fdrv

    (@jek-fdrv)

    Great answer, Thanks Pedro!

    Let me try describe a list of goals which i try to achieve with plugin:

    1) Automatic generate required image size not upon loading new image but upon request. So client upload a big image, like 4000 px in width, but we need it only for container with 500px width, so i would like to add some additional attributes to the img tag, like <img width=”500px”… so plugin will automatically generate lower size version of that image like 500 and 1000 (fore retina) and then convert them both to webP. And if it done in async (on API server) then, lets first response with big original image but on the next request it will return the previously generated 500 and 1000 width versions.

    2) If image are full width (take 100% width of client browser) and client uploaded a super big image like 6000 px in width, but we use it in a tag like <img widht=”100%”… then let me ask your plugin to create a list of sizes for this case, like 5120 (5k retina, 3840 fullHD retina, 3072, 1920 and 1440)
    I get this list after some research for a most popular device resolutions, and i will use srcset and sizes attrbiutes to explain browser which image i need to use when.

    3) Lazy load.

    • This reply was modified 2 years, 2 months ago by Jek-fdrv.
    Plugin Support Gerard Blanco

    (@sixaxis)

    Hi jek-fdrv,

    The problem with your approach is that nowadays, you may end up generating lots and lots of thumbnails, because there’s already too many devices with different resolutions out there.

    Have you thought about ShortPixel Adaptive Images? It will save you a lot of headaches, and it does exactly what you need: lazy load, generate the images with the exact size you need, take care of Retina, optimize them and as a nice bonus, serve them from a fast CDN instead of from your server.

    Cheers,

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘I would like to generate different image size via php.’ is closed to new replies.