protected function RenderImage($strPath = null) { $strWidth = $this->Width; // Make Sure Font File Exists if (file_exists($this->strFontNames)) { $strFontPath = $this->strFontNames; } else { throw new QCallerException('Cannot find font file: ' . $this->strFontNames); } // Figure Out Font Type $strFontExtension = substr($this->strFontNames, strlen($this->strFontNames) - 3); $strFontExtension = strtolower($strFontExtension); // Based on Font Type, Calculate Bounding Box switch ($strFontExtension) { case 'ttf': $blnTrueType = true; $objBox = imagettfbbox($this->strFontSize, $this->intAngle, $strFontPath, $this->strText); // Calculate Bounding Box Dimensions $intXCoordinate1 = $objBox[0]; $intYCoordinate1 = $objBox[5]; $intXCoordinate2 = $objBox[4]; $intYCoordinate2 = $objBox[1]; break; case 'pfb': $blnTrueType = false; // Load Font and Calculate $objFont = imagepsloadfont($strFontPath); $objBox = imagepsbbox($this->strText, $objFont, $this->strFontSize, $this->intSpace, $this->intTightness, $this->intAngle); // Calculate Bounding Box Dimensions $intXCoordinate1 = $objBox[0]; $intYCoordinate1 = $objBox[1]; $intXCoordinate2 = $objBox[2]; $intYCoordinate2 = $objBox[3]; break; default: throw new QCallerException('Cannot Determine Font Type: ' . $this->strFontNames); } $intBoxWidth = $intXCoordinate2 - $intXCoordinate1; $intBoxHeight = $intYCoordinate2 - $intYCoordinate1; // Figure Out Image Width and Height: // 1. If no width/height set, then use bounding box + padding // 2. otherwise, if alignment, we set to alignment // 3. otherwise, use coordinates if (!$strWidth) { // Step 1 -- Use Bounding Box + Padding $intWidth = $intBoxWidth + $this->intPaddingWidth * 2; $intX = $this->intPaddingWidth; } else { // Step 2 - Alignment switch ($this->strHorizontalAlign) { case QHorizontalAlign::Left: $intX = -1 * $intXCoordinate1 + 2 + $this->intPaddingWidth; break; case QHorizontalAlign::Right: $intX = $strWidth - $intBoxWidth - 2 - $this->intPaddingWidth; break; case QHorizontalAlign::Center: $intX = round(($strWidth - $intBoxWidth) / 2); break; // Step 3 - Use Coordinates // Step 3 - Use Coordinates default: $intX = $this->intXCoordinate; break; } $intWidth = $strWidth; } if (!$this->Height) { // Step 1 -- Use Bounding Box + Padding $intHeight = $intBoxHeight + $this->intPaddingHeight * 2; if ($blnTrueType) { $intY = $intBoxHeight - $intYCoordinate2 + $this->intPaddingHeight; } else { $intY = $intYCoordinate2 + $this->intPaddingHeight + 1; } } else { // Step 2 - Alignment switch ($this->strVerticalAlign) { case QVerticalAlign::Top: if ($blnTrueType) { $intY = $intBoxHeight - $intYCoordinate2 + $this->intPaddingHeight; } else { $intY = $intYCoordinate2 + 2 + $this->intPaddingHeight; } break; case QVerticalAlign::Bottom: if ($blnTrueType) { $intY = $this->Height - $intYCoordinate2 - $this->intPaddingHeight; } else { $intY = $this->Height + $intYCoordinate1 - 2 - $this->intPaddingHeight; } break; case QVerticalAlign::Middle: if ($blnTrueType) { $intY = round(($this->Height - $intBoxHeight) / 2) + $intBoxHeight - $intYCoordinate2; } else { $intY = round(($this->Height - $intBoxHeight) / 2) + $intYCoordinate2; } break; // Step 3 - Use Coordinates // Step 3 - Use Coordinates default: $intY = $this->intYCoordinate; break; } $intHeight = $this->Height; } if ($intWidth <= 0) { $intWidth = 100; } if ($intHeight <= 0) { $intHeight = 100; } $objImage = imagecreate($intWidth, $intHeight); // Define Colors $intRed = hexdec(substr($this->strBackColor, 0, 2)); $intGreen = hexdec(substr($this->strBackColor, 2, 2)); $intBlue = hexdec(substr($this->strBackColor, 4)); $clrBackground = imagecolorallocate($objImage, $intRed, $intGreen, $intBlue); $intRed = hexdec(substr($this->strForeColor, 0, 2)); $intGreen = hexdec(substr($this->strForeColor, 2, 2)); $intBlue = hexdec(substr($this->strForeColor, 4)); $clrForeground = imagecolorallocate($objImage, $intRed, $intGreen, $intBlue); if ($this->blnBackgroundTransparent) { imagecolortransparent($objImage, $clrBackground); } imagefilledrectangle($objImage, 0, 0, $intWidth, $intHeight, $clrBackground); if ($blnTrueType) { imagettftext($objImage, $this->strFontSize, $this->intAngle, $intX, $intY, $clrForeground, $strFontPath, $this->strText); } else { // Anti Aliasing if ($this->blnSmoothFont) { $intAntiAliasing = 16; } else { $intAntiAliasing = 4; } // Draw Text and Free Font imagepstext($objImage, $this->strText, $objFont, $this->strFontSize, $clrForeground, $clrBackground, $intX, $intY, $this->intSpace, $this->intTightness, $this->intAngle, $intAntiAliasing); imagepsfreefont($objFont); } // Output the Image (if path isn't specified, output to buffer. Otherwise, output to disk) if (!$strPath) { // TODO: Update Cache Parameters QApplication::$ProcessOutput = false; header('Cache-Control: cache'); header('Expires: Wed, 20 Mar 2019 05:00:00 GMT'); header('Pragma: cache'); switch ($this->strImageType) { case QImageType::Gif: header('Content-type: image/gif'); imagegif($objImage); break; case QImageType::Jpeg: header('Content-type: image/jpeg'); imagejpeg($objImage, null, $this->intQuality); break; default: header('Content-type: image/png'); imagepng($objImage); break; } } else { switch ($this->strImageType) { case QImageType::Gif: imagegif($objImage, $strPath); break; case QImageType::Jpeg: imagejpeg($objImage, $strPath, $this->intQuality); break; default: imagepng($objImage, $strPath); break; } } imagedestroy($objImage); }
function real_string_pixels($string) { if ($this->font_type == "type1") { list($llx, $lly, $urx, $ury) = imagepsbbox($string, $this->font, $this->font_size); return $urx - $llx; } else { return strlen($string) * 6; } }
private function center_text($text, $font, $background_color, $bounding_width, $bounding_height) { /* * get the left lower corner and the right upper */ list($lx, $ly, $rx, $ry) = imagepsbbox($text, $this->font, $this->font_size); /* * calculate the size of the text */ $textW = $rx - $lx; $textH = $ry - $ly; /* * Calculate the positions */ $positionLeft = ($bounding_width - $textW) / 2; $positionTop = ($bounding_height - $textH) / 2; /* * Add some text */ imagepstext($this->image, $text, $font, $this->font_size, $this->font_color, $this->background_color, $positionLeft, $positionTop, 0, 0, 0, 16); }
function bounding_box($text, $font = NULL, $size = NULL) { global $FLIR; if (is_null($font)) { $font = $FLIR['postscript'] ? $FLIR['ps']['font'] : $FLIR['font']; } if (is_null($size)) { $size = $FLIR['postscript'] ? $FLIR['size'] : $FLIR['size_pts']; } elseif ($FLIR['postscript']) { $size = get_points($FLIR['dpi'], $size); } // convert to points if ($FLIR['postscript']) { return convertPSBoundingBox(imagepsbbox($text, $font, $size, $FLIR['ps']['space'], $FLIR['ps']['kerning'], 0)); } else { return convertBoundingBox(imagettfbbox($size, 0, $font, $text)); } }