Skip to content

Instantly share code, notes, and snippets.

@simkimsia
Created June 22, 2012 07:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save simkimsia/2971092 to your computer and use it in GitHub Desktop.
Save simkimsia/2971092 to your computer and use it in GitHub Desktop.
annotate english or chinese text on image
<?php
function wordWrapAnnotation(&$image, &$draw, $text, $maxWidth)
{
$words = explode(" ", $text);
$lines = array();
$i = 0;
$lineHeight = 0;
while($i < count($words) )
{
$currentLine = $words[$i];
if($i+1 >= count($words))
{
$lines[] = $currentLine;
break;
}
//Check to see if we can add another word to this line
$metrics = $image->queryFontMetrics($draw, $currentLine . ' ' . $words[$i+1]);
while($metrics['textWidth'] <= $maxWidth)
{
//If so, do it and keep doing it!
$currentLine .= ' ' . $words[++$i];
if($i+1 >= count($words))
break;
$metrics = $image->queryFontMetrics($draw, $currentLine . ' ' . $words[$i+1]);
}
//We can't add the next word to this line, so loop to the next line
$lines[] = $currentLine;
$i++;
//Finally, update line height
if($metrics['textHeight'] > $lineHeight)
$lineHeight = $metrics['textHeight'];
}
return array($lines, $lineHeight);
}
function isThisEnglishText($text) {
return preg_match("/^[a-zA-Z0-9 ?!\']*$/u", $string);
}
$englishText = isThisEnglishText($text1);
$fontfiles = array(
'en' => 'AmericanTypewriter.ttc',
'zh' => 'STHeiTi.ttf'
);
$fontfile = $fontfiles['zh'];
if ($englishText) {
$fontfile = $fontfiles['en'];
}
$twoLines = !empty($text1) && !empty($text2);
$oneLine = !$twoLines;
// Create a new file for the image to be written
$new_file = 'img/overlayd_'.time().'.png';
// Resizing the uploaded image
$resized_photo = new Imagick($temp_name);
// Write the image to a new file so that we can put the overlay text later
$resized_photo->setImageFormat('png');
$resized_photo->writeImage($new_file);
// Now create the overlay text
$overlay = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( 'transparent' );
// Use the same width as the image or up to you
$overlay->newImage(260, 75, $pixel);
// Set fill color
$draw->setFillColor('#FEF94B');
// set utf 8 format
$draw->setTextEncoding('utf-8');
// Set font. Check your server for available fonts.
$draw->setFont($fontfile);
$draw->setFontSize( $fontsize );
// Create the text
list($lines1, $lineHeight) = wordWrapAnnotation($overlay, $draw, utf8_decode($text1), $maxWidthAllowedForText);
list($lines2, $lineHeight) = wordWrapAnnotation($overlay, $draw, utf8_decode($text2), $maxWidthAllowedForText);
$lines = array_merge($lines1, $lines2);
$angle = 0; // angle at which the word is printed
$overlay->annotateImage($draw, $xpos, $ypos + 0*$heightPerLine, $angle, $lines[0]);
if ($twoLines) {
$overlay->annotateImage($draw, $xpos2, $ypos2 + 1*$heightPerLine, $angle, $lines[1]);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment