@xuda.io/ai_module 1.1.4307 → 1.1.4309

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.mjs +68 -1
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -6739,6 +6739,25 @@ const get_business_logo = async function (uid, name, account_profile_info) {
6739
6739
 
6740
6740
  const url = ret.data;
6741
6741
  const file = await return_image_blob_from_downloaded_image(url);
6742
+ // const imageBase64 = Buffer.from(await file.image_blob.arrayBuffer()).toString('base64');
6743
+ const model = 'chatgpt-image-latest';
6744
+ const prompt = `
6745
+ extract the logo from the image
6746
+ `;
6747
+ const logo_response = await client.images.edit({
6748
+ model,
6749
+ image: file.image_blob, //logo
6750
+ prompt,
6751
+ background: 'transparent',
6752
+ });
6753
+ record_chat_usage(uid, logo_response.usage.input_tokens, logo_response.usage.output_tokens, 'business logo', prompt, model, { url }, account_profile_info);
6754
+ imageBase64 = await normalizeBase64To1024(ai_avatar_response.data[0].b64_json);
6755
+
6756
+ const outputBuffer = Buffer.from(imageBase64, 'base64');
6757
+ const outputPath = file.tempOutputPath;
6758
+ await fs.promises.writeFile(outputPath, outputBuffer);
6759
+
6760
+ console.log('Success! Saved →', outputPath);
6742
6761
 
6743
6762
  console.log(file);
6744
6763
  // prompt: `find "${name}" logo base on the following facts "${facts}", Return only png,jpg img tag , nothing else`,
@@ -6750,7 +6769,55 @@ const get_business_logo = async function (uid, name, account_profile_info) {
6750
6769
  // } catch (error) {}
6751
6770
  };
6752
6771
 
6772
+ async function normalizeBase64To1024(base64Image) {
6773
+ const inputBuffer = Buffer.from(base64Image, 'base64');
6774
+
6775
+ const normalizedBuffer = await sharp(inputBuffer)
6776
+ .resize(1024, 1024, {
6777
+ fit: 'contain', // Preserve aspect ratio, fit within 211x211
6778
+ background: { r: 0, g: 0, b: 0, alpha: 0 }, // Transparent background for padding
6779
+ })
6780
+ .png({
6781
+ quality: 100,
6782
+ compressionLevel: 9,
6783
+ force: true,
6784
+ })
6785
+ .toBuffer();
6786
+
6787
+ return normalizedBuffer.toString('base64'); // Return normalized base64 string
6788
+ }
6789
+
6753
6790
  const return_image_blob_from_downloaded_image = async function (url) {
6791
+ async function hasTransparentBackground(filePath) {
6792
+ const image = sharp(filePath);
6793
+ const metadata = await image.metadata();
6794
+
6795
+ // If the image doesn't have an alpha channel at all → no transparency possible
6796
+ if (!metadata.hasAlpha) {
6797
+ return false;
6798
+ }
6799
+
6800
+ // For images with alpha channel, check if any pixel actually has alpha < 255
6801
+ const { data, info } = await image.raw().toBuffer({ resolveWithObject: true });
6802
+
6803
+ // Determine position of alpha channel based on number of channels
6804
+ const channels = info.channels; // 2 (grayscale+alpha), 4 (RGBA), etc.
6805
+ const alphaOffset = channels === 2 ? 1 : channels === 4 ? 3 : -1;
6806
+
6807
+ if (alphaOffset === -1) {
6808
+ // Unusual channel count – be conservative and assume it might have transparency
6809
+ return true;
6810
+ }
6811
+
6812
+ for (let i = alphaOffset; i < data.length; i += channels) {
6813
+ if (data[i] < 255) {
6814
+ return true; // Found at least one transparent/semi-transparent pixel
6815
+ }
6816
+ }
6817
+
6818
+ return false; // Has alpha channel but all pixels are fully opaque
6819
+ }
6820
+
6754
6821
  async function imageToPngBlob(filePath) {
6755
6822
  const ext = path.extname(filePath).toLowerCase();
6756
6823
 
@@ -6810,7 +6877,7 @@ const return_image_blob_from_downloaded_image = async function (url) {
6810
6877
  return { is_transparent: img_ret.is_transparent, image_blob: blob };
6811
6878
  }),
6812
6879
  );
6813
- return images[0];
6880
+ return { image_blob: images[0].image_blob, is_transparent: images[0].is_transparent, tempOutputPath };
6814
6881
  } catch (err) {
6815
6882
  console.error(err);
6816
6883
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/ai_module",
3
- "version": "1.1.4307",
3
+ "version": "1.1.4309",
4
4
  "description": "Xuda AI Module",
5
5
  "main": "index.mjs",
6
6
  "type": "module",