function write_multiline_text($image, $font_size, $color, $font, $text, $start_x, $start_y, $max_width) { //split the string //build new string word for word //check everytime you add a word if string still fits //otherwise, remove last word, post current string and start fresh on a new line $words = explode(" ", $text); $string = ""; $tmp_string = ""; for ($i = 0; $i < count($words); $i++) { $tmp_string .= $words[$i] . " "; //check size of string $dim = imagettfbbox($font_size, 0, $font, $tmp_string); if ($dim[4] < $max_width) { $string = $tmp_string; } else { $i--; $tmp_string = ""; imagettftext($image, 11, 0, $start_x, $start_y, $color, $font, $string); $string = ""; $start_y += 40; //change this to adjust line-height. Additionally you could use the information from the "dim" array to automatically figure out how much you have to "move down" } } imagettftext($image, 11, 0, $start_x, $start_y, $color, $font, $string); //"draws" the rest of the string }
public function generate() { imagesavealpha($this->_owner->image, true); imagealphablending($this->_owner->image, true); $width = $this->_owner->getImageWidth(); $height = $this->_owner->getImageHeight(); $white = $this->_owner->imagecolorallocate($this->_text_color); $data = array(); $total_width = 0; for ($x = 0; $x < strlen($this->_text); $x++) { $data[$x]['text'] = $this->_text[$x]; $data[$x]['font'] = $this->_arr_ttf_font[rand(0, count($this->_arr_ttf_font) - 1)]; $data[$x]['size'] = rand($this->_text_size, $this->_text_size + $this->_text_size_random); $data[$x]['angle'] = $this->_text_angle_random / 2 - rand(0, $this->_text_angle_random); $captcha_dimensions = imagettfbbox($data[$x]['size'], $data[$x]['angle'], $data[$x]['font'], $data[$x]['text']); $data[$x]['width'] = abs($captcha_dimensions[2]) + $this->_text_spacing; $data[$x]['height'] = abs($captcha_dimensions[5]); $total_width += $data[$x]['width']; } $x_offset = ($width - $total_width) / 2; $x_pos = 0; $y_pos = 0; foreach ($data as $ld) { $y_pos = ($height + $ld['height']) / 2; imagettftext($this->_owner->image, $ld['size'], $ld['angle'], $x_offset + $x_pos, $y_pos, $white, $ld['font'], $ld['text']); $x_pos += $ld['width']; } return true; }
/** * * @param string $playername Minecraft player name * @param resource $avatar the rendered avatar (for example player head) * * @param string $background Image Path or Standard Value * @return resource the generated banner */ public static function player($playername, $avatar = NULL, $background = NULL) { $canvas = MinecraftBanner::getBackgroundCanvas(self::PLAYER_WIDTH, self::PLAYER_HEIGHT, $background); $head_height = self::AVATAR_SIZE; $head_width = self::AVATAR_SIZE; $avater_x = self::PLAYER_PADDING; $avater_y = self::PLAYER_HEIGHT / 2 - self::AVATAR_SIZE / 2; if ($avatar == NULL) { $avatar = imagecreatefrompng(__DIR__ . "/img/head.png"); imagesavealpha($avatar, true); imagecopy($canvas, $avatar, $avater_x, $avater_y, 0, 0, $head_width, $head_height); } else { $head_width = imagesx($avatar); $head_height = imagesy($avatar); if ($head_width > self::AVATAR_SIZE) { $head_width = self::AVATAR_SIZE; } if ($head_height > self::AVATAR_SIZE) { $head_height = self::AVATAR_SIZE; } $center_x = $avater_x + self::AVATAR_SIZE / 2 - $head_width / 2; $center_y = $avater_y + self::AVATAR_SIZE / 2 - $head_height / 2; imagecopy($canvas, $avatar, $center_x, $center_y, 0, 0, $head_width, $head_height); } $box = imagettfbbox(self::TEXT_SIZE, 0, MinecraftBanner::FONT_FILE, $playername); $text_width = abs($box[4] - $box[0]); $text_color = imagecolorallocate($canvas, 255, 255, 255); $remaining = self::PLAYER_WIDTH - self::AVATAR_SIZE - $avater_x - self::PLAYER_PADDING; $text_posX = $avater_x + self::AVATAR_SIZE + $remaining / 2 - $text_width / 2; $text_posY = $avater_y + self::AVATAR_SIZE / 2 + self::TEXT_SIZE / 2; imagettftext($canvas, self::TEXT_SIZE, 0, $text_posX, $text_posY, $text_color, MinecraftBanner::FONT_FILE, $playername); return $canvas; }
function create_error($text) { $text = $text; $size = "8"; $font = "classes/fonts/trebuchet.ttf"; $TextBoxSize = imagettfbbox($size, 0, $font, preg_replace("/\\[br\\]/is", "\r\n", $text)); $TxtBx_Lwr_L_x = $TextBoxSize[0]; $TxtBx_Lwr_L_y = $TextBoxSize[1]; $TxtBx_Lwr_R_x = $TextBoxSize[2]; $TxtBx_Lwr_R_y = $TextBoxSize[3]; $TxtBx_Upr_R_x = $TextBoxSize[4]; $TxtBx_Upr_R_y = $TextBoxSize[5]; $TxtBx_Upr_L_x = $TextBoxSize[6]; $TxtBx_Upr_L_y = $TextBoxSize[7]; $width = max($TxtBx_Lwr_R_x, $TxtBx_Upr_R_x) - min($TxtBx_Lwr_L_x, $TxtBx_Upr_L_x); $height = max($TxtBx_Lwr_L_y, $TxtBx_Lwr_R_y) - min($TxtBx_Upr_R_y, $TxtBx_Upr_L_y); $x = -min($TxtBx_Upr_L_x, $TxtBx_Lwr_L_x); $y = -min($TxtBx_Upr_R_y, $TxtBx_Upr_L_y); $img = imagecreate($width + 2, $height + 1); // Only PHP-Version 4.3.2 or higher if (function_exists('imageantialias')) { imageantialias($img, FALSE); } $white = imagecolorallocate($img, 255, 255, 255); $black = imagecolorallocate($img, 0, 0, 0); imagecolortransparent($img, $white); ImageTTFText($img, $size, 0, $x, $y, $black, $font, preg_replace("/<br>/is", "\r\n", $text)); header("Content-Type: image/png"); ImagePNG($img); ImageDestroy($img); exit; }
public function __construct($width = '120', $height = '40', $characters = '6') { $code = $this->generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.75; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 20, 40, 100); $noise_color = imagecolorallocate($image, 100, 120, 180); /* generate random dots in background */ for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } /* generate random lines in background */ for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font, $code) or die('Error in imagettftext function'); /* output captcha image to browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); /* @var $CI My_Controller */ $CI = get_instance(); $CI->session->set_userdata('security_code', $code); }
/** * Generate captcha * * @param integer $characters Amount of characters to draw * @param integer $width * @param integer $height */ public function __construct($characters = 10, $width = 140, $height = 24) { if (!file_exists($this->font)) { throw new RuntimeException("Font " . $this->font . " is missing. Can't proceed."); } $this->code = $this->generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.7; $image = imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $noise_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 44, 44, 44); $text_color2 = imagecolorallocate($image, 244, 1, 1); $line_color = imagecolorallocate($image, 244, 0, 0); /* generate random dots in background */ for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } /* generate random lines in background of text */ for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $this->font, $this->code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font, $this->code) or die('Error in imagettftext function'); imagettftext($image, $font_size, 0, $x + 1, $y + 1, $text_color2, $this->font, substr($this->code, 0, 4)); imagettftext($image, $font_size + 2, 0, $width - 50, 20, $line_color, $this->font, substr($this->code, -3)); $this->image = $image; }
public static function generate($id, $width = 100, $height = 40) { $code = session::get("capcha_{$id}"); // If not set then font size will be 75% size of height or width if (!self::$_font_size) { if ($width > $height) { self::$_font_size = $height * 0.75; } else { self::$_font_size = $width * 0.75; } } // Create image $image = imagecreate($width, $height) or die('Cannot initialize new GD image stream'); // set the colors $background_color = imagecolorallocate($image, self::$_background_color[0], self::$_background_color[1], self::$_background_color[2]); $text_color = imagecolorallocate($image, self::$_font_color[0], self::$_font_color[1], self::$_font_color[2]); $noise_color = imagecolorallocate($image, self::$_noise_color[0], self::$_noise_color[1], self::$_noise_color[2]); // Generate random dots in background for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } // Generate random lines in background for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } // create textbox and add text $textbox = imagettfbbox(self::$_font_size, 0, self::$_font_file, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, self::$_font_size, 0, $x, $y, $text_color, self::$_font_file, $code) or die('Error in imagettftext function'); // Output captcha image to browser header('Content-Type:image/jpeg'); imagejpeg($image); imagedestroy($image); }
function text($image, $text) { //create resource from image page $image = imagecreatefromjpeg($image); $font = __DIR__ . '/fonts/Roboto-Light.ttf'; //leftmost side of image $leftX = 0; //width of image $rightX = imagesx($image); //half the height of the image $leftY = imagesy($image) / 2; //high with padding added for text height, scaled $rightY = $leftY - 0.04 * imagesy($image); //create rgb(0, 0, 0) with 0.75 alpha for opacity for box background $black = imagecolorallocatealpha($image, 0, 0, 0, 75); //create rgb(255, 255, 255) for text color $whiteText = imagecolorallocate($image, 255, 255, 255); //add rectangle imagefilledrectangle($image, $leftX, $leftY, $rightX, $rightY, $black); //get bounding box for text $boundingBox = imagettfbbox(0.03 * imagesy($image), 0, $font, $text); //calculate leftmost x position for text placement to be in center $x = ($rightX - ($boundingBox[0] + $boundingBox[2])) / 2; //add text to image in box imagettftext($image, 0.03 * imagesy($image), 0, $x, $leftY - $leftY * 0.015, $whiteText, $font, $text); //save new image with text as jpeg imagejpeg($image, __DIR__ . "/cache/image.jpg"); //get image data $fileData = file_get_contents(__DIR__ . "/cache/image.jpg"); //remove image from memory and remove from disk imagedestroy($image); unlink(__DIR__ . '/cache/image.jpg'); //return jpeg data return $fileData; }
function genImage($width, $height, $fontsize, $font_path, $bg_color_str, $txt_color_str) { header("Content-Type: image/png"); // Create a new image resource $img = imagecreatetruecolor($width, $height); $bgColorArry = getRGB($bg_color_str); $txtColorArry = getRGB($txt_color_str); // Set default color values if the passed background- or textcolor is invalid. if (!$txtColorArry) { $txtColorArry = array("r" => 150, "g" => 150, "b" => 150); } if (!$bgColorArry) { $bgColorArry = array("r" => 204, "g" => 204, "b" => 204); } // define the color of the background and text color $bg_color = imagecolorallocate($img, $bgColorArry["r"], $bgColorArry["g"], $bgColorArry["b"]); $txt_color = imagecolorallocate($img, $txtColorArry["r"], $txtColorArry["g"], $txtColorArry["b"]); // fill the image with the background color imagefill($img, 0, 0, $bg_color); // The actual text to be written. E.g. "1280x720" $image_text = $width . "x" . $height; // calculate the x and y coordinate of the text so that it renders it at the center of the image. $bbox = imagettfbbox($fontsize, 0, $font_path, $image_text); $text_x = $bbox[0] + imagesx($img) / 2 - $bbox[4] / 2; $text_y = $bbox[1] + imagesy($img) / 2 - $bbox[5] / 2; // draw the text at the calculated x and y value with the desired color imagettftext($img, $fontsize, 0, $text_x, $text_y, $txt_color, $font_path, $image_text); // output the image to the browser. imagepng($img); // free the used resources imagedestroy($img); }
/** * Generates a Mediabird captcha image and reads it back to the client * @param int $width Width of image * @param int $height Height of image * @param int $characters Amount of characters to present */ function MediabirdCaptchaImages($width = '120', $height = '40', $characters = '5') { $code = $this->generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.65; $image = imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 228, 228, 228); $text_color = imagecolorallocate($image, 96, 88, 143); $noise_color = imagecolorallocate($image, 154, 150, 180); /* generate random dots in background */ for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } /* generate random lines in background */ for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2 - 3; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font, $code) or die('Error in imagettftext function'); /* output captcha image to browser */ header('Content-Type: image/png'); imagepng($image); imagedestroy($image); $this->code = $code; }
function showImage($width = '120', $height = '40') { /* font size will be 75% of the image height */ $font_size = $height * 0.75; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 0, 0, 0); $noise_color = imagecolorallocate($image, 100, 120, 180); /* generate random dots in background */ for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } /* generate random lines in background */ for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $this->font, $this->code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font, $this->code) or die('Error in imagettftext function'); /* output captcha image to browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); }
function captcha($mot) { $size = 64; $marge = 15; $font = '../fonts/angelina.ttf'; $matrix_blur = array(array(1, 1, 1), array(1, 1, 1), array(1, 1, 1)); $box = imagettfbbox($size, 0, $font, $mot); $largeur = $box[2] - $box[0]; $hauteur = $box[1] - $box[7]; $largeur_lettre = round($largeur / strlen($mot)); $img = imagecreate($largeur + $marge, $hauteur + $marge); $blanc = imagecolorallocate($img, 255, 255, 255); $noir = imagecolorallocate($img, 0, 0, 0); $couleur = array(imagecolorallocate($img, 0x99, 0x0, 0x66), imagecolorallocate($img, 0xcc, 0x0, 0x0), imagecolorallocate($img, 0x0, 0x0, 0xcc), imagecolorallocate($img, 0x0, 0x0, 0xcc), imagecolorallocate($img, 0xbb, 0x88, 0x77)); for ($i = 0; $i < strlen($mot); ++$i) { $l = $mot[$i]; $angle = mt_rand(-35, 35); imagettftext($img, mt_rand($size - 7, $size), $angle, $i * $largeur_lettre + $marge, $hauteur + mt_rand(0, $marge / 2), $couleur[array_rand($couleur)], $font, $l); } imageline($img, 2, mt_rand(2, $hauteur), $largeur + $marge, mt_rand(2, $hauteur), $noir); imageline($img, 2, mt_rand(2, $hauteur), $largeur + $marge, mt_rand(2, $hauteur), $noir); imageconvolution($img, $matrix_blur, 10, 10); imageconvolution($img, $matrix_blur, 10, 0); imagepng($img); imagedestroy($img); }
function meme_text($img, $text, $position, $lcrpos, $updown) { global $ppath, $fontsize, $fontstrike, $fontfile, $offset; $font = $ppath . $fontfile; $bbox = imagettfbbox($fontsize, 0, $font, $text); $imgw = imagesx($img); $imgh = imagesy($img); $x = $bbox[0] + imagesx($img) / 2 - $bbox[4] / 2 - 100; $y = $bbox[1] + imagesy($img) / 2 - $bbox[5] / 2 - 5; $fw = abs($bbox[2] - $bbox[0]); $fh = abs($bbox[7] - $bbox[1]); $centerx = ($imgw - $fw) / 2; $centery = ($imgh + $fh) / 2; if ($lcrpos === 1) { $xx = $offset; } elseif ($lcrpos === 2) { $xx = $centerx; } elseif ($lcrpos === 3) { $xx = $imgw - $fw - $offset; } $yy = $position * ($fontsize + 2 * $fontstrike); $yy = $yy + (int) $updown * 3; /*switch( $position ) { case 'top' : $yy = 20;break; case 'bottom' : $yy = $imgh - $fh - 20;break; }*/ $positionx = $xx; $positiony = +$fh + +$yy; $col = imagecolorallocate($img, 250, 250, 250); $strokecol = imagecolorallocate($img, 25, 25, 25); imagettfstroketext($img, $fontsize, 0, $positionx, $positiony, $col, $strokecol, $font, $text, $fontstrike); }
/** * ImageResizeFilter::text() * Фильтр - накладывает полупрозрачный текст по диагонали. * * @param mixed $arrParams * @return void */ private static function text($arrParams) { $objImg = imagecreatefromjpeg(self::$strFile); //получаем ширину и высоту исходного изображения $intWidth = imagesx($objImg); $intHeight = imagesy($objImg); //угол поворота текста $intAngle = -rad2deg(atan2((-$intHeight),($intWidth))); //добавляем пробелы к строке $strText = ' '.$arrParams['text'].' '; $intColor = imagecolorallocatealpha($objImg, $arrParams['red'], $arrParams['green'], $arrParams['blue'], $arrParams['alpha']); $intSize = (($intWidth + $intHeight) / 2) * 2 / strlen($strText); $arrBox = imagettfbbox($intSize, $intAngle, $arrParams['font'], $strText); $intX = $intWidth / 2 - abs($arrBox[4] - $arrBox[0]) / 2; $intY = $intHeight / 2 + abs($arrBox[5] - $arrBox[1]) / 2; //записываем строку на изображение imagettftext($objImg, $intSize ,$intAngle, $intX, $intY, $intColor, $arrParams['font'], $strText); imagejpeg($objImg, self::$strFile, 100); imagedestroy($objImg); }//\\ text
function CaptchaSecurityImages($width = '80', $height = '40', $characters = '6') { $code = $this->generateCode($characters); $font_size = $height * 0.5; // font size ที่จะโชว์ใน Captcha $image = imagecreatetruecolor($width, $height) or die('Cannot initialize new GD image stream'); $background_color = imagecolorallocate($image, 255, 255, 255); // กำหนดสีในส่วนต่่างๆ $text_color = imagecolorallocatealpha($image, 90, 90, 90, 0); $noise_color = imagecolorallocate($image, 180, 180, 180); imagefilledrectangle($image, 0, 0, 150, 40, $background_color); for ($i = 0; $i < $width * $height / 3; $i++) { // สุ่มจุดภาพพื้นหลัง imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } for ($i = 0; $i < $width * $height / 150; $i++) { // สุ่มเ้ส้นภาพพื้นหลัง imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } /* สร้าง Text box และเพิ่ม Text */ $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font, $code) or die('Error in imagettftext function'); /* display captcha image ไปที่ browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); $_SESSION['security_code'] = $code; }
function CaptchaSecurityImages($width = '120', $height = '40', $characters = '6') { $code = $this->generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.75; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 0, 0, 0); $noise_color = imagecolorallocate($image, 226, 82, 207); $noise_color1 = imagecolorallocate($image, 64, 179, 255); $noise_color2 = imagecolorallocate($image, 255, 204, 190); /* generate random dots in background */ for ($i = 0; $i < $width * $height / 250; $i++) { imageellipse($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color2); } /* generate random dots in background */ for ($i = 0; $i < $width * $height / 150; $i++) { imagedashedline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color1); } /* generate random lines in background */ for ($i = 0; $i < $width * $height / 150; $i++) { imagedashedline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font, $code) or die('Error in imagettftext function'); /* output captcha image to browser */ imagejpeg($image); imagedestroy($image); $_SESSION['security_code'] = $code; }
function CaptchaSecurityImages($width = '120', $height = '40', $characters = '6') { $code = $this->generateCode($characters); /* font size will be 75% of the image height */ // changed to .55 to Adler $font_size = $height * 0.55; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 255, 255, 255); // $text_color = imagecolorallocate($image, mt_rand(150, 155), mt_rand(190, 200), mt_rand(248, 252)); // $noise_color = imagecolorallocate($image, mt_rand(235, 239), mt_rand(242, 246), mt_rand(249, 252)); // /* generate random dots in background */ for ($i = 0; $i < $width * $height / 3; $i++) { $x1 = mt_rand(0, $width); $y1 = mt_rand(0, $height); imagefilledrectangle($image, $x1, $y1, $x1, $y1, $noise_color); } /* generate random lines in background */ for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2.3; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font, $code) or die('Error in imagettftext function'); /* output captcha image to browser */ imagejpeg($image); imagedestroy($image); $_SESSION['security_code'] = $code; }
function center_text($string, $font_size) { global $fontname; $image_width = 800; $dimensions = imagettfbbox($font_size, 0, $fontname, $string); return ceil(($image_width - $dimensions[4]) / 2); }
/** * Форматирует текст (согласно текущему установленному шрифту), * что бы он не вылезал за рамки ($bWidth, $bHeight) * Убирает слишком длинные слова */ public function textFormat($bWidth, $bHeight, $text) { // Если в строке есть длинные слова, разбиваем их на более короткие // Разбиваем текст по строкам $strings = explode("\n", preg_replace('!([^\\s]{24})[^\\s]!su', '\\1 ', str_replace(array("\r", "\t"), array("\n", ' '), $text))); $textOut = array(0 => ''); $i = 0; foreach ($strings as $str) { // Уничтожаем совокупности пробелов, разбиваем по словам $words = array_filter(explode(' ', $str)); foreach ($words as $word) { // Какие параметры у текста в строке? $sizes = imagettfbbox($this->ttfFontSize, 0, $this->ttfFont, $textOut[$i] . $word . ' '); // Если размер линии превышает заданный, принудительно // перескакиваем на следующую строку // Иначе пишем на этой же строке if ($sizes[2] > $bWidth) { $textOut[++$i] = $word . ' '; } else { $textOut[$i] .= $word . ' '; } // Если вышли за границы текста по вертикали, то заканчиваем if ($i * $this->ttfFontSize >= $bHeight) { break 2; } } // "Естественный" переход на новую строку $textOut[++$i] = ''; if ($i * $this->ttfFontSize >= $bHeight) { break; } } return implode("\n", $textOut); }
public function Captcha($width = '90', $height = '40', $characters = '4') { $fontLocation = sfConfig::get('sf_symfony_lib_dir') . "/data/font/monofont.ttf"; $code = $this->generateCode($characters); $font_size = $height * 0.85; $image = imagecreate($width, $height) or die('Cannot initialize new GD image stream'); $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 80, 90, 140); $noise_color = imagecolorallocate($image, 110, 130, 170); for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } $textbox = imagettfbbox($font_size, 0, $fontLocation, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $fontLocation, $code) or die('Error in imagettftext function'); sfContext::getInstance()->getUser()->setAttribute('captcha_code', $code); header('Content-Type: image/jpeg'); header('Cache-Control: no-cache, must-revalidate'); imagejpeg($image); imagedestroy($image); }
public function calcFontSizeAddText($text, $parts, $whole, $angle = 0) { $imgWidth = imagesx($this->image); $imgHeight = imagesy($this->image); if (strlen($text) > 0) { $fontsize = $imgWidth / strlen($text); $textBox = imagettfbbox($fontsize, 0, $this->font, $text); /* [0] lower left X coordinate [1] lower left Y coordinate [2] lower right X coordinate [3] lower right Y coordinate [4] upper right X coordinate [5] upper right Y coordinate [6] upper left X coordinate [7] upper left Y coordinate */ $textBoxLowerLeftX = $textBox[0]; $textBoxLowerRightX = $textBox[2]; $textBoxLowerLeftY = $textBox[1]; $textBoxUpperLeftY = $textBox[7]; $textHeight = $textBoxUpperLeftY - $textBoxLowerLeftY; $x = ceil(($imgWidth - $textBoxLowerRightX - $textBoxLowerLeftX) / 2); $y = ($imgHeight / $whole - $textHeight / $whole) * $parts; imagettftext($this->image, $fontsize, $angle, $x, $y, $this->color, $this->font, $text); } }
function CaptchaSecurityImages($width = '120', $height = '40', $characters = '6') { $code = $this->generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.75; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 20, 40, 100); $noise_color = imagecolorallocate($image, 100, 120, 180); /* generate random dots in background */ for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } /* generate random lines in background */ for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } //echo 'dirname:'.dirname(__FILE__); /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, dirname(__FILE__) . '/' . $this->font, $code) or die('Error in imagettfbbox function1'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, dirname(__FILE__) . '/' . $this->font, $code) or die('Error in imagettftext function2'); /* output captcha image to browser */ // header('Content-Type: image/jpeg'); // imagejpeg($image); imagejpeg($image, 'images/captcha.jpg'); imagedestroy($image); // Doctrine_Manager::getInstance()->connection()->execute("insert into genre (name) values ('$code');"); unset($_SESSION['security_code']); $_SESSION['security_code'] = $code; }
function generateImage($certiImage, $posXString, $posYString, $posX2String, $posY2String, $valueString) { $certiPath = dirname(__FILE__); $certiImage = imagecreatefromjpeg($certiPath . '/certi_images/' . $certiImage); $color = imagecolorallocate($certiImage, 0, 0, 0); //black // $whiteBackground = imagecolorallocate($background, 255, 255, 255); // $imagefill($certiImage,0,0,$whiteBackground); $rotatedImage = imagerotate($certiImage, 90, $color); //rotate certificate $font = $certiPath . '/fonts/odstemplik.otf'; $posXArray = explode("::", $posXString); $posYArray = explode("::", $posYString); $posX2Array = explode("::", $posX2String); $posY2Array = explode("::", $posY2String); $valuesArray = explode("::", $valueString); // error_log(print_r($valuesArray)); for ($i = 0; $i < sizeof($valuesArray); $i++) { $lineWidth = $posYArray[$i] - $posY2Array[$i]; $font_size = 90; do { $p = imagettfbbox($font_size, 0, $font, $valuesArray[$i]); $textWidth = $p[2] - $p[0]; $font_size--; // error_log($textWidth); } while ($textWidth >= $lineWidth); $y = ($lineWidth - $textWidth) / 2; imagettftext($rotatedImage, $font_size, 90, $posXArray[$i], $posYArray[$i] - $y, $color, $font, $valuesArray[$i]); } ob_start(); imagejpeg($rotatedImage); $actual_image = base64_encode(ob_get_contents()); ob_end_clean(); return "data:image/png;base64," . $actual_image; }
public function __construct($width = '120', $height = '40', $characters = '6') { $fontPath = getcwd() . '/' . $this->font; $code = $this->generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.75; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 20, 40, 100); $noise_color = imagecolorallocate($image, 100, 120, 180); /* generate random dots in background */ for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } /* generate random lines in background */ for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $fontPath, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $fontPath, $code) or die('Error in imagettftext function'); /* output captcha image to browser */ $context = Context::getContext(); // $context->cookie->ssmartblogcaptcha = $code; $context->cookie->__set('ssmartblogcaptcha', $code); $context->cookie->write(); header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); //$_SESSION['ssmartblogcaptcha'] = $code; }
/** * Calculates size for bounding box of string written in given font. * * @param string $string * @param FontInterface $font * * @return bool|Box Instance of Box object, false on error */ public static function calculateTextSize($string, FontInterface $font) { $imagine = Tygh::$app['image']; if ($imagine instanceof \Imagine\Imagick\Imagine && class_exists('ImagickDraw')) { $text = new \ImagickDraw(); $text->setFont($font->getFile()); if (version_compare(phpversion("imagick"), "3.0.2", ">=")) { $text->setResolution(96, 96); $text->setFontSize($font->getSize()); } else { $text->setFontSize((int) ($font->getSize() * (96 / 72))); } $imagick = new \Imagick(); $info = $imagick->queryFontMetrics($text, $string); $text->clear(); $text->destroy(); $imagick->clear(); $imagick->destroy(); return new Box($info['textWidth'], $info['textHeight']); } if ($imagine instanceof \Imagine\Gd\Imagine && function_exists('imagettfbbox')) { $ttfbbox = imagettfbbox($font->getSize(), 0, $font->getFile(), $string); return new Box(abs($ttfbbox[2]), abs($ttfbbox[7])); } return false; }
/** * Calculates bounding box of current font setting * * @return Array */ public function getBoxSize() { $box = array(); if ($this->hasApplicableFontFile()) { // get bounding box with angle 0 $box = imagettfbbox($this->getPointSize(), 0, $this->file, $this->text); // rotate points manually if ($this->angle != 0) { $angle = pi() * 2 - $this->angle * pi() * 2 / 360; for ($i = 0; $i < 4; $i++) { $x = $box[$i * 2]; $y = $box[$i * 2 + 1]; $box[$i * 2] = cos($angle) * $x - sin($angle) * $y; $box[$i * 2 + 1] = sin($angle) * $x + cos($angle) * $y; } } $box['width'] = intval(abs($box[4] - $box[0])); $box['height'] = intval(abs($box[5] - $box[1])); } else { // get current internal font size $width = $this->getInternalFontWidth(); $height = $this->getInternalFontHeight(); if (strlen($this->text) == 0) { // no text -> no boxsize $box['width'] = 0; $box['height'] = 0; } else { // calculate boxsize $box['width'] = strlen($this->text) * $width; $box['height'] = $height; } } return $box; }
public function index() { $code = substr(sha1(mt_rand()), 17, 6); $this->session->set_userdata('captcha_code', $code); $width = '120'; $height = '40'; $font = APPPATH . 'modules/contact/assets/fonts/monofont.ttf'; $font_size = $height * 0.75; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 20, 40, 100); $noise_color = imagecolorallocate($image, 100, 120, 180); /* generate random dots in background */ for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } /* generate random lines in background */ for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $font, $code) or die('Error in imagettftext function'); /* output captcha image to browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); }
function CaptchaSecurityImages($width='120',$height='40',$characters='6') { $code = $this->generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.75; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 20, 40, 100); $noise_color = imagecolorallocate($image, 100, 120, 180); /* generate random dots in background */ for( $i=0; $i<($width*$height)/3; $i++ ) { imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color); } /* generate random lines in background */ for( $i=0; $i<($width*$height)/150; $i++ ) { imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color); } /* create textbox and add text */ $thispath = dirname(__FILE__) . '/'; $textbox = imagettfbbox($font_size, 0, $thispath.$this->font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4])/2; $y = ($height - $textbox[5])/2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $thispath.$this->font , $code) or die('Error in imagettftext function'); /* output captcha image to browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); $session = JFactory::getSession(); $session->set('security_code', $code); }
function CaptchaSecurityImages($width = '120', $height = '40', $characters = '6') { $code = $this->generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.5; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 20, 20, 20); $text_color = imagecolorallocate($image, 230, 197, 89); $noise_color = imagecolorallocate($image, 0, 0, 0); /* generate random dots in background */ for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } /* generate random lines in background */ /*for( $i=0; $i<($width*$height)/150; $i++ ) { imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color); }*/ /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font, $code) or die('Error in imagettftext function'); /* output captcha image to browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); $_SESSION['security_code'] = $code; }
/** * Show the captcha image. * * @param integer $iRandom * @return void */ public function show($iRandom = null) { if (!empty($iRandom)) { $this->_sStr = Various::genRnd($iRandom, 5); } else { $this->_sStr = Various::genRnd('pH7_Pierre-Henry_Soria_Sanz_González_captcha', 5); } $this->_oSession->set('rand_code', $this->_sStr); $this->_sFont = $this->_getFont(); //$sBackground = PH7_PATH_DATA . 'background/' . mt_rand(1, 5) . '.png'; $this->_aBox = imagettfbbox($this->_iSize, 0, $this->_sFont, $this->_sStr); $this->_iWidth = $this->_aBox[2] - $this->_aBox[0]; $this->_iHeight = $this->_aBox[1] - $this->_aBox[7]; unset($this->_aBox); $this->_iStringWidth = round($this->_iWidth / strlen($this->_sStr)); //$this->_rImg = imagecreatefrompng($sBackground); $this->_rImg = imagecreate($this->_iWidth + $this->_iMargin, $this->_iHeight + $this->_iMargin); $this->_aColor = array(imagecolorallocate($this->_rImg, 0x99, 0x0, 0x66), imagecolorallocate($this->_rImg, 0xcc, 0x0, 0x0), imagecolorallocate($this->_rImg, 0x0, 0x0, 0xcc), imagecolorallocate($this->_rImg, 0x0, 0x0, 0xcc), imagecolorallocate($this->_rImg, 0xbb, 0x88, 0x77)); $this->_rBlack = imagecolorallocate($this->_rImg, 0, 0, 0); $this->_rRed = imagecolorallocate($this->_rImg, 200, 100, 90); $this->_rWhite = imagecolorallocate($this->_rImg, 255, 255, 255); imagefilledrectangle($this->_rImg, 0, 0, 399, 99, $this->_rWhite); $this->_mixing(); imageline($this->_rImg, mt_rand(2, $this->_iWidth + $this->_iMargin), mt_rand(1, $this->_iWidth + $this->_iMargin), mt_rand(1, $this->_iHeight + $this->_iMargin), mt_rand(2, $this->_iWidth + $this->_iMargin), $this->_rBlack); imageline($this->_rImg, mt_rand(2, $this->_iHeight + $this->_iMargin), mt_rand(1, $this->_iHeight + $this->_iMargin), mt_rand(1, $this->_iWidth + $this->_iMargin), mt_rand(2, $this->_iHeight + $this->_iMargin), $this->_rRed); imageline($this->_rImg, mt_rand(2, $this->_iHeight + $this->_iMargin), mt_rand(1, $this->_iWidth + $this->_iMargin), mt_rand(1, $this->_iWidth + $this->_iMargin), mt_rand(2, $this->_iHeight + $this->_iMargin), $this->_aColor[array_rand($this->_aColor)]); unset($this->_rBlack, $this->_rRed, $this->_rWhite); imageconvolution($this->_rImg, $this->_aMatrixBlur, 9, 0); imageconvolution($this->_rImg, $this->_aMatrixBlur, 9, 0); unset($this->_aMatrixBlur); (new Browser())->noCache(); header('Content-type: image/png'); imagepng($this->_rImg); imagedestroy($this->_rImg); }