Skip to content

Instantly share code, notes, and snippets.

@simkimsia
Created June 22, 2012 07:45
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save simkimsia/2971092 to your computer and use it in GitHub Desktop.
annotate english or chinese text on image
<?php
header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding('utf-8');
function wordWrapAnnotation(&$image, &$draw, $text, $maxWidth)
{
// separate the text by chinese characters or words or spaces
preg_match_all('/([\w]+)|(.)/u', $text, $matches);
// $words is array of Chinese characters, English Words or spaces
$words = $matches[0];
$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 isThisChineseText($text) {
return preg_match("/\p{Han}+/u", $text);
}
$text1 = $_POST['overlay_text_line1'];
$text2 = $_POST['overlay_text_line2'];
$temp_name = 'labels/' . $_POST['file'];
$xpos = $_POST['xpos'];
$ypos = $_POST['ypos'];
$xpos2 = $_POST['xpos2'];
$ypos2 = $_POST['ypos2'];
$fontsize = $_POST['fontsize'];
$maxWidthAllowedForText = $_POST['maxwidth'];
$heightPerLine = $_POST['line_height'];
$chineseText = isThisChineseText($text1);
$englishText = !$chineseText;
$fontfiles = array(
'en' => 'AmericanTypewriter.ttc',
'zh' => 'STHeiTi.ttf'
);
$fontfile = $fontfiles['zh'];
if ($englishText) {
$fontfile = $fontfiles['en'];
print 'english' ;
} else {
print 'chinese';
}
$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, $text1, $maxWidthAllowedForText, $chineseText);
list($lines2, $lineHeight) = wordWrapAnnotation($overlay, $draw, $text2, $maxWidthAllowedForText, $chineseText);
$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