问题描述
我在laravel
项目中使用https://github.com/Intervention/image在图像上生成水印。我从https://fonts.google.com和https://github.com/osify/cambodianfonts/tree/master/Unicode/Compartible-with-Photoshop-CS3下载并使用了两种字体。
我写了代码
$textFont = public_path('Khmer/Khmer-Regular.ttf');
$template = Image::make($this->disk->path($user->avatar));
$text = "កម្ពុជា";
$template->text($text,190,600,function ($font) use ($textFont) {
$font->file($textFont);
$font->size(30);
$font->color('#bc1f1f');
// $font->align('center');
$font->valign('top');
$font->angle(0);
});
缺少字体结果
注意*我也阅读并关注 How to recognize khmer unicode in intervention images?
https://github.com/Intervention/image/issues/702
但这也不是解决方案。 和
解决方法
我也尝试使用KH字体,但是它不起作用。 我的解决方案:
- NodeJS(text2png)从高棉Unicode创建图像。
const express = require('express');
const text2png = require('text2png');
const path = require('path');
const app = express();
app.get('/image',function (req,res) {
const buffer = text2png('ប្រទេសកម្ពុជា',{
color: 'red',font: '80px Bokor',localFontPath: path.join(__dirname,'/../public/fonts/Kh-Bokor.ttf'),localFontName: 'Bokor',output: 'buffer'
});
return res.status(200).send(buffer)
});
app.listen(3000,function () {
console.log("Express run on port: 3000");
})
- 从PHP发送请求并合并两个图像
$data = Http::get('http://localhost:3000/image');
$body = $data->body();
$watermark = Image::make($body);
$image = Image::make(public_path() . "/images/cam.jpg");
$image->insert($watermark,'center');
$image->save('image.png');
,
我真的想使用唯一的PHP。
如果我使用Java与下面的代码配合得很好。
@RestController
@RequestMapping("image")
public class ImageGenerateController {
final static String UPLOAD = "";
@PostMapping(value = "/generate")
public ResponseEntity<?> generate(@Valid @RequestBody ImagePayload request) throws Exception {
ByteBuffer buffer = StandardCharsets.UTF_8.encode(request.getTitle());
String utf8EncodedString = StandardCharsets.UTF_8.decode(buffer).toString();
try {
BufferedImage img = stringToBufferedImage(utf8EncodedString);
System.out.println(img);
File file = new File(UPLOAD + request.getName() + ".png");
ImageIO.write(img,"png",file);
String mmm = "ពិតជាអស្ចារ្យណាស់";
return ResponseEntity.ok(mmm);
} catch (IOException ex) {
ex.printStackTrace();
}
return ResponseEntity.badRequest().body("Missing something");
}
public BufferedImage stringToBufferedImage(String value) {
//First,we have to calculate the string's width and height
BufferedImage img = new BufferedImage(1,1,BufferedImage.TYPE_4BYTE_ABGR);
Graphics g = img.getGraphics();
//Set the font to be used when drawing the string
Font f = new Font("Khmer OS",Font.PLAIN,48);
g.setFont(f);
//Get the string visual bounds
AttributedString attributedText = new AttributedString(value);
attributedText.addAttribute(TextAttribute.FONT,f);
attributedText.addAttribute(TextAttribute.FOREGROUND,Color.GREEN);
FontRenderContext frc = g.getFontMetrics().getFontRenderContext();
Rectangle2D rect = f.getStringBounds(value,frc);
//Release resources
g.dispose();
//Then,we have to draw the string on the final image
//Create a new image where to print the character
img = new BufferedImage((int) Math.ceil(rect.getWidth()),(int) Math.ceil(rect.getHeight()),BufferedImage.TYPE_4BYTE_ABGR);
g = img.getGraphics();
g.setColor(Color.black);
//Otherwise the text would be white
g.setFont(f);
//Calculate x and y for that string
FontMetrics fm = g.getFontMetrics();
int x = 0;
int y = fm.getAscent(); //getAscent() = baseline
g.drawString(attributedText.getIterator(),x,y);
//Release resources
g.dispose();
//Return the image
return img;
}
}