如何使用GD库获取每行和中心文本的单词?

时间:2016-04-02 15:56:30

标签: php gd

我需要所有单词以图像为中心并具有良好的边距,我使用GD库。

此代码在图像上写下您想要的任何内容,问题不在于如何使您所写的内容始终居中。单词会降低,并且不会始终聚焦在图像上。

我有这段代码:

class textPainter{
private $img;
private $textColor;
private $position = array();
private $startPosition = array();

private $imagePath;
private $text;
private $fontFile;
private $fontSize;
private $format;

public function __construct($imagePath, $text, $fontFile, $fontSize){       
    $this->imagePath = $imagePath;
    $this->text = $text;
    $this->fontFile = $fontFile;
    $this->fontSize = $fontSize;

    $this->setFormat();
    $this->setQuality();
    $this->createImage();
    $this->setTextColor();
    $this->setPosition();
}

public function setTextColor($R=0, $G=0, $B=0){
    $this->textColor = imagecolorallocate ($this->img, $R, $G, $B);
}

public function show(){
    //show thumb
    header("Content-type: image/".$this->format);   
    //creates the text over the background image
    imagettftext($this->img, $this->fontSize, 0, $this->startPosition["x"], $this->startPosition["y"], $this->textColor, $this->fontFile, $this->text);

    switch ($this->format){
        case "JPEG":
            imagejpeg($this->img,"",$this->jpegQuality);
            break;
        case "PNG":
            imagepng($this->img);
            break;
        case "GIF":
            imagegif($this->img);
            break;
        case "WBMP":
            imagewbmp($this->img);
            break;
        default:
            imagepng($this->img);
    }
}

public function setQuality($value=85){
    $this->jpegQuality = $value;
}

public function setPosition($x="center", $y="center"){
    $this->position["x"] = $x;
    $this->position["y"] = $y;

    $dimensions = $this->getTextDimensions();

    if($x=="left"){
        $this->startPosition["x"] = 0;
    }
    else if($x=="center"){          
        $this->startPosition["x"] = imagesx($this->img)/2 - $dimensions["width"]/2;
    }
    else if($x=="right"){
        $this->startPosition["x"] = imagesx($this->img) - $dimensions["width"];
    }
    //custom
    else{
        $this->startPosition["x"] = $x;
    }

    if($y=="top"){
        $this->startPosition["y"] = 0 + $dimensions["heigh"];
    }
    else if($y=="center"){
        $this->startPosition["y"]  = imagesy($this->img)/2 + $dimensions["heigh"]/2;
    }
    else if($y=="bottom"){
        $this->startPosition["y"]  = imagesy($this->img);
    }
    //custom
    else{
        $this->startPosition["y"] = $y;
    }

}

private function setFormat(){
    $this->format = ereg_replace(".*\.(.*)$","\\1",$this->imagePath);
    $this->format = strtoupper($this->format);

    if($this->format=="JPG" || $this->format=="JPEG"){
        $this->format="JPEG";
    }
    else if($this->format=="PNG"){
        $this->format="PNG";
    }
    else if ($this->format=="GIF"){
        $this->format="GIF";
    }
    else if ($this->format=="WBMP"){
        $this->format="WBMP";
    }else{
        echo "Not Supported File";
        exit();
    }
}

private function createImage(){
    if($this->format=="JPEG"){
        $this->img = imagecreatefromjpeg($this->imagePath);
    }
    else if($this->format=="PNG"){
        $this->img = imagecreatefrompng($this->imagePath);
    }
    else if ($this->format=="GIF"){
        $this->img = imagecreatefromgif($this->imagePath);
    }
    else if ($this->format="WBMP"){
        $this->img = imagecreatefromwbmp($this->imagePath);
    }else{
        echo "Not Supported File";
        exit();
    }
}

public function setFontFile($fontFile){
    $this->fontFile = $fontFile;

    //recalculate the text position depending on the new font file
    $this->setPosition($this->position["x"], $this->position["y"]);
}

public function setFontSize($fontSize){
    $this->fontSize = $fontSize;

    //recalculate the text position depending on the new font size
    $this->setPosition($this->position["x"], $this->position["y"]);
}

private function getTextDimensions(){
    $dimensions = imagettfbbox($this->fontSize, 0, $this->fontFile, $this->text);

    $minX = min(array($dimensions[0],$dimensions[2],$dimensions[4],$dimensions[6]));
    $maxX = max(array($dimensions[0],$dimensions[2],$dimensions[4],$dimensions[6]));

    $minY = min(array($dimensions[1],$dimensions[3],$dimensions[5],$dimensions[7]));
    $maxY = max(array($dimensions[1],$dimensions[3],$dimensions[5],$dimensions[7]));

    return array(
        'width' => $maxX - $minX,
        'heigh' => $maxY - $minY
    );
}  
}

另一个:

    require_once 'class.textPainter.php';

$x = $_GET["x"];
$y = $_GET["y"];

$R = $_GET["r"];
$G = $_GET["g"];
$B = $_GET["b"];

$size = $_GET["size"];

$text = $_GET["text"];

$text = wordwrap($text, 20, "\n", false);

$img = new textPainter('sabiasBk.png', $text, 'Franklin.ttf', $size);

if(!empty($x) && !empty($y)){
    $img->setPosition($x, $y);
}

if(!empty($R) && !empty($G) && !empty($B)){
    $img->setTextColor($R,$G,$B);
}

$img->show();

使用一行可以正常工作:

With one line this works fine

这是失败的:

This is the fail

0 个答案:

没有答案
相关问题