/** * Method to apply a background color to an image resource. * * @param array $options An array of options for the filter. * color Background matte color * * @return void * * @since 3.4 * @throws InvalidArgumentException * @deprecated 5.0 Use Joomla\Image\Filter\Backgroundfill::execute() instead */ public function execute(array $options = array()) { // Validate that the color value exists and is an integer. if (!isset($options['color'])) { throw new InvalidArgumentException('No color value was given. Expected string or array.'); } $colorCode = !empty($options['color']) ? $options['color'] : null; // Get resource dimensions $width = imagesX($this->handle); $height = imagesY($this->handle); // Sanitize color $rgba = $this->sanitizeColor($colorCode); // Enforce alpha on source image if (imageIsTrueColor($this->handle)) { imageAlphaBlending($this->handle, false); imageSaveAlpha($this->handle, true); } // Create background $bg = imageCreateTruecolor($width, $height); imageSaveAlpha($bg, empty($rgba['alpha'])); // Allocate background color. $color = imageColorAllocateAlpha($bg, $rgba['red'], $rgba['green'], $rgba['blue'], $rgba['alpha']); // Fill background imageFill($bg, 0, 0, $color); // Apply image over background imageCopy($bg, $this->handle, 0, 0, 0, 0, $width, $height); // Move flattened result onto curent handle. // If handle was palette-based, it'll stay like that. imageCopy($this->handle, $bg, 0, 0, 0, 0, $width, $height); // Free up memory imageDestroy($bg); return; }
/** * @param string $name * @param int $colorScheme * @param int $backgroundStyle * * @return $this */ public function generate($name, $colorScheme, $backgroundStyle = null) { $name = strtoupper(substr($name, 0, $this->chars)); $bgColor = $this->colorSchemes[$colorScheme]; $this->avatar = imageCreateTrueColor($this->width, $this->height); imageFill($this->avatar, 0, 0, $bgColor); $this->drawString($name, 0xffffff); return $this; }
/** * @param string $name * @param int $colorScheme * @param int $backgroundStyle * * @return $this */ public function generate($name, $colorScheme, $backgroundStyle) { list($bgColor1, $bgColor2, $textColor) = $this->colorSchemes[$colorScheme]; $this->avatar = imageCreateTrueColor($this->width, $this->height); imageFill($this->avatar, 0, 0, $bgColor1); $this->drawBG($bgColor2, $backgroundStyle); $this->drawString($name, $textColor); $this->copyOverlay(); return $this; }
function Captcha($text, $font, $color) { $C = HexDec($color); $R = floor($C / pow(256, 2)); $G = floor($C % pow(256, 2) / pow(256, 1)); $B = floor($C % pow(256, 2) % pow(256, 1) / pow(256, 0)); $fsize = 32; $bound = array(); $bound = imageTTFBbox($fsize, 0, $font, $text); $this->image = imageCreateTrueColor($bound[4] + 5, abs($bound[5]) + 15); imageFill($this->image, 0, 0, ImageColorAllocate($this->image, 255, 255, 255)); imagettftext($this->image, $fsize, 0, 2, abs($bound[5]) + 5, ImageColorAllocate($this->image, $R, $G, $B), $font, $text); }
public function indexAction() { $size = 300; $image = imagecreatetruecolor($size, $size); // something to get a white background with black border $back = imagecolorallocate($image, 255, 255, 255); $border = imagecolorallocate($image, 0, 0, 0); imagefilledrectangle($image, 0, 0, $size - 1, $size - 1, $back); imagerectangle($image, 0, 0, $size - 1, $size - 1, $border); $yellow_x = 100; $yellow_y = 75; $red_x = 120; $red_y = 165; $blue_x = 187; $blue_y = 125; $radius = 150; // allocate colors with alpha values $yellow = imagecolorallocatealpha($image, 255, 255, 0, 75); $red = imagecolorallocatealpha($image, 255, 0, 0, 75); $blue = imagecolorallocatealpha($image, 0, 0, 255, 75); // drawing 3 overlapped circle imagefilledellipse($image, $yellow_x, $yellow_y, $radius, $radius, $yellow); imagefilledellipse($image, $red_x, $red_y, $radius, $radius, $red); imagefilledellipse($image, $blue_x, $blue_y, $radius, $radius, $blue); // don't forget to output a correct header! header('Content-type: image/png'); // and finally, output the result imagepng($image); imagedestroy($image); exit; $im1 = imagecreatetruecolor(300, 100) or die("Error"); imagealphablending($im1, true); imageSaveAlpha($im1, true); $color = '#FF0000'; $textColorAllocated = ImageColorAllocate($im1, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2))); $backgroundColor = '#FFFF00'; // $bgColor = ImageColorAllocate ($im1, hexdec(substr($backgroundColor,1,2)), hexdec(substr($backgroundColor,3,2)), hexdec(substr($backgroundColor,5,2))); $bgColor = imagecolorallocatealpha($im1, 255, 255, 255, 126); imageFill($im1, 0, 0, $bgColor); imageline($im1, 0, 0, 100, 100, $textColorAllocated); imagettftext($im1, 50, 0, 50, 50, $textColorAllocated, dirname(__FILE__) . '/Headline/arial.ttf', 'asdf'); header('Content-Type: image/png'); imagepng($im1); imagedestroy($im1); exit; }
public function creat_images($num) { $type = 2; header("Content-type: image/PNG"); // 產生種子, 作圖形干擾用 srand((double) microtime() * 10000000000); // 產生圖檔, 及定義顏色 $img_x = 120; $img_y = 28; $im = imageCreate($img_x, $img_y); //ImageColorAllocate 分配圖形的顏色 $back = ImageColorAllocate($im, rand(200, 255), rand(200, 255), rand(200, 255)); $authText = $this->num2adb($num); imageFill($im, 0, 0, $back); // imageString($im, 5, rand(0,55), rand(0,40), $authText, $font); $str_x = 0; $str_y = 0; for ($i = 0; $i < strlen($authText); $i++) { $str_x += rand(10, 20); $str_y = rand(0, $img_y / 2); $font = ImageColorAllocate($im, rand(0, 100), rand(0, 100), rand(0, 100)); imageString($im, 5, $str_x, $str_y, $authText[$i], $font); } // 插入圖形干擾點共 50 點, 可插入更多, 但可能會使圖形太過混雜 for ($i = 0; $i < rand(50, 200); $i++) { $point = ImageColorAllocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($im, rand(0, $img_x), rand(0, $img_y), $point); } for ($i = 1; $i <= rand(2, 5); $i++) { $point = ImageColorAllocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); imageline($im, rand(0, $img_x), rand(0, $img_y), rand(0, $img_x), rand(0, $img_y), $point); } // 定義圖檔類型並輸入, 最後刪除記憶體 if ($type == 1) { ob_start(); ImagePNG($im); $output = ob_get_contents(); ob_end_clean(); echo base64_encode($output); } else { ImagePNG($im); } ImageDestroy($im); }
function make_img($content) { $timage = array(strlen($content) * 20 + 10, 28); // array(largeur, hauteur) de l'image; ici la largeur est fonction du nombre de lettre du contenu, on peut bien sur mettre une largeur fixe. $content = preg_replace('/(\\w)/', '\\1 ', $content); // laisse plus d'espace entre les lettres $image = imagecreatetruecolor($timage[0], $timage[1]); // création de l'image // definition des couleurs $fond = imageColorAllocate($image, 240, 255, 240); $grey = imageColorAllocate($image, 210, 210, 210); $text_color = imageColorAllocate($image, rand(0, 100), rand(0, 50), rand(0, 60)); imageFill($image, 0, 0, $fond); // on remplit l'image de blanc //On remplit l'image avec des polygones for ($i = 0, $imax = mt_rand(3, 5); $i < $imax; $i++) { $x = mt_rand(3, 10); $poly = array(); for ($j = 0; $j < $x; $j++) { $poly[] = mt_rand(0, $timage[0]); $poly[] = mt_rand(0, $timage[1]); } imageFilledPolygon($image, $poly, $x, imageColorAllocate($image, mt_rand(150, 255), mt_rand(150, 255), mt_rand(150, 255))); } // Création des pixels gris for ($i = 0; $i < $timage[0] * $timage[1] / rand(15, 18); $i++) { imageSetPixel($image, rand(0, $timage[0]), rand(0, $timage[1]), $grey); } // affichage du texte demandé; on le centre en hauteur et largeur (à peu près ^^") //imageString($image, 5, ceil($timage[0]-strlen($content)*8)/2, ceil($timage[1]/2)-9, $content, $text_color); $longueur_chaine = strlen($content); for ($ch = 0; $ch < $longueur_chaine; $ch++) { imagettftext($image, 18, mt_rand(-30, 30), 10 * ($ch + 1), mt_rand(18, 20), $text_color, 'res/georgia.ttf', $content[$ch]); } $type = function_exists('imageJpeg') ? 'jpeg' : 'png'; @header('Content-Type: image/' . $type); @header('Cache-control: no-cache, no-store'); $type == 'png' ? imagePng($image) : imageJpeg($image); ImageDestroy($image); exit; }
<?php ## Увеличение картинки со сглаживанием. $tile = imageCreateFromJpeg("sample1.jpg"); $im = imageCreateTrueColor(800, 600); imageFill($im, 0, 0, imageColorAllocate($im, 0, 255, 0)); imageSetTile($im, $tile); // Создаем массив точек со случайными координатами. $p = []; for ($i = 0; $i < 4; $i++) { array_push($p, mt_rand(0, imageSX($im)), mt_rand(0, imageSY($im))); } // Рисуем закрашенный многоугольник. imageFilledPolygon($im, $p, count($p) / 2, IMG_COLOR_TILED); // Выводим результат. header("Content-type: image/jpeg"); // Выводим картинку с максимальным качеством (100). imageJpeg($im, '', 100); // Можно было сжать с помощью PNG. #header("Content-type: image/png"); #imagePng($im);
/** * Функция генерирует и выводит хидер 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); }
/** * @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; }
<?php $im = imageCreateTrueColor(150, 50); $color = imageColorAllocate($im, 128, 128, 128); imageFill($im, 10, 10, $color); $color = imageColorAllocate($im, 255, 255, 255); for ($i = 0; $i < 800; $i++) { $randW = mt_rand(0, imageSX($im)); $randH = mt_rand(0, imageSY($im)); imageSetPixel($im, $randW, $randH, $color); } imageSetThickness($im, 2); $color = imageColorAllocate($im, 100, 100, 100); imageLine($im, 10, 30, 130, 20, $color); $color = imageColorAllocate($im, 70, 70, 70); $n1 = mt_rand(0, 9); imageTtfText($im, 25, 10, mt_rand(2, 10), mt_rand(25, 45), $color, "times.ttf", $n1); $color = imageColorAllocate($im, 255, 0, 50); $str = "ABCDIFGHIJKLMNOPKASTUVWXYZ"; $nw = mt_rand(0, 15); $n2 = $str[$nw]; imageTtftext($im, 22, -10, mt_rand(25, 35), mt_rand(25, 45), $color, "times.ttf", $n2); $color = imageColorAllocate($im, 50, 50, 50); $n3 = mt_rand(0, 9); imageTtfText($im, 25, 15, mt_rand(60, 70), mt_rand(25, 45), $color, "times.ttf", $n3); $color = imageColorAllocate($im, 250, 250, 250); $nw2 = mt_rand(15, 25); $n4 = $str[$nw2]; imageTtfText($im, 22, 30, mt_rand(90, 100), mt_rand(25, 45), $color, "times.ttf", $n4); $color = imageColorAllocate($im, 255, 220, 70); $n5 = mt_rand(0, 9);
/** * Appends information about the source image to the thumbnail. * * @param string $thumbnail * @return string */ protected function appendSourceInfo($thumbnail) { if (!function_exists('imageCreateFromString') || !function_exists('imageCreateTrueColor')) { return $thumbnail; } $imageSrc = imageCreateFromString($thumbnail); // get image size $width = imageSX($imageSrc); $height = imageSY($imageSrc); // increase height $heightDst = $height + self::$sourceInfoLineHeight * 2; // create new image $imageDst = imageCreateTrueColor($width, $heightDst); imageAlphaBlending($imageDst, false); // set background color $background = imageColorAllocate($imageDst, 102, 102, 102); imageFill($imageDst, 0, 0, $background); // copy image imageCopy($imageDst, $imageSrc, 0, 0, 0, 0, $width, $height); imageSaveAlpha($imageDst, true); // get font size $font = 2; $fontWidth = imageFontWidth($font); $fontHeight = imageFontHeight($font); $fontColor = imageColorAllocate($imageDst, 255, 255, 255); // write source info $line1 = $this->sourceName; // imageString supports only ISO-8859-1 encoded strings if (CHARSET != 'ISO-8859-1') { $line1 = StringUtil::convertEncoding(CHARSET, 'ISO-8859-1', $line1); } // truncate text if necessary $maxChars = floor($width / $fontWidth); if (strlen($line1) > $maxChars) { $line1 = $this->truncateSourceName($line1, $maxChars); } $line2 = $this->sourceWidth . 'x' . $this->sourceHeight . ' ' . FileUtil::formatFilesize($this->sourceSize); // write line 1 // calculate text position $textX = 0; $textY = 0; if ($fontHeight < self::$sourceInfoLineHeight) { $textY = intval(round((self::$sourceInfoLineHeight - $fontHeight) / 2)); } if (strlen($line1) * $fontWidth < $width) { $textX = intval(round(($width - strlen($line1) * $fontWidth) / 2)); } imageString($imageDst, $font, $textX, $height + $textY, $line1, $fontColor); // write line 2 // calculate text position $textX = 0; $textY = 0; if ($fontHeight < self::$sourceInfoLineHeight) { $textY = self::$sourceInfoLineHeight + intval(round((self::$sourceInfoLineHeight - $fontHeight) / 2)); } if (strlen($line2) * $fontWidth < $width) { $textX = intval(round(($width - strlen($line2) * $fontWidth) / 2)); } imageString($imageDst, $font, $textX, $height + $textY, $line2, $fontColor); // output image ob_start(); if ($this->imageType == 1 && function_exists('imageGIF')) { @imageGIF($imageDst); $this->mimeType = 'image/gif'; } else { if (($this->imageType == 1 || $this->imageType == 3) && function_exists('imagePNG')) { @imagePNG($imageDst); $this->mimeType = 'image/png'; } else { if (function_exists('imageJPEG')) { @imageJPEG($imageDst, null, 90); $this->mimeType = 'image/jpeg'; } else { return false; } } } @imageDestroy($imageDst); $thumbnail = ob_get_contents(); ob_end_clean(); return $thumbnail; }
<?php session_start(); $my_img = imageCreateFromPng("../icons/marker-icon-red-empty.png"); imageAlphaBlending($my_img, true); imageSaveAlpha($my_img, true); $overlayImage = imageCreateFromPng("../icon.png"); imageAlphaBlending($overlayImage, true); imageSaveAlpha($overlayImage, true); $overlayImageSmall = imageCreateTrueColor(19, 19); imageSaveAlpha($overlayImageSmall, true); $color = imageColorAllocateAlpha($overlayImageSmall, 0, 0, 0, 127); imageFill($overlayImageSmall, 0, 0, $color); imageCopyResampled($overlayImageSmall, $overlayImage, 0, 0, 0, 0, 19, 19, imagesx($overlayImage), imagesy($overlayImage)); imagecopy($my_img, $overlayImageSmall, 3, 5, 0, 0, imagesx($overlayImageSmall), imagesy($overlayImageSmall)); header("Content-type: image/png"); imagepng($my_img); imageDestroy($my_img);
<?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);
function _555() { $b1 = imageTTFBbox($this->_112, 0, $this->_111, $this->_113); $b2 = $b1[4]; $b3 = abs($b1[5]); $im = imageCreateTrueColor($b1[4], abs($b1[5]) + 2); imageFill($im, 0, 0, imageColorAllocate($im, 255, 255, 255)); imagettftext($im, $this->_112, 0, 0, $b3, imageColorAllocate($im, 0, 0, 0), $this->_111, $this->_113); $this->_114 = $x = imageSX($im); $this->_115 = $y = imageSY($im); $c = 0; $q = 1; $this->_057[0] = 0; for ($i = 0; $i < $x; $i++) { for ($j = 0; $j < $y; $j++) { $p = imageColorsForIndex($im, imageColorAt($im, $i, $j)); if ($p['red'] < 128 && $p['green'] < 128 && $p['blue'] < 128) { $this->_057[$q] = $i; $this->_057[$q + 1] = $j; $q += 2; $c += 2; } } } $this->_057[0] = $c; $this->_435($this->_116); }
/** * @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 imageReflect($im, $dir = 1, $spread = 0.55, $decay = 15, $spacing = 0) { // Reflect an image pointer, returning a slightly larger image // // $dir: 1 == "vertical" reflection, 2 == "horizontal" // $spread: % to reflect. defaults to 0.55, or 55% original image // $decay: A value to adjust initial opacity and decaying visibility. lower == more visible $w = imagesx($im); $h = imagesy($im); $vert = $dir & 1; $hori = $dir & 2; // calculate the size of our attachment $nw = $vert ? $w : $w * $spread; $nh = $hori ? $h : $h * $spread; // add our reflection size to the height or width $fw = $w + ($vert ? 0 : $nw); $fh = $h + ($hori ? 0 : $nh); $reflect = imagecreatetruecolor($fw, $fh); imagealphablending($reflect, false); $trans = imagecolorallocatealpha($reflect, 0, 0, 0, 127); imageFill($reflect, 0, 0, $trans); // put the orig on canvas at 0x0 imagecopy($reflect, $im, 0, 0, 0, 0, $w, $h); if ($vert) { // vertical for ($y = 0; $y < $nh; $y++) { $opacity = $decay + ceil(127 * (($y + $decay) / $h)); if ($opacity > 127) { $opacity = 127; } for ($x = 0; $x < $nw; $x++) { $rgba = imagecolorat($im, $x, $h - $y - 1); // break out to function: $r = ($rgba & 0xff0000) >> 16; $g = ($rgba & 0xff00) >> 8; $b = $rgba & 0xff; $ttrans = imagecolorallocatealpha($reflect, $r, $g, $b, $rgba >> 24 >= 124 ? 127 : $opacity); imagesetpixel($reflect, $x, $y + $h + $spacing, $ttrans); } } } if ($hori) { // horizontal for ($x = 0; $x < $nw; $x++) { $opacity = $decay + ceil(127 * (($x + $decay) / $nw)); if ($opacity > 127) { $opacity = 127; } for ($y = 0; $y < $nh; $y++) { $rgba = imagecolorat($im, $w - $x - 1, $y); // FIXME: if $rgba >> 24 === 127, we need to muck opacity and keep it invisible, // otherwise our decay makes it visible but slightly opaque? // break into funciton: $red = ($rgba & 0xff0000) >> 16; $green = ($rgba & 0xff00) >> 8; $blue = $rgba & 0xff; $ttrans = imagecolorallocatealpha($reflect, $red, $green, $blue, $rgba >> 24 >= 124 ? 127 : $opacity); imagesetpixel($reflect, $x + $w + $spacing, $y, $ttrans); } } } imagesavealpha($reflect, true); return $reflect; }
<?php //$i = imageCreate(500, 300); $i = imageCreateTrueColor(500, 300); imageAntiAlias($i, true); $green = imageColorAllocate($i, 156, 14, 200); imageFill($i, 0, 0, $green); $white = imageColorAllocate($i, 255, 255, 255); /* $j=0; while(++$j<1000) imageSetPixel($i,rand(1,500), rand(1,300), $white); */ /* $j=0; while(++$j<1000) imageLine($i, 250, 150, rand(1,500), rand(1,300), $white); */ /* $j=0; while(++$j<100) imageRectangle($i, rand(1,500), rand(1,300), rand(1,500), rand(1,300), $white); */ /* $j=0; while(++$j<10) imageFilledRectangle($i, rand(1,500), rand(1,300), rand(1,500), rand(1,300), $white); */ /* $points = array(0,0,100,200,300,200); imagePolygon($i, $points, 3, $white);
/** * 生成验证码图片 * * @param String $word 验证码在session中的变量名称 */ function valiCode($word = 'randcode') { Header("Content-type: image/png"); $border = 1; //是否要边框 1要:0不要 $how = 4; //验证码位数 $w = $how * 15; //图片宽度 $h = 25; //图片高度 $fontsize = 32; //字体大小 $alpha = "abcdefghijkmnpqrstuvwxyz"; //验证码内容1:字母 $number = "23456789"; //验证码内容2:数字 $randcode = ""; //验证码字符串初始化 srand((double) microtime() * 1000000); //初始化随机数种子 $im = imagecreate($w, $h); //创建验证图片 /* * 绘制基本框架 */ $bgcolor = imagecolorallocate($im, 255, 255, 255); //设置背景颜色 imageFill($im, 0, 0, $bgcolor); //填充背景色 if ($border) { $black = imagecolorallocate($im, 9, 9, 9); //设置边框颜色 imagerectangle($im, 0, 0, $w - 1, $h - 1, $black); //绘制边框 } /* * 逐位产生随机字符 */ for ($i = 0; $i < $how; $i++) { $alpha_or_number = mt_rand(0, 1); //字母还是数字 $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str) - 1); //取哪个字符 $code = substr($str, $which, 1); //取字符 $j = !$i ? 4 : $j + 15; //绘字符位置 $color3 = imagecolorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //字符随即颜色 imagechar($im, $fontsize, $j, 3, $code, $color3); //绘字符 $randcode .= $code; //逐位加入验证码字符串 } /* * 如果需要添加干扰就将注释去掉 * * 以下for()循环为绘背景干扰线代码 */ /* + -------------------------------绘背景干扰线 开始-------------------------------------------- + */ for ($i = 0; $i < 5; $i++) { $color1 = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰线颜色 imagearc($im, mt_rand(-5, $w), mt_rand(-5, $h), mt_rand(20, 300), mt_rand(20, 200), 55, 44, $color1); //干扰线 } /* + -------------------------------绘背景干扰线 结束-------------------------------------- + */ /* * 如果需要添加干扰就将注释去掉 * * 以下for()循环为绘背景干扰点代码 */ /* + --------------------------------绘背景干扰点 开始------------------------------------------ + */ for ($i = 0; $i < $how * 40; $i++) { $color2 = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //干扰点颜色 imageSetPixel($im, mt_rand(0, $w), mt_rand(0, $h), $color2); //干扰点 } /* + --------------------------------绘背景干扰点 结束------------------------------------------ + */ //把验证码字符串写入session 方便提交登录信息时检验验证码是否正确 例如:$_POST['randcode'] = $_SESSION['randcode'] session_start(); $_SESSION[$word] = $randcode; /*绘图结束*/ imagepng($im); imagedestroy($im); /*绘图结束*/ }
$chars = rand(3, 5); $s = strlen($chars_list) - 1; for ($i = 0; $i < $chars; $i++) { $string .= $chars_list[rand(0, $s)]; } // Set the image parameters $img = imageCreate($img_width, $img_height); $white = imageColorAllocate($img, 255, 255, 255); $black = imageColorAllocate($img, 0, 0, 0); // Background if ($img_use_background) { $r = rand($img_rgb_min, $img_rgb_max); $g = rand($img_rgb_min, $img_rgb_max); $b = rand($img_rgb_min, $img_rgb_max); $bg = imageColorAllocate($img, $r, $g, $b); imageFill($img, 0, 0, $bg); } // Circles if ($img_use_circles) { for ($i = 1; $i <= $img_circles; $i++) { $cx = rand(0, $img_width - 10); $cy = rand(0, $img_height - 3); $w = rand(20, 70); $h = rand(20, 70); $r = rand($img_rgb_min, $img_rgb_max); $g = rand($img_rgb_min, $img_rgb_max); $b = rand($img_rgb_min, $img_rgb_max); $color = imageColorAllocate($img, $r, $g, $b); imageFilledEllipse($img, $cx, $cy, $w, $h, $color); } }
<?php /* Создание изображения */ // $i = imageCreate(500, 300); $i = imageCreateTrueColor(500, 300); /* Подготовка к работе */ imageAntiAlias($i, true); $red = imageColorAllocate($i, 255, 0, 0); $white = imageColorAllocate($i, 0xff, 0xff, 0xff); $black = imageColorAllocate($i, 0, 0, 0); $green = imageColorAllocate($i, 0, 255, 0); $blue = imageColorAllocate($i, 0, 0, 255); $grey = imageColorAllocate($i, 192, 192, 192); imageFill($i, 0, 0, $grey); /* Рисуем примитивы */ imageSetPixel($i, 10, 10, $black); imageLine($i, 20, 20, 280, 180, $red); imageRectangle($i, 20, 20, 280, 180, $blue); //array of dots $points = [120, 120, 100, 200, 300, 200]; imagePolygon($i, $points, 3, $green); imageEllipse($i, 200, 150, 300, 200, $red); // imageArc($i, 210, 160, 300, 200, 0, 90, $black); imageFilledArc($i, 200, 150, 300, 200, 0, 40, $red, IMG_ARC_PIE); /* Рисуем текст */ imageString($i, 5, 150, 200, 'php7', $black); imageCharUp($i, 3, 200, 200, 'PHP5', $blue); imageTtfText($i, 30, 10, 300, 150, $green, 'arial.ttf', 'PHP7'); /* Отдаем изображение */ // header("Content-type: image/gif"); // imageGif($i);
private function _makeTransparentImage() { $this->_blank_image = imagecreatetruecolor($this->_image_width, $this->_image_height); imageAlphaBlending($this->_blank_image, TRUE); imageSaveAlpha($this->_blank_image, TRUE); imageFill($this->_blank_image, 0, 0, imagecolorallocatealpha($this->_blank_image, 255, 255, 255, 127)); /** * Loop every Pixel */ for ($x = 0; $x < $this->_image_width; $x++) { for ($y = 0; $y < $this->_image_height; $y++) { /** * Get Color Index from real image and check the alpha state of the corresponding pixel. */ $alpha = imagecolorsforindex($this->_real_img_rs, imagecolorat($this->_real_img_rs, $x, $y)); /** * If no Alpha state just replace rgb color on fresh image */ if ($alpha['alpha'] != 127) { $color = imagecolorsforindex($this->_mod_img_rs, imagecolorat($this->_mod_img_rs, $x, $y)); imagesetpixel($this->_blank_image, $x, $y, imagecolorallocatealpha($this->_blank_image, $color['red'], $color['green'], $color['blue'], $color['alpha'])); } } } }
protected function _paintCanvasOpaque(&$image) { $red = '00'; $green = '00'; $blue = '00'; sscanf($this->params['bgcolor'], "%2x%2x%2x", $red, $green, $blue); $color = imageColorAllocate($image, $red, $green, $blue); imageFill($image, 0, 0, $color); }
// Getting all image sizes $sizes = ['x' => [], 'y' => []]; foreach ($files as $k => $file) { $size = getImageSize($file); $sizes['x'][$k] = $size[0]; $sizes['y'][$k] = $size[1]; } // Getting tile and canvas sizes $tile_size = ['x' => max($sizes['x']), 'y' => max($sizes['y'])]; $canvas_size = ceil(sqrt($count)); $canvas_size = ['x' => $canvas_size * $tile_size['x'], 'y' => $canvas_size * $tile_size['y']]; // Creating the canvas $canvas = imageCreateTrueColor($canvas_size['x'], $canvas_size['y']); // Making it transparent imageAlphaBlending($canvas, false); imageFill($canvas, 0, 0, imageColorAllocateAlpha($canvas, 0, 0, 0, 127)); imageSaveAlpha($canvas, true); // Initializing the CSS array $css = []; // Setting the image input function (it depends on the format) $imageCreateFrom = 'imageCreateFrom' . $input_extension; // Setting the 2D pointer to its initial position $pointer = ['x' => 0, 'y' => 0]; foreach ($files as $id => $file) { // Getting image $image = call_user_func($imageCreateFrom, $file); // Copying image into the canvas imageCopy($canvas, $image, $pointer['x'], $pointer['y'], 0, 0, $sizes['x'][$id], $sizes['y'][$id]); // Destroying image imageDestroy($image); // Adding coordinates to the CSS array
/** * Enable transparency on an image resource * * @param resource $image Image resource * @param array $transparentColor Transparent color * @return void */ protected static function _enableTranparency($image, array $transparentColor = null) { if ($transparentColor === null) { $transparentColor = array('red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => 127); } $targetTransparent = imagecolorallocatealpha($image, $transparentColor['red'], $transparentColor['green'], $transparentColor['blue'], $transparentColor['alpha']); $targetTransparentIndex = imagecolortransparent($image, $targetTransparent); imageFill($image, 0, 0, $targetTransparent); imagealphablending($image, false); imagesavealpha($image, true); }
<?php header("Content-Type: image/png"); $imageWidth = 600; $imageHeight = 600; $text = "PHP Course"; $font = 2; $textLength = strlen($text); $border1 = 20; $border2 = 60; $border3 = 120; $image = imageCreateTrueColor($imageWidth, $imageHeight); $black = imageColorAllocate($image, 0, 0, 0); $purple = imageColorAllocate($image, 255, 0, 255); $white = imageColorAllocate($image, 255, 255, 255); $red = imageColorAllocate($image, 255, 0, 0); imageFill($image, 0, 0, $black); imagefilledrectangle($image, $border1, $border1, $imageWidth - $border1, $imageHeight - $border1, $red); imagefilledrectangle($image, $border2, $border2, $imageWidth - $border2, $imageHeight - $border2, $purple); imagefilledrectangle($image, $border3, $border3, $imageWidth - 90, $imageHeight - 120, $black); imageString($image, $font, $imageWidth / 2 - $textLength, $imageHeight / 2, $text, $white); imagePNG($image); imageDestroy($image);
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); } } }
/** * Get canvas * * @param integer $width * @param integer $height * @param Asido_Color &$color * @return Asido_TMP * @access protected */ function __canvas($width, $height, &$color) { $t = new Asido_TMP(); $t->target = imageCreateTrueColor($width, $height); list($r, $g, $b) = $color->get(); imageFill($t->target, 1, 1, imageColorAllocate($t->target, $r, $g, $b)); $t->image_width = $width; $t->image_height = $height; return $t; }
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 ; }
public function generate($name, $colorScheme, $backgroundStyle) { list($bgColor1, $bgColor2, $textColor) = self::$colorSchemes[$colorScheme]; $this->avatar = imageCreateTrueColor($this->width, $this->height); imageFill($this->avatar, 0, 0, $bgColor1); // Draw some random chars into the background. Unlike the other GD drawing functions // (imageFilledArc, imageFilledPolygon etc.) imageTTFText is anti-aliased. $sizeFactor = $this->width / 40; switch ($backgroundStyle) { case 0: imageTTFText($this->avatar, 190 * $sizeFactor, 10, 0, 35 * $sizeFactor, $bgColor2, $this->fontFace, 'O'); break; case 1: imageTTFText($this->avatar, 90 * $sizeFactor, 0, -30 * $sizeFactor, 45 * $sizeFactor, $bgColor2, $this->fontFace, 'o'); break; case 2: imageTTFText($this->avatar, 90 * $sizeFactor, 0, -30 * $sizeFactor, 30 * $sizeFactor, $bgColor2, $this->fontFace, '>'); break; case 3: imageTTFText($this->avatar, 90 * $sizeFactor, 0, -30 * $sizeFactor, 45 * $sizeFactor, $bgColor2, $this->fontFace, '//'); break; } // Draw the first few chars of the name imageTTFText($this->avatar, $this->fontSize, 0, 4, $this->height - $this->fontSize / 2, $textColor, $this->fontFace, mb_substr($name, 0, $this->chars)); // Copy the overlay on top if ($this->overlay) { imageCopy($this->avatar, $this->overlay, 0, 0, 0, 0, imageSX($this->overlay), imageSY($this->overlay)); } }