S
S
Sergey Valitov2015-09-17 18:45:05
PHP
Sergey Valitov, 2015-09-17 18:45:05

How to quickly embed translucent text into an image in php?

Hello! Such a question - how to quickly embed translucent text at an angle into the picture?
There is such a code:

function textonimage($path) {
        $string =  "Текстовочка";
        $image = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT'].$path);
        $color = imagecolorallocatealpha($image, 255, 255, 255, 80);
        $box = imagettfbbox(40, 45, '/var/www/includes/modules/arial.ttf', $string);
        $left = 490-round(($box[2]-$box[0])/2);
        
        imagettftext($image, 40, -45, $left, 180, $color, '/var/www/includes/modules/arial.ttf', $string);
        ob_start();
        header('Content-type: image/jpeg');
        imagejpeg($image);
        imagedestroy($image);
        $stringdata = ob_get_clean();
        return 'data:image/jpeg;base64,' . base64_encode($stringdata);
      }

Since the size of the pictures for inserting text into them is large - we sacrifice the loading time of the site. It takes about two seconds for 3 large pictures. Is there a faster way to embed text on an image? so that even when saving the picture, it remains. In general watermark. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Ukolov, 2015-09-17
@alexey-m-ukolov

So you cache watermarked images and create them when you add images, not when you output. A content editor can tolerate two seconds.

R
Roman Mirilaczvili, 2015-09-17
@2ord

I think imagemagick can handle it quickly.

S
sivabur, 2015-09-20
@sivabur

You can simply adjust the picture on the picture. I can't say exactly what is the best way to do this. But it will be clearly faster than yours, since you also load the font every time. and draw the letters themselves.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question