You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@ryanwelcher I was just rewatching the OpenAI videos you streamed a while back and I played around with optimizing the blob generation time.
We can actually create the blob directly without having to create an itermediate Canvas element and call the canvas.toBlob() method. Using this method I saw up to 5x speed increase but in most cases it was around 4x increase!
Here's the updated code for helpers.js with performance timers included, which log the function run times to the console. The convertImageToBlobOld and loadImage functions can be removed now if using the new method.
Hope it's useful. 🙂
importapiFetchfrom'@wordpress/api-fetch';import{uploadMedia}from'@wordpress/media-utils';import{cleanForSlug}from'@wordpress/url';import{API_KEY}from'./constants';exportconstconvertImageToBlobOld=async(base64Image)=>{// Start the timerconststartTime=performance.now();constimage=newImage();image.src=`data:image/png;base64,${base64Image}`;image.crossOrigin='anonymous';awaitloadImage(image);constcanvas=document.createElement('canvas');canvas.width=image.width;canvas.height=image.height;constctx=canvas.getContext('2d');if(!ctx)return;ctx.drawImage(image,0,0);constblob=awaitnewPromise((resolve)=>{canvas.toBlob((blob)=>{blob&&resolve(blob);},'image/jpeg');});// Stop the timerconstendTime=performance.now();// Calculate the execution time in millisecondsconstexecutionTime=endTime-startTime;// Log the execution time to the consoleconsole.log(`Execution time for convertImageToBlobOld(): ${executionTime} milliseconds`);returnblob;};constloadImage=(img)=>{returnnewPromise((resolve)=>(img.onload=resolve));};exportconstconvertImageToBlob=(base64Image)=>{// Start the timerconststartTime=performance.now();// Your base64 encoded image stringconstbase64ImageSrc=`data:image/png;base64,${base64Image}`;// Convert the base64 string to binary dataconstbinaryData=atob(base64ImageSrc.split(',')[1]);// Create a Uint8Array from the binary dataconstuint8Array=newUint8Array(binaryData.length);for(leti=0;i<binaryData.length;i++){uint8Array[i]=binaryData.charCodeAt(i);}// Create a Blob from the Uint8Arrayconstblob=newBlob([uint8Array],{type: 'image/png'});// Stop the timerconstendTime=performance.now();// Calculate the execution time in millisecondsconstexecutionTime=endTime-startTime;// Log the execution time to the consoleconsole.log(`Execution time for convertImageToBlob(): ${executionTime} milliseconds`);// Return the Blobreturnblob;};exportasyncfunctionmakeRequest({
prompt ='',
numberOfImages =1,
size ='256x256',
format ='b64_json',}){constrequest=awaitfetch('https://api.openai.com/v1/images/generations',{method: 'POST',headers: {'Content-Type': 'application/json',Authorization: `Bearer ${API_KEY}`,},body: JSON.stringify({
prompt,n: numberOfImages,
size,response_format: format,}),});constjson=awaitrequest.json();returnjson.data;}exportconstuploadImageToMediaLibrary=async(imageSrc,prompt,uploadCallback)=>{constblob=awaitconvertImageToBlob(imageSrc);constblobOld=awaitconvertImageToBlobOld(imageSrc);conststatus=awaituploadMedia({filesList: [newFile([blob],`${cleanForSlug(prompt)}.png`)],onFileChange: ([fileObj])=>{returnuploadCallback(fileObj);},onError: console.error,});returnstatus;};
The text was updated successfully, but these errors were encountered:
@ryanwelcher I was just rewatching the OpenAI videos you streamed a while back and I played around with optimizing the blob generation time.
We can actually create the blob directly without having to create an itermediate Canvas element and call the
canvas.toBlob()
method. Using this method I saw up to 5x speed increase but in most cases it was around 4x increase!Here's the updated code for
helpers.js
with performance timers included, which log the function run times to the console. TheconvertImageToBlobOld
andloadImage
functions can be removed now if using the new method.Hope it's useful. 🙂
The text was updated successfully, but these errors were encountered: