Changeset View
Changeset View
Standalone View
Standalone View
src/applications/macro/engine/PhabricatorMemeEngine.php
| Show First 20 Lines • Show All 230 Lines • ▼ Show 20 Lines | for ($ii = 0; $ii < $frames; $ii++) { | ||||
| $output_files[] = $output_name; | $output_files[] = $output_name; | ||||
| $frame_data = Filesystem::readFile($frame_name); | $frame_data = Filesystem::readFile($frame_name); | ||||
| $memed_frame_data = $this->newGDAsset($template, $frame_data); | $memed_frame_data = $this->newGDAsset($template, $frame_data); | ||||
| Filesystem::writeFile($output_name, $memed_frame_data); | Filesystem::writeFile($output_name, $memed_frame_data); | ||||
| } | } | ||||
| $future = new ExecFuture('convert -loop 0 %Ls %s', $output_files, $output); | $future = new ExecFuture( | ||||
| 'convert -dispose background -loop 0 %Ls %s', | |||||
| $output_files, | |||||
| $output); | |||||
| $future->setTimeout(10)->resolvex(); | $future->setTimeout(10)->resolvex(); | ||||
| return Filesystem::readFile($output); | return Filesystem::readFile($output); | ||||
| } | } | ||||
| private function newGDAsset(PhabricatorFile $template, $data) { | private function newGDAsset(PhabricatorFile $template, $data) { | ||||
| $img = imagecreatefromstring($data); | $img = imagecreatefromstring($data); | ||||
| if (!$img) { | if (!$img) { | ||||
| ▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | final class PhabricatorMemeEngine extends Phobject { | ||||
| private function getMetrics($dim_x, $dim_y) { | private function getMetrics($dim_x, $dim_y) { | ||||
| if ($this->metrics === null) { | if ($this->metrics === null) { | ||||
| $font = $this->getFont(); | $font = $this->getFont(); | ||||
| $font_max = 72; | $font_max = 72; | ||||
| $font_min = 5; | $font_min = 5; | ||||
| $margin_x = 16; | |||||
| $margin_y = 16; | |||||
| $last = null; | $last = null; | ||||
| $cursor = floor(($font_max + $font_min) / 2); | $cursor = floor(($font_max + $font_min) / 2); | ||||
| $min = $font_min; | $min = $font_min; | ||||
| $max = $font_max; | $max = $font_max; | ||||
| $texts = array( | $texts = array( | ||||
| 'above' => $this->getAboveText(), | 'above' => $this->getAboveText(), | ||||
| 'below' => $this->getBelowText(), | 'below' => $this->getBelowText(), | ||||
| ); | ); | ||||
| $metrics = null; | $metrics = null; | ||||
| $best = null; | $best = null; | ||||
| while (true) { | while (true) { | ||||
| $all_fit = true; | $all_fit = true; | ||||
| $text_metrics = array(); | $text_metrics = array(); | ||||
| foreach ($texts as $key => $text) { | foreach ($texts as $key => $text) { | ||||
| $box = imagettfbbox($cursor, 0, $font, $text); | $box = imagettfbbox($cursor, 0, $font, $text); | ||||
| $height = abs($box[3] - $box[5]); | $height = abs($box[3] - $box[5]); | ||||
| $width = abs($box[0] - $box[2]); | $width = abs($box[0] - $box[2]); | ||||
| // This is the number of pixels below the baseline that the | // This is the number of pixels below the baseline that the | ||||
| // text extends, for example if it has a "y". | // text extends, for example if it has a "y". | ||||
| $descend = $box[3]; | $descend = $box[3]; | ||||
| if ($height > $dim_y) { | if (($height + $margin_y) > $dim_y) { | ||||
| $all_fit = false; | $all_fit = false; | ||||
| break; | break; | ||||
| } | } | ||||
| if ($width > $dim_x) { | if (($width + $margin_x) > $dim_x) { | ||||
| $all_fit = false; | $all_fit = false; | ||||
| break; | break; | ||||
| } | } | ||||
| $text_metrics[$key]['width'] = $width; | $text_metrics[$key]['width'] = $width; | ||||
| $text_metrics[$key]['height'] = $height; | $text_metrics[$key]['height'] = $height; | ||||
| $text_metrics[$key]['descend'] = $descend; | $text_metrics[$key]['descend'] = $descend; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 47 Lines • Show Last 20 Lines | |||||