Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initTextLayerWithMaxWidth to init a text layer with a given max width #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/PHPImageWorkshop/ImageWorkshop.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,47 @@ public static function initTextLayer($text, $fontPath, $fontSize = 13, $fontColo
return $layer;
}

/**
* Initialize a text layer With a given Width
*
* @param string $text
* @param string $fontPath
* @param integer $fontSize
* @param string $fontColor
* @param integer $textRotation
* @param integer $backgroundColor
* @param integer $maxWidth
*
* @return ImageWorkshopLayer
*/
public static function initTextLayerWithMaxWidth($text, $fontPath, $fontSize = 13, $fontColor = 'ffffff', $textRotation = 0, $backgroundColor = null,$maxWidth=200)
{
$txt_clean = $text;
$textDimensions = ImageWorkshopLib::getTextBoxDimension($fontSize, $textRotation, $fontPath, $text);
$resetMaxWith=false;
while($textDimensions['width'] > $maxWidth ){
if(!$resetMaxWith)
{
/*
* on prevoit 5 pixels pour les 3 points
*/
$maxWidth = $maxWidth -5;
$resetMaxWith = true;
}

$txt_clean = substr($txt_clean, 0,-1);
$text = $txt_clean.'...';
$textDimensions = ImageWorkshopLib::getTextBoxDimension($fontSize, $textRotation, $fontPath, $text);
}



$layer = static::initVirginLayer($textDimensions['width'], $textDimensions['height'], $backgroundColor);
$layer->write($text, $fontPath, $fontSize, $fontColor, $textDimensions['left'], $textDimensions['top'], $textRotation);

return $layer;
}

/**
* Initialize a new virgin layer
*
Expand Down Expand Up @@ -142,4 +183,4 @@ public static function initFromString($imageString)
{
return new ImageWorkshopLayer(imageCreateFromString($imageString));
}
}
}