/** * Creates an image for every code character. */ protected function createCharImages() { $counter = 0; for ($i = 0, $j = strlen($this->codeWord); $i < $j; $i++) { $char = $this->codeWord[$i]; $tempImageWidth = $this->fontSize * 2; $tempImageHeight = $this->fontSize + 40; // create image $tempImage = imageCreate($tempImageWidth, $tempImageHeight); $tempColor = imageColorAllocate($tempImage, $this->color2['red'], $this->color2['green'], $this->color2['blue']); imageColorTransparent($tempImage, $tempColor); // set font color $fontColor = imageColorAllocate($tempImage, $this->fontColor['red'], $this->fontColor['green'], $this->fontColor['blue']); // write text imageTtfText($tempImage, $this->fontSize, 0, 10, $this->fontSize + mt_rand(25, 30), $fontColor, $this->fontFace, $char); // morph text if (CAPTCHA_FONT_MORPH) { $tempImageHeight = 120; $tempImage2 = imageCreate($tempImageWidth, 120); $tempColor = imageColorAllocate($tempImage2, $this->color2['red'], $this->color2['green'], $this->color2['blue']); imageColorTransparent($tempImage2, $tempColor); $divisor = mt_rand(6, 7); $quotient = mt_rand(4, 6); $method = mt_rand(0, 1); // morph text on x-axis if ($method == 0) { for ($y = 1; $y <= $tempImageHeight; $y++) { $posX = sin($y / $divisor) * $quotient; imageCopyMerge($tempImage2, $tempImage, $posX, $y, 0, $y, $tempImageWidth, 1, 100); } } // morph text on y-axis if ($method == 1) { for ($x = 1; $x <= $tempImageWidth; $x++) { $posY = sin($x / $divisor) * $quotient; imageCopyMerge($tempImage2, $tempImage, $x, $posY, $x, 0, 1, $tempImageHeight, 100); } } $image = $tempImage2; } else { $image = $tempImage; } // get text width and height $positionX = 0; for ($x = $tempImageWidth - 1; $x > 0; $x--) { for ($y = $tempImageHeight - 1; $y > 0; $y--) { $color = imageColorAt($image, $x, $y); $colorArray = imageColorsForIndex($image, $color); if ($colorArray['red'] == $this->fontColor['red'] && $colorArray['green'] == $this->fontColor['green'] && $colorArray['blue'] == $this->fontColor['blue']) { $positionX = $x; $x = 0; $y = 0; break; } } } $width = $positionX + 10; $height = 100; // create final char image $this->chars[$counter] = imageCreate($width, $height); $color2 = imageColorAllocate($this->chars[$counter], $this->color2['red'], $this->color2['green'], $this->color2['blue']); imageColorTransparent($this->chars[$counter], $color2); imageCopyMerge($this->chars[$counter], $image, 5, 5, 0, 0, $width, $tempImageHeight, 100); $this->charWidth[$counter] = $width; // destroy temp images imageDestroy($tempImage); if (CAPTCHA_FONT_MORPH) { imageDestroy($tempImage2); } $counter++; } }
/** * @param int $maxX * @param int $maxY * @param bool $ignoreAspectRatio 画像の縦横比を維持するか。 * @return Image */ public function resize($maxX = 180, $maxY = 180, $ignoreAspectRatio = false) { $width = $this->getWidth(); $height = $this->getHeight(); if ($ignoreAspectRatio === false) { $resizeX = $maxX; $resizeY = $maxY; } elseif ($width < $height) { $resizeX = ceil($maxX * $width / $height); $resizeY = $maxY; } else { $resizeX = $maxX; $resizeY = ceil($maxY * $height / $width); } // 透明色が指定されているとjpg保存時に透明色部が黒くなるのでその対策。 // MEMO: PNGで保存するなら // imagealphablending($resize, false); // imagesavealpha($resize, true); $resizeGD = @imageCreateTrueColor($resizeX, $resizeY); $alpha = imagecolortransparent($this->gd); if ($alpha !== -1) { $color = imagecolorallocate($resizeGD, 255, 255, 255); imageFill($resizeGD, 0, 0, $color); imageColorTransparent($resizeGD, $alpha); } else { imageAlphaBlending($resizeGD, true); imageSaveAlpha($resizeGD, true); $fill_color = imagecolorallocate($resizeGD, 255, 255, 255); imageFill($resizeGD, 0, 0, $fill_color); } imageCopyResampled($resizeGD, $this->gd, 0, 0, 0, 0, $resizeX, $resizeY, $width, $height); return new Image($resizeGD); }
function text2image($height, $text) { header("Content-type: image/png"); $width = $height * strlen($text)/ 5 * 2; $font = "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf"; $image = imageCreate($width, $height); $backgroundColor = imageColorAllocate($image, 255, 255, 255); $textColor = imageColorAllocate($image, 0, 0, 0); imagefttext($image, $height/2, 0, 0, $height/10*9, $textColor, $font, $text); imageInterlace($image, 1); imageColorTransparent($image, $backgroundColor); imagePNG($image); }
public function getContent() { $this->getConfig(); $questions = Config::read('secure.captcha_text_questions'); $qkey = array_rand($questions); $text = $questions[$qkey]; $w = 140; $h = 72; $font_size = 14; header("Content-type: image/png"); $im = imagecreatetruecolor($w, $h); $transparent = imagecolorallocate($im, 0, 0, 0); $text_color = imagecolorallocate($im, 30, 30, 30); //imageSaveAlpha($im, true); imageColorTransparent($im, $transparent); imagestring($im, $font_size, ($w - mb_strlen($text) * ($font_size / 2)) / 2, 30, $text, $text_color); imagepng($im); imagedestroy($im); return $qkey; }
/** * Make a particular image transparent * @param array $image The image to make transparent * @param array[optional] $color The color to make transparent, white by default */ protected function setTransparancy(array $image, array $color = array(255, 255, 255)) { $transparentColor = $this->color($color, $image); imageColorTransparent($image['image'], $transparentColor); imagecolordeallocate($image['image'], $transparentColor); }
/** * Copy an existing internal image resource, or part of it, to this Image instance * @param resource existing internal image resource as source for the copy * @param int x x-coordinate where the copy starts * @param int y y-coordinate where the copy starts * @param int resourceX starting x coordinate of the source image resource * @param int resourceY starting y coordinate of the source image resource * @param int width resulting width of the copy (not of the resulting image) * @param int height resulting height of the copy (not of the resulting image) * @param int resourceWidth width of the source image resource to copy * @param int resourceHeight height of the source image resource to copy * @return null */ protected function copyResource($resource, $x, $y, $resourceX, $resourceY, $width, $height, $resourceWidth, $resourceHeight) { if (!imageCopyResampled($this->resource, $resource, $x, $y, $resourceX, $resourceY, $width, $height, $resourceWidth, $resourceHeight)) { if (!imageCopyResized($this->resource, $resource, $x, $y, $resourceX, $resourceY, $width, $height, $resourceWidth, $resourceHeight)) { throw new ImageException('Could not copy the image resource'); } } $transparent = imageColorAllocate($this->resource, 0, 0, 0); imageColorTransparent($this->resource, $transparent); $this->width = $width; $this->height = $height; }
break; } } if ($stringx == $stringy && $stringx == -1) { imageString($map, 5, 200, 245, "NO SUCH USER", imagecolorallocate($map, 255, 0, 0)); } else { $width = imageFontWidth(5); $height = imageFontHeight(5); if ($x + (strlen($user) + 1) * $width > 500) { $stringx = $x - (strlen($user) + 1) * $width - 12; } if ($y + $height > 500) { $stringy = $y - $height / 2 - 2; } $magenta = imageColorAllocate($map, 255, 0, 255); imageColorTransparent($map, $magenta); $brown = imagecolorallocate($map, 102, 51, 0); $parchment = imagecolorallocate($map, 255, 255, 204); // Avoid drawing a brown dot on a brown area $rgb = imageColorAt($map, $x, $y); if ($rgb > 0) { // $rgb is 0 on our parchment-colored areas $temp = $brown; $brown = $parchment; $parchment = $temp; } // YOU ARE HERE imageFilledEllipse($map, $x, $y, 6, 6, $brown); // background for text imageFilledRectangle($map, $stringx + 6, $stringy - $height / 2, $stringx + 6 + $width * (strlen($user) + 1), $stringy + $height / 2, $brown); // text itself
/** * Функция генерирует и выводит хидер PNG и изображение с кодом подтверждения (код берется из массива полей) * Возвращает результат выполнения imagePNG * @return bool */ function confirmImage() { $width = ajaxform::$captcha[width]; $height = ajaxform::$captcha[height]; $multi = 4; $xwidth = $width * $multi; $xheight = $height * $multi; $code = $this->fields[confirm][value]; $im = imageCreateTrueColor($xwidth, $xheight); $w = imageColorAllocate($im, 255, 255, 255); $b = imageColorAllocate($im, 000, 000, 000); $g = imageColorAllocate($im, 100, 100, 100); imageFill($im, 1, 1, $w); $w = imageColorTransparent($im, $w); $r = mt_rand(0, $xheight); for ($x = 0; $x < $xwidth + $xheight; $x += 4 * $multi) { for ($i = 0; $i < $multi; $i++) imageLine($im, $x + $i, 0, $x + $i - $xheight, $xheight + $r, $g); } $arr = preg_split('//', $code, -1, PREG_SPLIT_NO_EMPTY); for ($i = 0; $i < count($arr); $i++) { $x = ($xwidth * 0.04) + (floor(($xwidth * 0.97) * 0.167)) * $i; // разрядка $y = ($xheight * 0.8); $s = ($xheight * 0.5) * (96 / 72); // 96 — res $a = mt_rand(-20, 20); imageTTFText($im, $s, $a, $x, $y, $b, $_SERVER[DOCUMENT_ROOT] . "/lib/core/classes/form_ajax/consolas.ttf", $arr[$i]); // } $temp = imageCreateTrueColor($xwidth, $xheight); $w = imageColorAllocate($temp, 255, 255, 255); imageFill($temp, 1, 1, $w); $w = imageColorTransparent($temp, $w); $phase = rand(-M_PI, M_PI); $frq = 2 * M_PI / $xheight; $amp = $xwidth * 0.02; for ($y = 0; $y < $xheight; $y++) { $dstx = $amp + sin($y * $frq + $phase) * $amp; imagecopy($temp, $im, $dstx, $y, 0, $y, $xwidth, 1); //imagesetpixel($im, $dstx, $y, $b); } $res = imageCreateTrueColor($width, $height); $w = imageColorAllocate($res, 255, 255, 255); imageFill($res, 1, 1, $w); $w = imageColorTransparent($res, $w); imageCopyResampled($res, $temp, 0, 0, 0, 0, $width, $height, $xwidth, $xheight); imageTrueColorToPalette($res, true, 256); header("CONTENT-TYPE: IMAGE/PNG"); return imagePNG($res); }
function txt2img() { $hImg_temp = imageCreate( 10, 10 ); $txt_color = $this->txtcolor( $hImg_temp ); $p4 = imageTTFText( $hImg_temp, $this->font_size, 0, 0, 0, $txt_color, $this->ttf_font, $this->text ); imageDestroy( $hImg_temp ); $margin = 4 ; $x_size = ($p4[2]-$p4[0]) + $margin ; $y_size = $p4[1]-$p4[7] + $margin ; $this->hTxt = imageCreate( $x_size, $y_size ); //$hTxt = imageCreate( 600, 300 ); $bg_color = imageColorAllocate( $this->hTxt, 200, 200, 200 ); imageFill( $this->hTxt, 0, 0, $bg_color ); imageColorTransparent( $this->hTxt, $bg_color ); $x = 0+floor($margin/2) ; $y = $y_size-floor($margin/2); $txt_color = $this->txtcolor( $this->hTxt ); imageTTFText( $this->hTxt, $this->font_size, 0, $x, $y, $txt_color, $this->ttf_font, $this->text ); //$txt_shadow = imageColorAllocate( $hTxt, 100, 100, 100 ); //imageTTFText( $hTxt, $this->font_size, 0, $x-1, $y-1, $txt_shadow, $this->ttf_font, $this->text ); //return $hTxt ; }
<?php header("Content-type: image/gif"); dl('php3_gd.dll'); //header("Content-type: image/png"); $image = imageCreate(100, 100); imageGIF($image); //imagePNG($image); // create an interlaced image for better loading in the browser imageInterlace($image, 1); // mark background color as being transparent $colorBackgr = imageColorAllocate($image, 192, 192, 192); imageColorTransparent($image, $colorBackgr);
/** * @see \wcf\system\image\adapter\IImageAdapter::setTransparentColor() */ public function setTransparentColor($red, $green, $blue) { if ($this->type == IMAGETYPE_PNG) { $color = imagecolorallocate($this->image, $red, $green, $blue); imageColorTransparent($this->image, $color); } }
/* * -leeres Bild erzeugen (270x400, so groß wie das ganze Vorschaubild) * -Hintergrundfarbe (schwarz) erstellen * -diese Hintergrundfarbe transparent machen * -den Text auf dem Bild abbilden * -(speichern erst am Ende des Skriptes) */ $vorschaubild = imageCreateTrueColor(270, 400); $hintergrundfarbe = imageColorAllocate($vorschaubild, 0, 0, 0); imageColorTransparent($vorschaubild, $hintergrundfarbe); $vorschaubild = twMachTextzeilenForVorschaubild($vorschaubild); /*----- Vorschau-Bild (270x400-) END -----*/ /*----- Textbild (nur Text ohne transp. Rand) START -----*/ $textbild = imageCreateTrueColor(500, 500); $hintergrundfarbe2 = imageColorAllocate($textbild, 0, 0, 0); imageColorTransparent($textbild, $hintergrundfarbe2); $textbild = twMachTextzeilenForTextbild($textbild); /*----- Textbild (nur Text ohne transp. Rand) END -----*/ /*----- VorschaubildFuerWarenkorb (100 hoch) START -----*/ $vorschaubildFuerWarenkorb = twMachImgWarenkorb($vorschaubild, 270, 400, 4); $vorschaubildFuerWarenkorb = twMachTransparent($vorschaubildFuerWarenkorb); /*----- VorschaubildFuerWarenkorb (100 hoch) END -----*/ /*----- alles speichern START -----*/ // Vorschaubild $filenameVorschaubild = $filename; $wohinV = "" . $twPrefixDir . $_SESSION['dirVorschauVorneText'] . $filenameVorschaubild; imagePng($vorschaubild, $wohinV); // Textbild $filenameTextbild = $filename; $wohinT = "" . $twPrefixDir . $_SESSION['dirText'] . $filenameTextbild; imagePng($textbild, $wohinT);
<?php ## Создание изображений с областями прозрачности. $im = imageCreate(620, 241); $t = imageColorAllocate($im, 0, 0, 0); $c = imageColorAllocate($im, 0, 255, 0); imageFill($im, 0, 0, $c); imageFilledRectangle($im, 180, 20, 420, 220, $t); imageColorTransparent($im, $t); Header("Content-type: image/png"); imagePng($im);
/** * @param $part_type * @param $part_id * @param $part_color * @param string $format * @return string */ public function generate_part($part_type, $part_id, $part_color, $format = "png") { $time_start = microtime(true); $this->is_head_only = $part_type == "hd"; $avatar_image = imageCreateTrueColor($this->rect_width, $this->rect_height); imageAlphaBlending($avatar_image, false); imageSaveAlpha($avatar_image, true); $rect_mask = imageColorAllocateAlpha($avatar_image, 255, 0, 255, 127); imageFill($avatar_image, 0, 0, $rect_mask); $draw_parts = $this->get_draw_order("std", $this->direction); $active_parts['rect'] = $this->get_active_part_set($this->is_head_only ? "head" : "figure", true); $active_parts['eye'] = $this->get_active_part_set("eye"); if ($part_type == 'ri' || $part_type == 'li') { $set_parts[$part_type][0] = ['id' => $part_id, 'colorable' => false]; } else { $set_parts = $this->get_part_color($part_type, $part_id, $part_color); } imageAlphaBlending($avatar_image, true); $draw_count = 0; foreach ($draw_parts as $id => $type) { if (isset($set_parts[$type])) { $draw_part_array = $set_parts[$type]; } else { continue; } foreach ($draw_part_array as $draw_part) { if (!is_array($draw_part)) { continue; } if ($this->get_part_unique_name($type, $draw_part['id']) == '') { continue; } if ($this->is_head_only && !$active_parts['rect'][$type]['active']) { continue; } if ($active_parts['eye'][$type]['active']) { $draw_part['colorable'] = false; } $unique_name = $this->get_part_unique_name($type, $draw_part['id']); $draw_part_rect = $this->get_part_resource($unique_name, $this->action, $type, $draw_part['id'], $this->direction); $draw_count++; if ($draw_part_rect === false) { $this->debug .= "PART[" . $this->action . "][" . $type . "][" . $draw_part['id'] . "][" . $this->direction . "][0]/"; continue; } else { $this->debug .= $draw_part_rect['lib'] . ":" . $draw_part_rect['name'] . "(" . $draw_part_rect['width'] . "x" . $draw_part_rect['height'] . ":" . $draw_part_rect['offset']['x'] . "," . $draw_part_rect['offset']['y'] . ")/"; } $draw_part_transparent_color = imageColorTransparent($draw_part_rect['resource']); if ($draw_part['colorable']) { $this->set_part_color($draw_part_rect['resource'], $draw_part['color']); } $_posX = -$draw_part_rect['offset']['x']; $_posY = $this->rect_height / 2 - $draw_part_rect['offset']['y'] + $this->rect_height / 2.5; if ($draw_part_rect['isFlip']) { $_posX = -($_posX + $draw_part_rect['width'] - ($this->rect_width + 1)); } imageCopy($avatar_image, $draw_part_rect['resource'], $_posX, $_posY, 0, 0, $draw_part_rect['width'], $draw_part_rect['height']); imageDestroy($draw_part_rect['resource']); } } $this->debug .= "DRAWCOUNT: " . $draw_count; ob_start(); if ($format == "gif") { $rect_mask = imageColorAllocateAlpha($avatar_image, 255, 0, 255, 127); imageColorTransparent($avatar_image, $rect_mask); imageGIF($avatar_image); } elseif ($format == "png") { imagePNG($avatar_image); } else { ob_end_clean(); exit; } $resource = ob_get_contents(); ob_end_clean(); imageDestroy($avatar_image); $time_end = microtime(true); $this->process_time += $time_end - $time_start; return $resource; }
} } reset($items); // Calculate height of graph $height = 38; $im = imagecreate($width, $height); // Allocate colors $white = ImageColorAllocate($im, 204, 204, 204); $black = ImageColorAllocate($im, 0, 0, 0); $yellow = ImageColorAllocate($im, 240, 240, 70); $line = ImageColorAllocate($im, 0, 0, 102); $blue = ImageColorAllocate($im, 64, 100, 168); $barViews = ImageColorAllocate($im, 0, 102, 204); $barClicks = ImageColorAllocate($im, 153, 204, 255); // Background imageColorTransparent($im, $white); ImageFilledRectangle($im, 0, 0, $width, $height, $white); // line Imageline($im, $x, $y - 5, $x, $height - 17, $line); //draw data while (list($key, $item) = each($items)) { if ($item) { $pos = strpos($item, "^"); $item_title = substr($item, 0, $pos); $value = substr($item, $pos + 1, strlen($item)); // display percent $percent = $total == 0 ? 0 : intval(round($value / $total * 100)); ImageString($im, 3, $x - 24, $y - 4, $percent . "%", $black); // value right side rectangle $percent = $total == 0 ? 0 : intval(round($value / $total * 100)); $px = $x + $percent * $unit;
<?php $dest = dirname(realpath(__FILE__)) . '/bug22544.png'; @unlink($dest); $image = imageCreateTruecolor(640, 100); $transparent = imageColorAllocate($image, 0, 0, 0); $red = imageColorAllocate($image, 255, 50, 50); imageColorTransparent($image, $transparent); imageFilledRectangle($image, 0, 0, 640 - 1, 100 - 1, $transparent); imagePng($image, $dest); echo md5_file($dest) . "\n"; @unlink($dest);
// orange if (empty($mobid)) { $sql = "SELECT * FROM characters WHERE charId = '{$playerid}'"; $result = mysql_query($sql, $conn) or die(mysql_error()); while ($newArray = mysql_fetch_array($result)) { $currentx = $newArray['x']; $currenty = $newArray['y']; createloc(); $mapposition = imagecreatefrompng("images/map/x.png"); imageColorTransparent($mapposition, imageColorAt($mapposition, 0, 0)); imagecopymerge($im, $mapposition, $resultx, $resulty, 0, 0, 9, 9, 100); } } if (empty($playerid)) { $sql = "SELECT * FROM spawnlist WHERE npc_templateid = '{$mobid}'"; $result = mysql_query($sql, $conn) or die(mysql_error()); while ($newArray = mysql_fetch_array($result)) { $currentx = $newArray['locx']; $currenty = $newArray['locy']; createloc(); $mapposition = imagecreatefrompng("images/map/x2.png"); imageColorTransparent($mapposition, imageColorAt($mapposition, 0, 0)); imagecopymerge($im, $mapposition, $resultx, $resulty, 0, 0, 9, 9, 100); } } header("Content-type: image/gif"); imagegif($im); // outputs image to browser imagedestroy($im); } dbclose();
function _adjustTransparency(&$Source, &$Destination) { if ($this->_isTransparent($Source)) { $rgba = imageColorsForIndex($Source, imageColorTransparent($Source)); $color = imageColorAllocate($Destination, $rgba['red'], $rgba['green'], $rgba['blue']); imageColorTransparent($Destination, $color); imageFill($Destination, 0, 0, $color); } else { if ($this->_format == 'png') { imageAlphaBlending($Destination, false); imageSaveAlpha($Destination, true); } elseif ($this->_format != 'gif') { $white = imageColorAllocate($Destination, 255, 255, 255); imageFill($Destination, 0, 0, $white); } } }
/** * * 直接改变图片大小 * @param $image * @param $dw * @param $dh * @return boolean */ public static function change_size($image, $dw = 450, $dh = 450, $npath = null) { if (!file_exists($image)) { return false; } if (!$npath) { $npath = $image; } $img = self::create_from_ext($image); //创建一个和图片2一样大小的真彩色画布(ps:只有这样才能保证后面copy图片1的时候不会失真) $nimg = imageCreatetruecolor($dw, $dh); //为真彩色画布创建白色背景,再设置为透明 $color = imagecolorallocate($nimg, 255, 255, 255); imagefill($nimg, 0, 0, $color); imageColorTransparent($nimg, $color); //首先将图片2画布采样copy到真彩色画布中,不会失真 #如果是执行调整尺寸操作则 $width = imagesx($img); $height = imagesy($img); imagecopyresampled($nimg, $img, 0, 0, 0, 0, $dw, $dh, $width, $height); #重采样拷贝部分图像并调整大小 imagejpeg($nimg, $npath, 100); #以jpeg格式将图像输出到浏览器或文件 #取得文件的类型,根据不同的类型建立不同的对象 return true; }