Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image 3.3.0 --> 4.3.0 #696

Open
NajdKarajeh opened this issue Dec 12, 2024 · 1 comment
Open

image 3.3.0 --> 4.3.0 #696

NajdKarajeh opened this issue Dec 12, 2024 · 1 comment

Comments

@NajdKarajeh
Copy link

NajdKarajeh commented Dec 12, 2024

i had to upgrade my image library package to 4.3.0 because of conflict with other libraries now I'm using image 4.3.0
and now this function from my flutter app has syntax errors after i changed the library version...any help?! :(
tried many things couldnt figure the issue

Future<List> addTextWithBorderAsImage(
String text, {
required Generator generator,
required int linesAfter,
double fontSize = 24.0,
TextAlign align = TextAlign.left,
TextDirection textDirection = TextDirection.ltr,
bool bold = false,
}) async {
// Generate the text as an image
final textImageBytes = await _textToImage(
text,
fontSize: fontSize,
align: align,
textDirection: textDirection,
bold: bold, // Use the parameter value instead of hardcoding true
);

final textImage = img.decodeImage(textImageBytes);

if (textImage == null) {
  throw Exception("Failed to decode text image.");
}

// Add padding for the border
final borderPadding = 10;

// Create a new image with extra space for the border
final borderedImage = img.Image(
  width: textImage.width + borderPadding * 2,
  height: textImage.height + borderPadding * 2,
  numChannels: 4,
);

// Fill the background with white
img.fill(borderedImage, img.getColor(255, 255, 255)); // White background

// Draw a black border
img.drawRect(
  borderedImage,
  x1: 0,
  y1: 0,
  x2: borderedImage.width - 1,
  y2: borderedImage.height - 1,
  color: img.getColor(0, 0, 0), // Black border
);

// Paste the text image into the bordered image
img.copyInto(
  borderedImage,
  textImage,
  dstX: borderPadding,
  dstY: borderPadding,
);

// Convert the bordered image to raster data for the generator
return generator.imageRaster(
  borderedImage,
  align: PosAlign.center,
);

}


final borderedImage =** img.Image**(
** textImage.width + borderPadding * 2,**
textImage.height + borderPadding * 2,
);
** img.fill(borderedImage, img.getColor(255, 255, 255)); // White background**
** img.drawRect(**
borderedImage,
0, // x1
0, // y1
borderedImage.width - 1, // x2
borderedImage.height - 1, // y2
img.getColor(0, 0, 0), // Black border
);
** img.copyInto(**
***

@brendan-duncan
Copy link
Owner

I fixed up the code with comments describing the changes.

    final textImage = img.decodeImage(textImageBytes);

    if (textImage == null) {
      throw Exception("Failed to decode text image.");
    }

// Add padding for the border
      final borderPadding = 10;

// Create a new image with extra space for the border
      final borderedImage = img.Image(
        width: textImage.width + borderPadding * 2,
        height: textImage.height + borderPadding * 2,
        numChannels: 4,
      );

// Fill the background with white
     // *****************************
      // int32 colors were removed in favor of more generic Color objects, so getColor was removed.
      // Images can now support many different types of formats and bit-depths, and it was necessary to move away
      // from the restrictive uint32 colors.
      // *****************************
      img.fill(borderedImage, color: img.ColorRgb8(255, 255, 255)); // White background

// Draw a black border
      // *****************************
      // Use of ColorRgb8 instead of getColor.
      // *****************************
      img.drawRect(
        borderedImage,
        x1: 0,
        y1: 0,
        x2: borderedImage.width - 1,
        y2: borderedImage.height - 1,
        color: img.ColorRgb8(0, 0, 0), // Black border
      );

// Paste the text image into the bordered image
     // *****************************
     // copyInto was removed because there previously were multiple functions that did essentially the same thing, and they
     // were consolidated into the single compositeImage function.
     // *****************************
      img.compositeImage(
        borderedImage,
        textImage,
        dstX: borderPadding,
        dstY: borderPadding,
      );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants