标签: imagettftext

使用imagettftext(),PHP右对齐图像中的文本

我正在为我的用户设置动态论坛签名图像,我希望能够将他们的用户名放在图像上.我能够做到这一点很好,但由于用户名是不同的长度,我想对齐用户名,当我必须设置x和y坐标时,我怎么能这样做.

$im = imagecreatefromjpeg("/path/to/base/image.jpg");
$text = "Username";
$font = "Font.ttf";
$black = imagecolorallocate($im, 0, 0, 0);

imagettftext($im, 10, 0, 217, 15, $black, $font, $text);
imagejpeg($im, null, 90);
Run Code Online (Sandbox Code Playgroud)

php gd image imagettftext

24
推荐指数
3
解决办法
2万
查看次数

将php生成的图像动态文本对齐在中心

我想将图像上生成的文本与图像的中心对齐.目前,我不知道是否可以对齐它.下面是代码.

$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

// Add …
Run Code Online (Sandbox Code Playgroud)

php imagettftext

17
推荐指数
3
解决办法
3万
查看次数

当文本以数字开头时,imagettfbbox计算错误的矩形

问题是,当imagettfbbox用于计算文本尺寸时,当输入文本以数字开头时,会返回太小的矩形.这是我的代码:

$fontSize = 150;
$font = "font/courier_new.ttf";
$text = $_GET["text"];

//Determine font dimensions
$bbox = imagettfbbox($fontSize, 0, $font, $text);   
$bbox["width"]=  abs($bbox[4]-$bbox[0]);
$bbox["height"]= abs($bbox[5]-$bbox[1]);

$im = imagecreatetruecolor($bbox["width"], $bbox["height"]);

echo "<b>Image size:</b>\n";
print_r($bbox);

// This part makes transparent background
imagesavealpha($im, true);
$bg = imagecolorallocatealpha($im, 254, 254, 254,127); 
$text_color= imagecolorallocate($im, 0, 0, 0);
imagealphablending($im, false);
imagefilledrectangle($im, 0, 0, imagesx($im), imagesy($im), $bg );
imagealphablending($im, true); 


header("X-Img-Size: ".$bbox["width"]."x".$bbox["height"]."");

imagettftext($im, $fontSize, 0, 0, $bbox["height"], $text_color, $font, $text);

// This is output    
header("Content-Type: text/html");  
ob_start(); …
Run Code Online (Sandbox Code Playgroud)

php gd imagettftext

17
推荐指数
1
解决办法
2522
查看次数

警告:imagettftext()[function.imagettftext]:在第35行的/home/a2424901/public_html/index.php中找不到/打开字体

<?php
session_start();
require_once 'facebook.php';
$app_id = "418907881455014";
$app_secret = "36389d2c4caaf6de86982cb87686a494";
$redirect_uri = 'http://gooogle12.comuf.com';
$facebook = new Facebook(array(
        'appId' => $app_id,
        'secret' => $app_secret,
        'cookie' => true
));
$user = $facebook->getUser();
$user_profile = $facebook->api('/me');

$coded = $_REQUEST['code'];

$access_token = $facebook->getAccessToken();
$name = "".$user_profile['name']."";
$fbid = "".$user_profile['id']."";

function RandomLine($filename) {
    $lines = file($filename) ;
    return $lines[array_rand($lines)] ;
}
$reason = RandomLine("reason.txt");  

$canvas = imagecreatefromjpeg ("bg.jpg");                                   // background image file
$black = imagecolorallocate( $canvas, 0, 0, 0 );                         // The second colour - …
Run Code Online (Sandbox Code Playgroud)

php truetype imagettftext

16
推荐指数
4
解决办法
4万
查看次数

在imagettftext()函数中从右到左读取文本字符串

我知道这个问题可能听起来很奇怪,但我很担心我为什么来找你...

我想用imagettftext()从右到左而不是从左到右写一个文本字符串; 功能

我在手册中读到了角度变量控制它,它说0角度意味着从左到右,所以我尝试了180,360但没有任何反应

我需要用什么角度来让它从右到左书写

我正在写一个带有支持希伯来字符的font.ttf的希伯来文本字符串

<?php   
$white = imagecolorallocate($background, 255, 255, 255);
$fontfile = "davidtr.ttf";
$string = "??????";
imagettftext($background, 12, 360, 3, 17, $white, $fontfile, $string);

?>
Run Code Online (Sandbox Code Playgroud)

我也使用了这个函数strrev(),

$white = imagecolorallocate($background, 255, 255, 255);
$fontfile = "davidtr.ttf";
$string = strrev("?????");
//imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
imagettftext($background, 12, 0, 3, 17, $white, $fontfile, $string);
Run Code Online (Sandbox Code Playgroud)

现在文字搞砸了图像上的一些字母是白色的盒子

然后我用这个功能:

function utf8_strrev($str){
   preg_match_all('/./us', $str, $ar);
   return join('',array_reverse($ar[0]));
}
Run Code Online (Sandbox Code Playgroud)

它对我帮助很大,但现在它也扭转了整数

$string = "??? 65 ???";
echo …
Run Code Online (Sandbox Code Playgroud)

php imagettftext

15
推荐指数
1
解决办法
1455
查看次数

将阿拉伯语写入图像时出错

我以前的相关问题:

php使用图像:用阿拉伯语,ttf字体写完整的单词

我的问题是:

  • 如果我想写入????图像,则显示为? ? ? ?
  • 好吧,我修复了它现在输出: ? ? ? ?

使用此功能:

function arab($word){

       $w = explode(' ',$word) ;

       $f = array(array('?','?'),'?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?');

       $t = array(array('?_','?_'),'?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_');

       $my_arab = '' ;

       foreach($w as $wo)
        {
             $r  = array() ;

             $wo = str_replace($f , $t ,$wo);

             $ne = explode('_', $wo) ;

             foreach($ne as $new) {
                $new = str_replace('_','',$new) ;
                array_unshift($r , $new);
             }

            $my_arab .=  ' '.implode('',$r) ;

        }

     return trim($my_arab) ;

}
Run Code Online (Sandbox Code Playgroud)

但新问题是:

? ? ? ?

(分开的字母)它应该是: …

php arabic imagettftext

11
推荐指数
2
解决办法
2590
查看次数

用于在php中转换为图像的开箱即用文本

我正在尝试将文本转换为图像.我已经这样做了,但有些情况下文本不在图像框中图片

"The"这个词的"e"被删除了.我尝试过减小字体大小或增加图像的宽度,但在某些情况下,这会再次发生在另一个文本中.这是代码:

    $new_line_position = 61;        
    $angle = 0;        
    $left = 20;
    $top = 45;
    $image_width = 1210;
    $image_line_height = 45;                

    $content_input = wordwrap($content_input,    $new_line_position, "\n", true);  

    $lineas = preg_split('/\\n/', $content_input);
    $lines_breaks = count($lineas); 
    $image_height = $image_line_height * $lines_breaks;
    $im = imagecreatetruecolor($image_width, $image_height);

    // Create some colors
    $white = imagecolorallocate($im, 255, 255, 255);        
    $black = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, $image_width, $image_height, $white);   

   $font_ttf =  public_path().'/fonts/'.$ttf_font;                     


    foreach($lineas as $linea){
        imagettftext($im, $font_size, $angle, $left, $top, $black, $font_ttf, $linea);        
        $top = …
Run Code Online (Sandbox Code Playgroud)

php fonts gd truetype imagettftext

11
推荐指数
1
解决办法
687
查看次数

PHP imagettftext基线解决方法

我正在写使用PHP将文本打印到图像.但是,该函数imagettftext()使用基线,而我需要垂直居中的文本.

所以,我要么需要一种方法来打印带有y的文本,而不是从顶部到基线的距离,但是从边界框的顶部到顶部,或者我需要一种方法,使用它可以确定边界框顶部和基线之间的距离.

显然,我很困惑你.所以,要说清楚:我知道这个功能imagettfbbox().使用该功能,我可以确定结果文本框的高度和宽度.然而,当打印时imagettftext(),它的高度对于垂直对齐是完全没用的,因为Y参数不是到盒子顶部的距离(或者甚至是底部,但至少我可以使用具有高度的东西)但是距离到内部文本的基线.

编辑:为什么我不接受最新的答案?

请参阅答案下方的最新评论,并将此图像用作参考.

php imagettftext baseline

9
推荐指数
2
解决办法
6008
查看次数

具有透明度/ Alpha背景的PHP GD文本

好吧,我有一个问题,让我的文字覆盖在部分透明的图像上.我希望文本是实体的,但我希望图像的背景部分是透明的,文本的部分是实心的,我有,问题是文本继承了其中一个的透明背景以前的图层.这里是代码,输出的一个例子,在那个输出下我想要的样子.图像位于浅灰色背景上,因此在深灰色之间的图像周围的光边界是透明的,但没有其他任何东西应该特别是文本.它似乎不是文本本身,而是文本块的背景是透明的.正如你所看到的那样不是很理想.请帮忙,这是我完成项目的唯一问题.:)

