--[[  v1.3
2024-01-19 Scripted by Koentje
2024-09-29 Fixed nil value warning (Koentje)
2025-09-29 Added alpha transparency option

                                            _____________ vertical position in pixels
                                           |  ___________ horizontal position in pixels
                                           | |  _________ width of image
                                           | | |    _____ height of image
                                           | | |   |    _ Transparency (1-100)
                                           | | |   |   |
usage:  ${lua conky_image /path/to/picture 2 2 380 425 10}

]]

require 'cairo'
require "imlib2"


function fDrawImage(tcr,path,xx,yy,ww,hh,tt)
    local img =  cairo_image_surface_create_from_png(path)
    local w_img = cairo_image_surface_get_width(img)
    local h_img = cairo_image_surface_get_height(img)

    local tt = tonumber(tt)
    if tt == nil then tt = 100 end
    if tt <= 100 then
      if tt >= 0 then
        alph = (tt / 100)
      else
        alph = 100
      end
    else
     alph = 100
    end

    cairo_save(tcr)
    cairo_translate (tcr, xx, yy)
    cairo_scale (tcr, ww/w_img, hh/h_img)
    cairo_set_source_surface (tcr, img, xx + 6, yy + 3)
    cairo_paint_with_alpha (tcr, alph)
    cairo_surface_destroy (img)
    collectgarbage ()
    cairo_restore (tcr)
end


function conky_image(img,xxx,yyy,www,hhh,ttt)
  if conky_window==nil then return '' end
  local tcs=cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)
  local tcr=cairo_create(tcs)
  local updates=conky_parse('${updates}')
  update_num=tonumber(updates)

    fDrawImage(tcr,img,xxx,yyy,www,hhh,ttt)

   cairo_surface_destroy(tcs)
   cairo_destroy(tcr)
  return ''
end
