skip to Main Content

I’m using https://github.com/Intervention/image in my laravel Project to generate a watermark on the image. I download and use fonts from https://fonts.google.com and https://github.com/osify/cambodianfonts/tree/master/Unicode/Compartible-with-Photoshop-CS3 Both not working.

I wrote code

            $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);
            });

Missing font result

Preview Result

So, Please help suggestion any code or solutions.

Note* I also read and follow
How to recognize khmer unicode in intervention images?

https://github.com/Intervention/image/issues/702

But it is also no solution.
and

2

Answers


  1. Chosen as BEST ANSWER

    I really want to use the only PHP.

    If I use java working well with code below.

    
    @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;
        }
    }
    
    

  2. I also try with KH-font but it is not working.
    My Solution:

    1. NodeJS (text2png) to create image from Khmer 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");
    })
    1. Send request from PHP and combine two images
    $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');

    Here is Result

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search