ACF Library

resizeAndConvert2PNG

Back to List

Description: Resize an image to a sqare PNG image, 256, 512 or 1024 pixels wide, compatible with DALL-E-2

FileMaker Prototype:

Set Variable [$res; ACF_Run("resizeAndConvert2PNG"; string_path)]

Category: OPENAI

Dependencies

Function source:

function resizeAndConvert2PNG ( string path )
   string basepath = regex_replace("^(.+)/(.+\..+)$", path, "\1");
   string filename = regex_replace("^(.+)/(.+\..+)$", path, "\2");
   string filetype = regex_replace("^(.+/)(.+\.)(.+)$", path, "\3");
   string commands, result, newfile; 
   JSON size, jresult; 
   size = getImageSize (path); 
   int h = int(size["h"]); 
   int w = int(size["w"]);
   string targetSize;
   bool convert = true;  
   case 
      :(h == 1024 && w == 1024 && filetype == "png")
         convert = false; 
         targetSize = "1024x1024";
      :(h == 512 && w == 512 && filetype == "png")
         convert = false; 
         targetSize = "512x512";
      :(h == 256 && w == 256 && filetype == "png")
         convert = false; 
         targetSize = "256x256";
      :(h<=256 && w<=256)
         targetSize = "256x256";
      :(h<=512 && w<=512)
         targetSize = "512x512";
      :(h<=1024 && w<=1024)
         targetSize = "1024x1024";
      default
         targetSize = "1024x1024";
   end case
   if (convert) then
      newfile = substitute (filename, "."+filetype, "")+"_"+targetSize+"_conv.png";
      commands = format("convert %s -resize %s -gravity center -background none -extent %s %s", 
         filename, targetSize, targetSize, newfile); 
      result = SystemCommand(commands, basepath);
      jresult = JSON ("file", newfile,"path",basepath+"/"+newfile, "size", targetSize, "result", result); 
   else
      jresult = JSON ("file", filename,"path",path, "size", targetSize, "result", "--"); 
   end if
   return jresult; 
end 

Back to List