File: /var/www/html/wptoho/wp-content/themes/themify-ultra/themify/js/modules/image-resize.js
let ThemifyImageResize;((t,i,s,h)=>{"use strict";const e=new Map,a=new Map,r="image/webp",o=t=>{let i=!1;for(let[s,h]of e)if(h===t){i=!0;break}if(!1===i){URL.revokeObjectURL(t);for(let[i,s]of a)if(s===t){a.delete(i);break}}};ThemifyImageResize=class{static getCache(t,i,s,h){return a.get(t+i+s+h)}constructor(h,e,a,r){this.img=h;const o=!a&&h.dataset.orig?h.dataset.orig:h.currentSrc||h.src||"",n=t.upload_url||window.location.protocol+"//"+t.urlHost;if(this.isLocal=!0===r||(0===o.indexOf(n)||o.includes("blob:")||o.includes(t.cdnPlugin)||o.includes("data:")||o.includes("themify.org"))&&!h.closest("[data-has-editor]"),this.url=o,!0===this.isLocal){if(o.includes("blob:")||o.includes("data:")||(h.dataset.orig=o),h.complete&&(this.w=h.naturalWidth,this.h=h.naturalHeight,this.w>2560||this.h>2560))return this.isBig=!0,void(this.prms=Promise.resolve());if(this.ext=o.slice(2+(o.lastIndexOf(".")-1>>>0)),"svg"===this.ext)return void(this.prms=Promise.resolve());this.prms=new Promise((async(t,h)=>{this.tmpImg=new Image,this.tmpImg.crossOrigin="anonymous",this.tmpImg.decoding="async",this.tmpImg.src=o;try{if(await this.tmpImg.decode(),this.w=s.trunc(this.tmpImg.naturalWidth),this.h=s.trunc(this.tmpImg.naturalHeight),this.w>2560||this.h>2560)return this.isBig=!0,void t();this.aspectRatio=parseFloat(this.w/this.h),this.canvas=i.createElement("canvas"),this.ctx=this.canvas.getContext("2d",{alpha:!this.ext.includes("jpeg")}),e>0?this.q=e:(this.q=.01,this.w<380&&this.h<380?this.q=1:this.w<400&&this.h<400?this.q=.9:this.w<450&&this.h<450?this.q=.8:this.w<500&&this.h<500?this.q=.7:this.w<550&&this.h<550?this.q=.6:this.w<650&&this.h<650?this.q=.5:this.w<850&&this.h<850?this.q=.4:this.w<1e3&&this.h<1e3?this.q=.3:this.w<1100&&this.h<1100?this.q=.2:this.w<1200&&this.h<1200?this.q=.1:this.w>1700&&this.h>1700&&(this.q=.001)),this.ctx.imageSmoothingEnabled=this.q>.7,this.ctx.imageSmoothingQuality=this.q>.7?"high":this.q>.5?"medium":"low",1!==this.q&&(this.origImg=this.tmpImg.cloneNode(!0)),t()}catch(t){h(t)}}))}else this.prms=Promise.resolve()}lowQuality(i,s){return new Promise(((h,e)=>{this.prms.then((()=>{if(!1===this.isLocal||!0===this.isBig||"svg"===this.ext)return void h();const a=navigator.deviceMemory;i||(i="mobile"===t.device?400:a>4?800:600),s||(s="mobile"===t.device?400:a>4?800:600);const o=i,n=s;if(this.w>o||this.h>n){this.w>this.h?this.w>o&&(this.h*=o/this.w,this.w=o):this.h>n&&(this.w*=n/this.h,this.h=n),this.canvas.width=this.w,this.canvas.height=this.h,this.aspectRatio=parseFloat(this.w/this.h),this.ctx.imageSmoothingEnabled=!1,this.ctx.imageSmoothingQuality="low";(()=>"function"==typeof createImageBitmap?createImageBitmap(this.tmpImg,{resizeQuality:"pixelated",resizeWidth:this.w,resizeHeight:this.h}):Promise.reject())().then((t=>{this.ctx.drawImage(t,0,0)})).catch((()=>{this.ctx.drawImage(this.tmpImg,0,0,this.w,this.h)})).then((()=>{this.canvas.toBlob((t=>{try{const i=URL.createObjectURL(t,r);this.tmpImg.tfOn("load",(()=>{URL.revokeObjectURL(i),h()}),{passive:!0,once:!0}),this.tmpImg.src=i}catch(t){e(t)}}),r,.1)})).catch(e)}else h()})).catch(e)}))}highQuality(t,i){return new Promise((async(s,h)=>{try{if(await this.prms,!1===this.isLocal||!0===this.isBig||"svg"===this.ext)return void s();this.origImg&&(await this.origImg.decode(),this.tmpImg=this.origImg,this.w=this.tmpImg.naturalWidth,this.h=this.tmpImg.naturalHeight,this.aspectRatio=parseFloat(this.w/this.h));s(await this.toBlob(t,i,.8))}catch(t){h(t)}}))}setAttribute(t,i){t?(this.img.width=t,this.img.style.width&&"cover"!==this.img.style.objectFit&&(this.img.style.width=t+"px")):this.img.removeAttribute("width"),i?(this.img.height=i,this.img.style.height&&"cover"!==this.img.style.objectFit&&(this.img.style.height=i+"px")):this.img.removeAttribute("height")}resize(t,i,e){return new Promise(((a,o)=>{this.prms.then((()=>{t&&(t=s.trunc(t)),i&&(i=s.trunc(i));const o=this.calculate(t,i),n=t>0?t:s.floor(i*this.aspectRatio),l=i>0?i:s.floor(t/this.aspectRatio);let c;!0===this.isLocal&&!0!==this.isBig&&"svg"!==this.ext?(o.w=s.trunc(o.w),o.h=s.trunc(o.h),this.canvas.width=n,this.canvas.height=l,this.ctx.drawImage(this.tmpImg,o.x,o.y,o.w,o.h,0,0,n,l),e===h&&(e=this.q),c=this.canvas.toDataURL(r,e),this.img.src=c):c=this.url,this.setAttribute(t,i),a([c,t,i,n,l])})).catch(o)}))}async toBlob(t,i,s){if(await this.prms,!1===this.isLocal||!0===this.isBig||"svg"===this.ext)return;s||(s=this.q),t===h&&(t=""),i===h&&(i="");let r=this.url+t+i+s,o=this.constructor.getCache(this.url,t,i,s);return o?(this.setAttribute(t,i),this.img.src=o,e.set(this.img,o)):(this.ctx.imageSmoothingEnabled=s>.7,this.ctx.imageSmoothingQuality=s>.7?"high":s>.5?"medium":"low",await this.resize(t,i,s),o=await this.save(s),a.set(r,o)),o}save(t){return new Promise(((i,s)=>{this.prms.then((()=>{this.canvas.toBlob((t=>{try{const s=URL.createObjectURL(t,r),h=()=>{const t=e.get(this.img);e.set(this.img,s),t&&o(t),i(s)};"IMG"===this.img.tagName?this.img.tfOn("load",h,{passive:!0,once:!0}).src=s:h()}catch(t){s(t)}}),r,t)})).catch(s)}))}calculate(t,i){let h=t,e=i,a=t,r=i;h||(h=t=s.trunc(e*this.aspectRatio)),e||(e=i=s.trunc(h/this.aspectRatio));const o=h/this.w,n=e/this.h,l=o>n?o:n;a=h/l+.5>>0,r=e/l+.5>>0;return{x:(this.w-a)/2>>0,y:(this.h-r)/2>>0,w:a,h:r}}};ThemifyImageResize.toBlob=(t,s,h,e=!0)=>((t,s,h,e)=>{if(!s&&!h)return Promise.resolve();let a,r,o=new ThemifyImageResize(t,.8);return!0===o.isLocal&&!0!==o.isBig&&t.isConnected&&(r=i.createElement("span"),r.className="tf_loader tf_abs_c",t.after(r)),a=e?o.toBlob(s,h):o.resize(s,h),a.finally((()=>{r&&(r.remove(),r=null),o.canvas&&(o.ctx.clearRect(0,0,o.canvas.width,o.canvas.height),o.tmpImg?.remove(),o.origImg?.remove(),o.canvas=o.tmpImg=o.origImg=o.ctx=null),o=null}))})(t,s,h,e),ThemifyImageResize.clear=t=>{t&&a.clear();for(let[i,s]of e)!0!==t&&i.isConnected||(e.delete(i),o(s))},setInterval(ThemifyImageResize.clear,18e3)})(Themify,document,Math,void 0);