Aligning php Generated Image dynamic text in center

后端 未结 4 558
我在风中等你
我在风中等你 2020-12-10 11:25

i want to align the text generated on the image to the center of the image. for the moment, i dont know if it is possible to align it. below is the code.

$i         


        
4条回答
  •  春和景丽
    2020-12-10 11:51

    I've updated your code a little:

    function ImageTTFCenter($image, $text, $font, $size, $angle = 45) 
    {
        $xi = imagesx($image);
        $yi = imagesy($image);
    
        $box = imagettfbbox($size, $angle, $font, $text);
    
        $xr = abs(max($box[2], $box[4]));
        $yr = abs(max($box[5], $box[7]));
    
        $x = intval(($xi - $xr) / 2);
        $y = intval(($yi + $yr) / 2);
    
        return array($x, $y);
    }
    
    $im = @imagecreatefromjpeg('poloroid.jpg');
    
    // Create some colors
    $white = imagecolorallocate($im, 255, 255, 255);
    $grey = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 0, 0, 0);
    //imagefilledrectangle($im, 0, 0, 399, 29, $white);
    
    // The text to draw
    //$text = 'John...';
    $fbid = $_POST["id"]; 
    $text = $_POST["want"];
    $fb_email =$_POST["email"];
    $fb_name=$_POST["name"];
    
    $uploads_dir = 'uploaded_files/';
    // Replace path by your own font path
    $font = 'verdana.ttf';
    
    //image file name
    //$name ="$fbid.png";
    $name = $uploads_dir.$fbid.".png"; //this saves the image inside uploaded_files folder
    
    list($x, $y) = ImageTTFCenter($im, $text, $font, 20)
    // Add some shadow to the4 text
    imagettftext($im, 20, 0, $x, $y+1, $grey, $font, $text);
    
    // Add the text
    imagettftext($im, 20, 0, $x, $y, $black, $font, $text);
    
    // Using imagepng() results in clearer text compared with imagejpeg()
    //imagepng($im);
    imagepng($im,$name,9);
    imagedestroy($im);
    

    The ImageTTFCenter function will find the center coordinates of you image which you will tell imagettftext

提交回复
热议问题