无法发布图像,因此继承了示例输出图像和所需结果(orig)的链接:

在此输入图像描述

<?php

$img = imagecreatetruecolor(200, 50);

$imageX = imagesx($img);
$imageY = imagesy($img);

imagealphablending($img, false);
imagesavealpha($img, true);

$transparent = imagecolorallocatealpha($img, 255,255,255, 127);
$white = imagecolorallocate($img, 255,255,255);
$grey = imagecolorallocate($img, 127,127,127);
imagefilledrectangle($img, 0, 0, $imageX, $imageY, $grey);
imagefilledrectangle($img, 2, 2, $imageX-4, $imageY-4, $transparent);

$font = "./arialbd.ttf";
$fontSize = 12;
$text = "THIS IS A TEST";

$textDim = imagettfbbox($fontSize, 0, $font, $text);
$textX = $textDim[2] - $textDim[0];
$textY = $textDim[7] - $textDim[1];

$text_posX = ($imageX / 2) - ($textX / …
Run Code Online (Sandbox Code Playgroud)

php gd transparent imagettftext

7
推荐指数
1
解决办法
1万
查看次数

PHP - imagettftext无法正常工作并安装了GD

我正在寻找这个问题的答案是很长时间.我找到的所有解决方案都是捕捉字体名称,但我很确定这不是我的问题.

它看起来像是安装了GD

array(11) {
  ["GD Version"]=>
  string(27) "bundled (**2.0.34 compatible**)"
  ["FreeType Support"]=>
  bool(false)
  ["T1Lib Support"]=>
  bool(false)
  ["GIF Read Support"]=>
  bool(true)
  ["GIF Create Support"]=>
  bool(true)
  ["JPEG Support"]=>
  bool(true)
  ["PNG Support"]=>
  bool(true)
  ["WBMP Support"]=>
  bool(true)
  ["XPM Support"]=>
  bool(true)
  ["XBM Support"]=>
  bool(true)
  ["JIS-mapped Japanese Font Support"]=>
  bool(false)
}
Run Code Online (Sandbox Code Playgroud)

上面你可以看到我的GD支持.我的PHP版本是5.3,我在Linux上运行.

我尝试过来自不同网站的几个不同的代码示例,但都没有.ImageString确实适合我,但我需要让imagettftext工作..

这是我现在尝试的最后一个代码 -

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Set the content-type
header('Content-Type: image/png');

// Create the image
$im = imagecreatetruecolor(400, 100) or die("Can't create image!");

// Create some colors
$white = imagecolorallocate($im, …
Run Code Online (Sandbox Code Playgroud)

php gd image imagettftext

7
推荐指数
4
解决办法
3万
查看次数

标签 统计

imagettftext ×10

php ×10

gd ×5

image ×2

truetype ×2

arabic ×1

baseline ×1

fonts ×1

transparent ×1