I want to create an image from a string and be abble to use this image as a background for a pdf that I'm generating.

This is almost not an issue. I want the image generated to be a full size image and I know have two choices :

First : Say that the image has a fixed size and find a way to say that the name has to be full size (The image created is the text with a 45° rotation beginning at the end bottom left of the image).

Second : Say that the image has a various size depending on the size used by the name.

I don't know if I made myself clear, but if anyone has any idea, please let me know !

PS : this is my code for the moment :

    // Création de l'image
    $im = imagecreatetruecolor();

    // Création de quelques couleurs
    $white = imagecolorallocate($im, 255, 255, 255);
    $grey = imagecolorallocate($im, 240, 240, 240);
    $black = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, 400, 500, $white);

    // Le texte à dessiner
    $text = $this->user->data->display_name;

    // Définition de la variable d'environnement pour GD
    putenv('GDFONTPATH=' . realpath('.'));
    // Remplacez le chemin par votre propre chemin de police
    $font = 'arial.ttf';


    // Ajout du texte
    imagettftext($im, 100, 45, 100, 500, $grey, $font, $text);

    // Utiliser imagepng() donnera un texte plus claire,
    // comparé à l'utilisation de la fonction imagejpeg()
    $this->watermark = 'watermark/'.date('Y-m-d-H-i-s').'.png';
    imagepng($im, $this->watermark);
    imagedestroy($im);
有帮助吗?

解决方案

If you know the font you are going to use to create the image from the text, you can use imagettfbbox() to determine the watermark size and then use imagecreatetruecolor() to generate the image.

See http://www.php.net/manual/en/function.imagettfbbox.php

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top