Skip to content

Instantly share code, notes, and snippets.

@chandrapatel
Forked from simkimsia/annotate.php
Created May 11, 2016 13:37
Show Gist options
  • Select an option

  • Save chandrapatel/b28531d8656f0b7dee514cb3117c60f5 to your computer and use it in GitHub Desktop.

Select an option

Save chandrapatel/b28531d8656f0b7dee514cb3117c60f5 to your computer and use it in GitHub Desktop.

Revisions

  1. simkimsia revised this gist Jun 27, 2012. 1 changed file with 34 additions and 11 deletions.
    45 changes: 34 additions & 11 deletions annotate.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,14 @@
    <?php
    header('Content-Type: text/html; charset=utf-8');
    mb_internal_encoding('utf-8');

    function wordWrapAnnotation(&$image, &$draw, $text, $maxWidth)
    {
    $words = explode(" ", $text);
    // 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;
    @@ -14,14 +21,14 @@ function wordWrapAnnotation(&$image, &$draw, $text, $maxWidth)
    break;
    }
    //Check to see if we can add another word to this line
    $metrics = $image->queryFontMetrics($draw, $currentLine . ' ' . $words[$i+1]);
    $metrics = $image->queryFontMetrics($draw, $currentLine . $words[$i+1]);
    while($metrics['textWidth'] <= $maxWidth)
    {
    //If so, do it and keep doing it!
    $currentLine .= ' ' . $words[++$i];
    $currentLine .= $words[++$i];
    if($i+1 >= count($words))
    break;
    $metrics = $image->queryFontMetrics($draw, $currentLine . ' ' . $words[$i+1]);
    $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;
    @@ -33,13 +40,25 @@ function wordWrapAnnotation(&$image, &$draw, $text, $maxWidth)
    return array($lines, $lineHeight);
    }

    function isThisEnglishText($text) {
    return preg_match("/^[a-zA-Z0-9 ?!\']*$/u", $string);
    function isThisChineseText($text) {
    return preg_match("/\p{Han}+/u", $text);
    }


    $englishText = isThisEnglishText($text1);
    $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',
    @@ -49,7 +68,10 @@ function isThisEnglishText($text) {

    $fontfile = $fontfiles['zh'];
    if ($englishText) {
    $fontfile = $fontfiles['en'];
    $fontfile = $fontfiles['en'];
    print 'english' ;
    } else {
    print 'chinese';
    }

    $twoLines = !empty($text1) && !empty($text2);
    @@ -78,15 +100,15 @@ function isThisEnglishText($text) {
    $draw->setFillColor('#FEF94B');

    // set utf 8 format
    $draw->setTextEncoding('utf-8');
    $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);
    list($lines1, $lineHeight) = wordWrapAnnotation($overlay, $draw, $text1, $maxWidthAllowedForText, $chineseText);
    list($lines2, $lineHeight) = wordWrapAnnotation($overlay, $draw, $text2, $maxWidthAllowedForText, $chineseText);

    $lines = array_merge($lines1, $lines2);

    @@ -97,4 +119,5 @@ function isThisEnglishText($text) {
    if ($twoLines) {
    $overlay->annotateImage($draw, $xpos2, $ypos2 + 1*$heightPerLine, $angle, $lines[1]);
    }

    ?>
  2. simkimsia revised this gist Jun 22, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion annotate.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    function wordWrapAnnotation(&$image, &$draw, $text, $maxWidth)
    {
    $words = explode(" ", $text);
    @@ -95,4 +96,5 @@ function isThisEnglishText($text) {

    if ($twoLines) {
    $overlay->annotateImage($draw, $xpos2, $ypos2 + 1*$heightPerLine, $angle, $lines[1]);
    }
    }
    ?>
  3. simkimsia revised this gist Jun 22, 2012. No changes.
  4. simkimsia revised this gist Jun 22, 2012. 1 changed file with 39 additions and 0 deletions.
    39 changes: 39 additions & 0 deletions annotate.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,42 @@
    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);


  5. simkimsia created this gist Jun 22, 2012.
    59 changes: 59 additions & 0 deletions annotate.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    $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]);
    }