$font = $_SERVER["DOCUMENT_ROOT"]."/HDWFormCaptcha/".$font; if (!file_exists($font)) $font = dirname(__FILE__)."/".$font; */ $font_size = rand($min_size, $max_size); $angle = rand(-15, 15); if (function_exists("imagettfbbox") && function_exists("imagettftext")) { $box = imagettfbbox($font_size, $angle, $font, $str); $x = (int) ($imgX - $box[4]) / 2; $y = (int) ($imgY - $box[5]) / 2; imagettftext($image, $font_size, $angle, $x, $y, $text_col, $font, $str); } else { if (function_exists("imageFtBBox") && function_exists("imageFTText")) { $box = imageFtBBox($font_size, $angle, $font, $str); $x = (int) ($imgX - $box[4]) / 2; $y = (int) ($imgY - $box[5]) / 2; imageFTText($image, $font_size, $angle, $x, $y, $text_col, $font, $str); } else { $angle = 0; $font = 6; $wf = ImageFontWidth(6) * strlen($str); $hf = ImageFontHeight(6); $x = (int) ($imgX - $wf) / 2; $y = (int) ($imgY - $hf) / 2; imagestring($image, $font, $x, $y, $str, $text_col); } } header("Content-type: image/png"); imagepng($image); imagedestroy($image); exit;
public static function createWaterMark($source, $options) { // Get info about image to be watermarked $old_image = $source; $size = getimagesize($old_image); $imagetype = $size[2]; $usingAlpha = false; // Create GD Image Resource from original image switch ($imagetype) { case 1: $image = imagecreatefromgif($old_image); break; case 2: $image = imagecreatefromjpeg($old_image); break; case 3: $image = imagecreatefrompng($old_image); break; case 6: $image = imagecreatefrombmp($old_image); break; } // end switch($imagetype) if ($options['type'] == 'img') { $sizeWM = getimagesize($options['image']); switch ($sizeWM[2]) { case 1: $watermark = imagecreatefromgif($options['image']); break; case 2: $watermark = imagecreatefromjpeg($options['image']); break; case 3: $watermark = imagecreatefrompng($options['image']); break; case 6: $watermark = imagecreatefrombmp($options['image']); break; } //$watermark = imagecreatefromjpeg($options['image']); // Create a GD image resource from overlay image $wm_width = imagesx($watermark); // get the original width $wm_height = imagesy($watermark); // get the original height $watermark_rWidth = $wm_width * $options['factor']; // calculate new, reduced width $watermark_rHeight = $wm_height * $options['factor']; // calculate new, reduced height $watermark_r = @imagecreatetruecolor($watermark_rWidth, $watermark_rHeight); // Make transparent if ($sizeWM[2] == 3) { $usingAlpha = true; imagealphablending($watermark_r, false); imagesavealpha($watermark_r, true); $transparent = imagecolorallocatealpha($watermark_r, 255, 255, 255, 127); imagefilledrectangle($watermark_r, 0, 0, $wm_width, $wm_height, $transparent); } imagecopyresampled($watermark_r, $watermark, 0, 0, 0, 0, $watermark_rWidth, $watermark_rHeight, $wm_width, $wm_height); imagedestroy($watermark); // original watermark image no longer needed } if ($options['type'] == 'msg') { // Calculate size of overlay image for text // array imagettfbbox ( float $size , float $angle , string $fontfile , string $text ) $wm_bBox = imagettfbbox($options['fsize'], 0, $options['font'], $options['text']); $minX = min(array($wm_bBox[0], $wm_bBox[2], $wm_bBox[4], $wm_bBox[6])); $maxX = max(array($wm_bBox[0], $wm_bBox[2], $wm_bBox[4], $wm_bBox[6])); $minY = min(array($wm_bBox[1], $wm_bBox[3], $wm_bBox[5], $wm_bBox[7])); $maxY = max(array($wm_bBox[1], $wm_bBox[3], $wm_bBox[5], $wm_bBox[7])); $thebox = array("left" => abs($minX) - 1, "top" => abs($minY) - 1, "width" => $maxX - $minX, "height" => $maxY - $minY); //$watermark_rWidth = ($upperRightX - $upperLeftX)+$options['padding']; //$watermark_rHeight = ($lowerLeftY - $upperLeftY)+$options['padding']; $watermark_rWidth = $thebox["width"] + $options['padding'] * 2; $watermark_rHeight = $thebox["height"] + $options['padding'] * 2; // Create the overlay image $watermark_r = imagecreatetruecolor($watermark_rWidth, $watermark_rHeight); $userColor = self::hex2int(self::validHexColor($options['fcolor'], 'ffffff')); $txtColor = imageColorAllocate($watermark_r, $userColor['r'], $userColor['g'], $userColor['b']); if ($options['bg']) { // Set an appropriate background color $userColor = self::hex2int(self::validHexColor($options['bgcolor'], '000000')); $watermark_rBGColor = imageColorAllocate($watermark_r, $userColor['r'], $userColor['g'], $userColor['b']); imagefilledrectangle($watermark_r, 0, 0, $watermark_rWidth, $watermark_rHeight, $watermark_rBGColor); } else { // Make the background transparent $usingAlpha = true; imagealphablending($watermark_r, false); imagesavealpha($watermark_r, true); $transparent = imagecolorallocatealpha($watermark_r, 255, 255, 255, 127); imagefilledrectangle($watermark_r, 0, 0, $watermark_rWidth, $watermark_rHeight, $transparent); } // array imageTTFText ( resource image, int size, int angle, int x, int y, int color, string fontfile, string text) imageFTText($watermark_r, $options['fsize'], 0, $thebox["left"] + ($watermark_rWidth - $thebox["width"]) / 2, $thebox["top"] + ($watermark_rHeight - $thebox["height"]) / 2, $txtColor, $options['font'], $options['text']); } // endif $options['type'] == 'msg' // Handle rotation if ($options['rotate'] != 0) { if (phpversion() >= 5.1) { $bg = imagecolortransparent($watermark_r); $watermark_r = imagerotate($watermark_r, $options['rotate'], $bg, 1); } else { $watermark_r = imagerotate($watermark_r, $options['rotate'], 0); } // endif phpversion() $watermark_rWidth = imagesx($watermark_r); $watermark_rHeight = imagesy($watermark_r); } // endif $options['rotate'] !=0 // Calculate overlay image position switch ($options['position']) { case 'tl': $dest_x = $options['padding']; $dest_y = $options['padding']; break; case 'tr': $dest_x = $size[0] - $watermark_rWidth - $options['padding']; $dest_y = $options['padding']; break; case 'bl': $dest_x = $options['padding']; $dest_y = $size[1] - $watermark_rHeight - $options['padding']; break; case 'br': $dest_x = $size[0] - $watermark_rWidth - $options['padding']; $dest_y = $size[1] - $watermark_rHeight - $options['padding']; break; default: $dest_x = round(($size[0] - $watermark_rWidth) / 2); $dest_y = round(($size[1] - $watermark_rHeight) / 2); break; } // Overlay the logo watermark image on the original image // int imagecopymerge ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, int pct ); if ($usingAlpha) { self::imagecopymerge_alpha($image, $watermark_r, $dest_x, $dest_y, 0, 0, $watermark_rWidth, $watermark_rHeight, $options['opacity']); } else { imagecopymerge($image, $watermark_r, $dest_x, $dest_y, 0, 0, $watermark_rWidth, $watermark_rHeight, $options['opacity']); } imagedestroy($watermark_r); $obLevel = ob_get_level(); while ($obLevel > 0) { ob_end_clean(); $obLevel--; } switch ($imagetype) { case 1: header('Content-type: image/gif'); imagegif($image); break; case 2: header('Content-type: image/jpeg'); imagejpeg($image); break; case 3: header('Content-type: image/png'); imagepng($image); break; case 6: header('Content-type: image/bmp'); imagewbmp($image); break; } // end switch($imagetype) imagedestroy($image); exit; }