Skip to content
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));
}
}
}