protected function mockBox($im) { imagealphablending($im, true); imagesavealpha($im, true); $box = new Box($im); $box->setFontFace(__DIR__ . '/LinLibertine_R.ttf'); // http://www.dafont.com/franchise.font $box->setFontColor(new Color(255, 75, 140)); $box->setFontSize(16); $box->setBackgroundColor(new Color(0, 0, 0)); $box->setBox(0, 10, imagesx($im), 150); return $box; }
/** * Generates button image to memory */ public function generate() { if ($this->config['signature']) { $im = imagecreatefrompng($this->resourcesDir . '/signatures/en.png'); $this->elements['signature'] = array('im' => $im, 'width' => imagesx($im), 'height' => 17); } else { $this->elements['signature'] = null; } switch ($this->config['theme']) { case 'green': case 'blue': case 'pink': $this->button['fontColor'] = new Color(255, 255, 255, 0); $this->button['shadowColor'] = new Color(0, 0, 0, 70); break; case 'red': case 'black': case 'yellow': $this->button['fontColor'] = new Color(255, 255, 255, 0); $this->button['shadowColor'] = new Color(0, 0, 0, 60); break; case 'grey': $this->button['fontColor'] = new Color(52, 52, 52, 0); $this->button['shadowColor'] = new Color(249, 249, 249, 0); break; default: throw new \InvalidArgumentException('Invalid theme.'); } $im = imagecreatefrompng($this->resourcesDir . '/themes/' . $this->config['theme'] . '.png'); $this->button['im'] = $im; $this->button['width'] = imagesx($im); $this->button['height'] = imagesy($im); $this->button['shadowY'] = -1; if ((int) $this->config['priority'] <= 5 and (int) $this->config['priority'] >= 1) { $this->elements['priority']['val'] = (int) $this->config['priority']; $this->elements['priority']['height'] = 24; } else { $this->elements['priority'] = null; } if ((int) $this->config['priority'] <= 5 and (int) $this->config['priority'] >= 1) { $this->elements['priority']['val'] = (int) $this->config['priority']; $this->elements['priority']['height'] = 24; } else { $this->elements['priority'] = null; } if (!empty($this->config['captionText'])) { if (strlen($this->config['captionText']) < 200) { $this->elements['caption']['text'] = $this->config['captionText']; $this->elements['caption']['height'] = 27; $this->elements['caption']['y'] = 3; } else { throw new \InvalidArgumentException('Invalid caption'); } } else { $this->elements['caption'] = null; } $width = $this->button['width']; $height = 0; if (!is_null($this->elements['priority']) && is_null($this->elements['caption'])) { $height += 37; } elseif (is_null($this->elements['priority']) && !is_null($this->elements['caption'])) { $height += 37; } elseif (!is_null($this->elements['priority']) && !is_null($this->elements['caption'])) { $height += 60; } if (!is_null($this->elements['signature'])) { $height += 17; } $height += $this->button['height']; $this->im = imagecreatetruecolor($width, $height + 10); imagesavealpha($this->im, true); imagefill($this->im, 0, 0, imagecolorallocatealpha($this->im, 0, 0, 0, 127)); /** * MoD signature */ if (!is_null($this->elements['signature'])) { imagecopy($this->im, $this->elements['signature']['im'], ($width - $this->elements['signature']['width']) / 2, 0, 0, 0, $this->elements['signature']['width'], $this->elements['signature']['height']); } /** * Additional information */ if (!is_null($this->elements['priority']) and is_null($this->elements['caption']) or is_null($this->elements['priority']) and !is_null($this->elements['caption'])) { $footer = imagecreatefrompng($this->resourcesDir . '/themes/f1.png'); imagecopy($this->im, $footer, (imagesx($this->im) - imagesx($footer)) / 2, 59 + $this->elements['signature']['height'], 0, 0, imagesx($footer), imagesy($footer)); } if (!is_null($this->elements['priority']) and !is_null($this->elements['caption'])) { $this->elements['priority']['y'] = -13; $footer = imagecreatefrompng($this->resourcesDir . '/themes/f2.png'); imagecopy($this->im, $footer, (imagesx($this->im) - imagesx($footer)) / 2, 53 + $this->elements['signature']['height'], 0, 0, imagesx($footer), imagesy($footer)); } /** * Priorities */ if (!is_null($this->elements['priority'])) { $x = 154; $y = 72 + $this->elements['signature']['height']; if ($this->elements['priority']['val'] < 5) { $starI = imagecreatefrompng($this->resourcesDir . '/stars/s0.png'); } else { throw new \InvalidArgumentException("Too high priority has been specified."); } $starA = imagecreatefrompng($this->resourcesDir . '/stars/s' . $this->elements['priority']['val'] . '.png'); for ($i = 0; $i < $this->elements['priority']['val']; $i++) { imagecopy($this->im, $starA, $x + $i * 18, $y, 0, 0, 17, 17); } for ($i = 0; $i < 5 - $this->elements['priority']['val']; $i++) { imagecopy($this->im, $starI, $x + $this->elements['priority']['val'] * 18 + $i * 18, $y, 0, 0, 17, 17); } $type = new Box($this->im); $type->setFontFace($this->resourcesDir . '/fonts/d.otf'); $type->setFontSize(16); $type->setLineHeight(1.25); $type->setTextShadow(new Color(255, 255, 255), 0, 1); $type->setFontColor(new Color(75, 75, 75)); $type->setTextAlign('right', 'center'); $type->setBox(17, 5 + $this->button['height'] + $this->elements['signature']['height'], 130, 20); $type->draw($this->config['priorityLabel']); } if (!is_null($this->elements['caption'])) { $type = new Box($this->im); $type->setFontFace($this->resourcesDir . '/fonts/bn.ttf'); $type->setFontSize(21); $type->setTextShadow(new Color(255, 255, 255), 0, 1); $type->setFontColor(new Color(52, 52, 52)); $type->setTextAlign('center', 'center'); $type->setBox(10, $this->button['height'] + $this->elements['priority']['height'] + $this->elements['signature']['height'] + $this->elements['caption']['y'], 280, 30); $type->draw($this->elements['caption']['text']); } imagecopy($this->im, $this->button['im'], 0, $this->elements['signature']['height'], 0, 0, $this->button['width'], $this->button['height']); $type = new Box($this->im); $type->setFontFace($this->config['fontPath']); $type->setFontSize($this->config['fontSize']); $type->setLineHeight(0.8); $type->setFontColor($this->button['fontColor']); $type->setTextShadow($this->button['shadowColor'], 0, $this->button['shadowY']); $type->setTextAlign('center', 'center'); $type->setBox(0, 4 + $this->elements['signature']['height'], $this->button['width'], $this->button['height']); $type->draw($this->config['titleText']); imagesavealpha($this->im, true); }
private function buildImage($p_sText, $p_aColor, $p_aBackgroundColor) { $this->createImage($p_aColor, $p_aBackgroundColor); if (!empty($p_sText)) { $iWidth = self::IMAGE_WIDTH; $iHeight = self::IMAGE_HEIGHT; $sText = strtoupper($p_sText); $aLines = explode("\n", $sText); $iLongestLine = 0; foreach ($aLines as $t_sLine) { $iLineLength = strlen($t_sLine); if ($iLineLength > $iLongestLine) { $iLongestLine = $iLineLength; } } $iFontSize = 1500 / $iLongestLine; $box = new Box($this->m_rImage); $box->setFontFace(__DIR__ . '/../../fonts/OpenSans-Bold.ttf'); $box->setFontColor(new Color($p_aColor['red'], $p_aColor['green'], $p_aColor['blue'])); //$box->setTextShadow(new Color(0, 0, 0, 50), 2, 2); $box->setFontSize($iFontSize); $box->setBox(0, 0, $iWidth, $iHeight); $box->setTextAlign('center', 'center'); $box->draw($sText); } }
/** * Generate letter image and return image * * @param $letter * @param $color * @param $size * @return resource */ protected function createImage($letter, $color, $size) { $this->img = imagecreatetruecolor($size, $size); $bgColor = imagecolorallocate($this->img, $color[0], $color[2], $color[1]); imagefill($this->img, 0, 0, $bgColor); $box = new Box($this->img); $box->setFontFace($this->getFontFile()); $box->setFontColor($this->getTextColor()); if ($this->showTextShadow) { $box->setTextShadow(new Color(0, 0, 0, 50), 2, 2); } $box->setFontSize(round($size * $this->fontRatio)); $box->setBox(0, 0, $size, $size); $box->setTextAlign('center', 'center'); $box->draw($letter); }