/** * function filter_render_type * Returns HTML markup containing an image with a data: URI * @param string $content The text to be rendered * @param string $font_file The path to a font file in a format ImageMagick can handle * @param integer $font_size The font size, in pixels (defaults to 28) * @param string $font_color The font color (defaults to 'black') * @param string $background_color The background color (defaults to 'transparent') * @param string $output_format The image format to use (defaults to 'png') * @return string HTML markup containing the rendered image **/ public function filter_render_type($content, $font_file, $font_size, $font_color, $background_color, $output_format, $width) { // Preprocessing $content // 1. Strip HTML tags. It would be better to support them, but we just strip them for now. // 2. Decode HTML entities to UTF-8 charaaters. $content = html_entity_decode(strip_tags($content), ENT_QUOTES, 'UTF-8'); // 3. Do word wrap when $width is specified. Not work for double-width characters. if (is_int($width)) { $content = wordwrap($content, floor($width / (0.3 * $font_size))); } $cache_group = strtolower(get_class($this)); $cache_key = $font_file . $font_size . $font_color . $background_color . $output_format . $content . $width; if (!Cache::has(array($cache_group, md5($cache_key)))) { $font_color = new ImagickPixel($font_color); $background_color = new ImagickPixel($background_color); $draw = new ImagickDraw(); $draw->setFont($font_file); $draw->setFontSize($font_size); $draw->setFillColor($font_color); $draw->setTextEncoding('UTF-8'); $draw->annotation(0, $font_size * 2, $content); $canvas = new Imagick(); $canvas->newImage(1000, $font_size * 5, $background_color); $canvas->setImageFormat($output_format); $canvas->drawImage($draw); // The following line ensures that the background color is set in the PNG // metadata, when using that format. This allows you, by specifying an RGBa // background color (e.g. #ffffff00) to create PNGs with a transparent background // for browsers that support it but with a "fallback" background color (the RGB // part of the RGBa color) for IE6, which does not support alpha in PNGs. $canvas->setImageBackgroundColor($background_color); $canvas->trimImage(0); Cache::set(array($cache_group, md5($cache_key)), $canvas->getImageBlob()); } return '<img class="rendered-type" src="' . URL::get('display_rendertype', array('hash' => md5($cache_key), 'format' => $output_format)) . '" title="' . $content . '" alt="' . $content . '">'; }
function generate_final_video_frames($converted_avatars, $uid, $dir = 'data', $videos_frames) { include get_template_directory() . '/video_libs/libs.php'; $frames = array(); $i = 1; $converted_path = $dir . "/{$uid}/frames"; if (!file_exists("{$converted_path}")) { exec("mkdir -p {$converted_path}"); } exec("chmod 777 -R {$converted_path}"); exec("rm -rf {$converted_path}/*"); #include get_template_directory() . '/video_libs/data.php'; /*include get_template_directory() . '/video_libs/data_live.php'; $videos_frames = get_data_videos($_REQUEST['uid'], $_REQUEST['eid']);*/ foreach ($videos_frames as $key => $value) { $name = generate_filename($i); $filename = $value->imageFrame; $curr_frame = "{$converted_path}/{$name}"; $frames[] = $curr_frame; $text_objs = isset($value->text) ? $value->text : array(); $image_objs = isset($value->imageFace) ? $value->imageFace : array(); if (array_key_exists($filename, $converted_avatars) && !empty($converted_avatars[$filename])) { if (!empty($image_objs)) { foreach ($image_objs as $image_obj) { $order = $image_obj->order; if ($order == 'back') { $msg = exec("convert {$converted_avatars[$filename]} {$filename} -composite {$curr_frame}"); } else { $msg = exec("convert {$filename} {$converted_avatars[$filename]} -composite {$curr_frame}"); } } } } else { exec("cp {$filename} {$curr_frame}"); } $w = 210; if (!empty($text_objs)) { foreach ($text_objs as $text_obj) { $picin = new Imagick($curr_frame); $draw = new ImagickDraw(); $draw->setTextEncoding('utf-8'); $draw->setFillColor("rgba(255, 255, 255, {$text_obj->opacity})"); $draw->setFont($text_obj->font); $draw->setFontSize($text_obj->size); $positions = explode(',', $text_obj->position); //list($lines, $lineHeight)= wordWrapAnnotation($picin, $draw, $text_obj->text, $w-10); if (!empty($text_obj->break) && $text_obj->break == 'yes') { $y = $positions[1]; $line_height = 55; $str = wordwrap($text_obj->text, 19, "\n"); $str_array = explode("\n", $str); foreach ($str_array as $line) { $picin->annotateImage($draw, $positions[0], $y, 0, $line); $y += $line_height; } } else { $picin->annotateImage($draw, $positions[0], $positions[1], 0, $text_obj->text); } //$picin->annotateImage($draw, $positions[0], $positions[1], 0, $text_obj->text); //$picin->annotateImage($draw, $positions[0], $positions[1], 0, $lines); $picin->writeimage($curr_frame); /*if( strpos($filename, '110.png')){ echo '<pre>'; print_r($image_obj); echo '</pre>'; echo 'aaaa';die; }*/ } } $i++; } return $frames; }
var_dump($draw->getGravity() === Imagick::GRAVITY_SOUTHEAST); // stroke $draw->setStrokeAntialias(false); var_dump($draw->getStrokeAntialias()); $draw->setStrokeColor(new ImagickPixel('#F02B88')); var_dump($draw->getStrokeColor()->getColor()); $draw->setStrokeDashArray(array(1, 2, 3)); var_dump($draw->getStrokeDashArray()); $draw->setStrokeDashOffset(-1); var_dump($draw->getStrokeDashOffset()); $draw->setStrokeLineCap(Imagick::LINECAP_SQUARE); var_dump($draw->getStrokeLineCap() === Imagick::LINECAP_SQUARE); $draw->setStrokeLineJoin(Imagick::LINEJOIN_BEVEL); var_dump($draw->getStrokeLineJoin() === Imagick::LINEJOIN_BEVEL); $draw->setStrokeMiterLimit(3); var_dump($draw->getStrokeMiterLimit()); $draw->setStrokeOpacity(0.9); printf("%.2f\n", $draw->getStrokeOpacity()); $draw->setStrokeWidth(1.2); printf("%.2f\n", $draw->getStrokeWidth()); // text $draw->setTextAlignment(Imagick::ALIGN_CENTER); var_dump($draw->getTextAlignment() === Imagick::ALIGN_CENTER); $draw->setTextAntialias(false); var_dump($draw->getTextAntialias()); $draw->setTextDecoration(Imagick::DECORATION_LINETROUGH); var_dump($draw->getTextDecoration() === Imagick::DECORATION_LINETROUGH); $draw->setTextEncoding('UTF-8'); var_dump($draw->getTextEncoding()); $draw->setTextUnderColor('cyan'); var_dump($draw->getTextUnderColor()->getColor());
function text2image($text, $width, $margin, $size, $font, $align, $stroke, $bg, $color, $scolor, $out) { $text = html_entity_decode($text); $lines = array(); //break up the text on the line $imageForMetric = new Imagick(); $drawForMetric = new ImagickDraw(); $drawForMetric->setFont($font); $drawForMetric->setFontSize($size); $explicitLines = preg_split('/\\n|\\r\\n?/', $text); $key = 0; foreach ($explicitLines as $line) { $words = explode(" ", $line); $line = $words[0]; for ($i = 1; $i < count($words); $i++) { if ($imageForMetric->queryFontMetrics($drawForMetric, $line . " " . $words[$i])['textWidth'] >= $width - $margin * 2) { $key++; $lines[$key]['text'] = $line; $line = $words[$i]; } else { $line .= " " . $words[$i]; } } $key++; $lines[$key]['text'] = $line; $lines[$key]['type'] = 'last'; } $imageForMetric->destroy(); $drawForMetric->destroy(); //line height $lineHeightPx = $size * 1.5; //margin top and bottom (can leave $pad=$margin;) if ($size < 16) { $pad = $size / 2; } elseif ($size < 26) { $pad = $size / 3; } elseif ($size < 36) { $pad = $size / 4; } else { $pad = 0; } //height image $textHeight = count($lines) * $lineHeightPx + $pad * 2; $image = new Imagick(); $draw = new ImagickDraw(); $pixel = new ImagickPixel('#' . $bg); $image->newImage($width, $textHeight + $lineHeightPx / 2, $pixel); $draw->setFillColor('#' . $color); $draw->setFont($font); $draw->setFontSize($size); //for setTextAlignment if ($align == 'center') { $alignNumber = 2; } elseif ($align == 'right') { $alignNumber = 3; } else { $alignNumber = 1; } $draw->setTextAlignment($alignNumber); $draw->setTextEncoding('UTF-8'); //x start position if ($align == 'center') { $xpos = $width / 2; } elseif ($align == 'right') { $xpos = $width - $margin; } else { $xpos = $margin; } //text stroke if ($stroke) { $drawstroke = new ImagickDraw(); $drawstroke->setFillColor('#' . $scolor); $drawstroke->setFont($font); $drawstroke->setFontSize($size); $drawstroke->setTextAlignment($alignNumber); $drawstroke->setTextEncoding('UTF-8'); } //y start position $ypos = $lineHeightPx + $pad; $i = 0; foreach ($lines as $value => $line) { if ($align == 'justify' && (!isset($lines[$value]['type']) || $lines[$value]['type'] != 'last')) { //justify for all lines (except last) $line = trim($lines[$value]['text']); $sp = 0; $j = 0; $line = preg_replace("/ +/", " ", $line); $spaces = substr_count($line, ' '); $word = explode(' ', $line); //word metrics $metrics = 0; foreach ($word as $w) { $metrics = $metrics + $image->queryFontMetrics($draw, $w)['textWidth']; } $spacewidth = ($width - $margin * 2 - $metrics) / $spaces; foreach ($word as $w) { if ($w !== '') { $j++; if ($j > 1) { $sp = $sp + $spacewidth; } if ($stroke) { //draw stroke $ys = 0; $xs = $stroke; $d = (5 - $stroke * 4) / 4; while ($xs >= $ys) { $image->annotateImage($drawstroke, $xpos + $sp + $stroke, $ypos + $i * $lineHeightPx + $ys, 0, $w); $image->annotateImage($drawstroke, $xpos + $sp + $stroke, $ypos + $i * $lineHeightPx - $ys, 0, $w); $image->annotateImage($drawstroke, $xpos + $sp - $stroke, $ypos + $i * $lineHeightPx + $ys, 0, $w); $image->annotateImage($drawstroke, $xpos + $sp - $stroke, $ypos + $i * $lineHeightPx - $ys, 0, $w); $image->annotateImage($drawstroke, $xpos + $sp + $ys, $ypos + $i * $lineHeightPx + $stroke, 0, $w); $image->annotateImage($drawstroke, $xpos + $sp + $ys, $ypos + $i * $lineHeightPx - $stroke, 0, $w); $image->annotateImage($drawstroke, $xpos + $sp - $ys, $ypos + $i * $lineHeightPx + $stroke, 0, $w); $image->annotateImage($drawstroke, $xpos + $sp - $ys, $ypos + $i * $lineHeightPx - $stroke, 0, $w); if ($d < 0) { $d += 2 * $ys + 1; } else { $d += 2 * ($ys - $xs) + 1; $xs--; } $ys++; } } //draw word $image->annotateImage($draw, $xpos + $sp, $ypos + $i * $lineHeightPx, 0, $w); //space width $sp = $sp + $image->queryFontMetrics($draw, $w)['textWidth']; } } } else { if ($stroke) { //draw stroke $ys = 0; $xs = $stroke; $d = (5 - $stroke * 4) / 4; while ($xs >= $ys) { $image->annotateImage($drawstroke, $xpos + $stroke, $ypos + $i * $lineHeightPx + $ys, 0, $lines[$value]['text']); $image->annotateImage($drawstroke, $xpos + $stroke, $ypos + $i * $lineHeightPx - $ys, 0, $lines[$value]['text']); $image->annotateImage($drawstroke, $xpos - $stroke, $ypos + $i * $lineHeightPx + $ys, 0, $lines[$value]['text']); $image->annotateImage($drawstroke, $xpos - $stroke, $ypos + $i * $lineHeightPx - $ys, 0, $lines[$value]['text']); $image->annotateImage($drawstroke, $xpos + $ys, $ypos + $i * $lineHeightPx + $stroke, 0, $lines[$value]['text']); $image->annotateImage($drawstroke, $xpos + $ys, $ypos + $i * $lineHeightPx - $stroke, 0, $lines[$value]['text']); $image->annotateImage($drawstroke, $xpos - $ys, $ypos + $i * $lineHeightPx + $stroke, 0, $lines[$value]['text']); $image->annotateImage($drawstroke, $xpos - $ys, $ypos + $i * $lineHeightPx - $stroke, 0, $lines[$value]['text']); if ($d < 0) { $d += 2 * $ys + 1; } else { $d += 2 * ($ys - $xs) + 1; $xs--; } $ys++; } } //draw line $image->annotateImage($draw, $xpos, $ypos + $i * $lineHeightPx, 0, $lines[$value]['text']); } $i++; } //save image to png $image->setCompressionQuality(100); $image->setCompression(Imagick::COMPRESSION_NO); $image->setImageFormat('png'); $image->writeImage('png24:' . $out); $image->destroy(); }