I am building an application that creates a label for a manufacturing company… it finds an appropriate font size for given text, and then draws it and displays the image as a bmp(it is required to be a bmp). That is all fine and dandy and working correctly. The problem is that the label must have a depth of 2 bits for it to work correctly on their machine…I am using the following code, but cannot get a bit depth lower than 24. iMagick and GD are both available on the server.
$text = $_GET['text'];
if (isset($text)) {
$image = new Imagick();
$image->setResolution(71.5, 71.5);
$image->newImage(160, 40, new ImagickPixel('black'));
$draw = new ImagickDraw();
$draw->setFillColor('white');
$draw->setStrokeColor(new ImagickPixel('black'));
$draw->rectangle(0, 0, 159, 39);
$font_size = 25;
$font_name = 'arial.ttf';
do
{
$font_size--;
$bbox=imagettfbbox($font_size, 0, $font_name, stripcslashes($text));
$right_text = $bbox[2];
$left_text = $bbox[0];
$width_text = $right_text - $left_text;
$height_text = abs($bbox[7] - $bbox[1]);
}
while ($font_size>9 && ($height_text>40 || $width_text>160));
if ($height_text>40 || $width_text>160 || $font_size<10)
{
echo 'The text given will not fit on the label.<br />';
}
else
{
$draw->setFont($font_name);
$draw->setFontSize($font_size);
$draw->setFillColor('black');
$draw->setTextAntialias(false);
$draw->setGravity (Imagick::GRAVITY_CENTER);
$draw->annotation(0, 0, stripcslashes($text));
$image->drawImage($draw);
$image->blackthresholdImage( "#999999" );
$image->whitethresholdImage( "#999999" );
$image->setImageType(1);
$image->setImageFormat('bmp');
$image->writeImage("flavor_labels/".$text.".bmp");
echo '<img src="flavor_labels/'.$text.'.bmp" /><br /><p>Right click this image and click Save As or Save Image As. <br />Enter in the desired label name and save it to the desired destination.</p>';
}
}