function make_captcha_img() { global $CaptchaBGs; $Length = 6; $ImageHeight = 75; $ImageWidth = 300; $Chars = 'abcdefghjkmprstuvwxyzABCDEFGHJKLMPQRSTUVWXY23456789'; $CaptchaString = ''; for ($i = 0; $i < $Length; $i++) { $CaptchaString .= $Chars[mt_rand(0, strlen($Chars) - 1)]; } for ($x = 0; $x < $Length; $x++) { $FontDisplay[$x]['size'] = mt_rand(24, 32); $FontDisplay[$x]['top'] = mt_rand($FontDisplay[$x]['size'] + 5, $ImageHeight - $FontDisplay[$x]['size'] / 2); $FontDisplay[$x]['angle'] = mt_rand(-30, 30); $FontDisplay[$x]['font'] = get_font(); } $Img = imagecreatetruecolor($ImageWidth, $ImageHeight); $BGImg = imagecreatefrompng(SERVER_ROOT . '/captcha/' . $CaptchaBGs[mt_rand(0, count($CaptchaBGs) - 1)]); imagecopymerge($Img, $BGImg, 0, 0, 0, 0, 300, 75, 50); $ForeColor = imagecolorallocatealpha($Img, 255, 255, 255, 65); for ($i = 0; $i < strlen($CaptchaString); $i++) { $CharX = $ImageWidth / $Length * ($i + 1) - $ImageWidth / $Length * 0.75; imagettftext($Img, $FontDisplay[$i]['size'], $FontDisplay[$i]['angle'], $CharX, $FontDisplay[$i]['top'], $ForeColor, $FontDisplay[$i]['font'], $CaptchaString[$i]); } header('Content-type: image/png'); imagepng($Img); imagedestroy($Img); return $CaptchaString; }
public static function ImageHue(&$image, $hue, $saturation) { $width = imagesx($image); $height = imagesy($image); for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $rgb = imagecolorat($image, $x, $y); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; $alpha = ($rgb & 0x7f000000) >> 24; $hsl = ColorUtils::rgb2hsl(array('r' => $r, 'g' => $g, 'b' => $b)); $h = $hsl['h'] / 360; $s = $hsl['s'] / 100; $h += $hue / 360; $s += $saturation / 100; if ($h > 1) { $h--; } if ($s > 1) { $s--; } $hsl['h'] = $h * 360; $hsl['s'] = $s * 100; $rgb = ColorUtils::hsl2rgb($hsl); imagesetpixel($image, $x, $y, imagecolorallocatealpha($image, $rgb['r'], $rgb['g'], $rgb['b'], $alpha)); } } }
/** * Method to apply a filter to an image resource. * * @param array $options An array of options for the filter. * * @return void * * @throws InvalidArgumentException * @throws RuntimeException */ public function execute(array $options = array()) { // Verify that image filter support for PHP is available. if (!function_exists('imagefilter')) { throw new RuntimeException('The imagefilter function for PHP is not available.'); } if (empty($options)) { throw new InvalidArgumentException('No valid amount was given. Expected float.'); } $value = (int) array_shift($options); if ($value == 0) { $value = 128; } $width = imagesx($this->handle); $height = imagesy($this->handle); for ($x = 0; $x < $width; ++$x) { for ($y = 0; $y < $height; ++$y) { $index = imagecolorat($this->handle, $x, $y); $rgb = imagecolorsforindex($this->handle, $index); $r = $rgb['red']; $g = $rgb['green']; $b = $rgb['blue']; $a = $rgb['alpha']; $v = round(($r + $g + $b) / 3) >= $value ? 255 : 0; $color = imagecolorallocatealpha($this->handle, $v, $v, $v, $a); if ($color === false) { $color = imagecolorclosestalpha($this->handle, $v, $v, $v, $a); } imagesetpixel($this->handle, $x, $y, $color); } } }
function render_block($left_side, $top_side, $right_side) { global $size; $size = 2048; $x1 = (2 - sqrt(3)) * 0.25 * $size; $x2 = 0.5 * $size; $x3 = (2 + sqrt(3)) * 0.25 * $size; $y1 = 0; $y2 = 0.25 * $size; $y3 = 0.5 * $size; $y4 = 0.75 * $size; $y5 = $size; $first_poligon = array($x1, $y2, $x2, $y3, $x2, $y5, $x1, $y4); $second_poligon = array($x1, $y2, $x2, $y1, $x3, $y2, $x2, $y3); $third_poligon = array($x2, $y3, $x3, $y2, $x3, $y4, $x2, $y5); $im = imagecreatetruecolor($size, $size); // Transparentbackground imagealphablending($im, true); imagesavealpha($im, true); $trans = imagecolorallocatealpha($im, 0, 0, 0, 127); imagefill($im, 0, 0, $trans); imagetranslatedtexture($im, $first_poligon, imagelight(load_png($left_side), 96)); imagetranslatedtexture($im, $second_poligon, load_png($top_side)); imagetranslatedtexture($im, $third_poligon, imagelight(load_png($right_side), 64)); return $im; }
/** * Background fill an image using the provided color * * @param int $width The desired width of the new image * @param int $height The desired height of the new image * @param Array the desired pad colors in RGB format, array should be array( 'top' => '', 'right' => '', 'bottom' => '', 'left' => '' ); */ private function fill_color(array $colors) { $current_size = $this->editor->get_size(); $size = array('width' => $this->args['width'], 'height' => $this->args['height']); $offsetLeft = ($size['width'] - $current_size['width']) / 2; $offsetTop = ($size['height'] - $current_size['height']) / 2; $new_image = imagecreatetruecolor($size['width'], $size['height']); // This is needed to support alpha imagesavealpha($new_image, true); imagealphablending($new_image, false); // Check if we are padding vertically or horizontally if ($current_size['width'] != $size['width']) { $colorToPaint = imagecolorallocatealpha($new_image, substr($colors['left'], 0, 3), substr($colors['left'], 3, 3), substr($colors['left'], 6, 3), substr($colors['left'], 9, 3)); // Fill left color imagefilledrectangle($new_image, 0, 0, $offsetLeft + 5, $size['height'], $colorToPaint); $colorToPaint = imagecolorallocatealpha($new_image, substr($colors['right'], 0, 3), substr($colors['right'], 3, 3), substr($colors['right'], 6, 3), substr($colors['left'], 9, 3)); // Fill right color imagefilledrectangle($new_image, $offsetLeft + $current_size['width'] - 5, 0, $size['width'], $size['height'], $colorToPaint); } elseif ($current_size['height'] != $size['height']) { $colorToPaint = imagecolorallocatealpha($new_image, substr($colors['top'], 0, 3), substr($colors['top'], 3, 3), substr($colors['top'], 6, 3), substr($colors['left'], 9, 3)); // Fill top color imagefilledrectangle($new_image, 0, 0, $size['width'], $offsetTop + 5, $colorToPaint); $colorToPaint = imagecolorallocatealpha($new_image, substr($colors['bottom'], 0, 3), substr($colors['bottom'], 3, 3), substr($colors['bottom'], 6, 3), substr($colors['left'], 9, 3)); // Fill bottom color imagefilledrectangle($new_image, 0, $offsetTop - 5 + $current_size['height'], $size['width'], $size['height'], $colorToPaint); } imagecopy($new_image, $this->editor->get_image(), $offsetLeft, $offsetTop, 0, 0, $current_size['width'], $current_size['height']); $this->editor->update_image($new_image); $this->editor->update_size(); }
protected function setResource() { $this->resource = imagecreatetruecolor($this->background->width, $this->background->height); if (isset($this->fill) === true) { $backgroundindex = imagecolorallocate($this->resource, $this->fill['red'], $this->fill['green'], $this->fill['blue']); imagefill($this->resource, 0, 0, $backgroundindex); } else { if ($this->format === 'gif') { $backgroundindex = imagecolorallocatealpha($this->resource, 255, 255, 255, 127); imagefill($this->resource, 0, 0, $backgroundindex); imagecolortransparent($this->resource, $backgroundindex); } else { if ($this->format === 'jpeg') { $backgroundindex = imagecolorallocate($this->resource, 255, 255, 255); imagefill($this->resource, 0, 0, $backgroundindex); } else { if ($this->format === 'png') { imagealphablending($this->resource, false); imagesavealpha($this->resource, true); $backgroundindex = imagecolorallocatealpha($this->resource, 255, 255, 255, 127); imagefill($this->resource, 0, 0, $backgroundindex); imagealphablending($this->resource, true); } } } } }
/** * Wrapper function for 'imagecopyresampled' * * @param Image $image * @param integer $dst_x * @param integer $dst_y * @param integer $src_x * @param integer $src_y * @param integer $dst_w * @param integer $dst_h * @param integer $src_w * @param integer $src_h * @return boolean */ protected function modify($image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { foreach ($image as $frame) { // create new image $modified = imagecreatetruecolor($dst_w, $dst_h); // get current image $resource = $frame->getCore(); // preserve transparency $transIndex = imagecolortransparent($resource); if ($transIndex != -1) { $rgba = imagecolorsforindex($modified, $transIndex); $transColor = imagecolorallocatealpha($modified, $rgba['red'], $rgba['green'], $rgba['blue'], 127); imagefill($modified, 0, 0, $transColor); imagecolortransparent($modified, $transColor); } else { imagealphablending($modified, false); imagesavealpha($modified, true); } // copy content from resource imagecopyresampled($modified, $resource, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); // free memory of old core imagedestroy($resource); // set new content as recource $frame->setCore($modified); } return true; }
public function Resize($image, $newWidth, $targetName) { if (!file_exists(PUBLIC_ROOT . $image)) { $image = '/assets/images/not-found.gif'; } $imgInfo = getimagesize(PUBLIC_ROOT . $image); $oldWidth = $imgInfo[0]; $oldHeight = $imgInfo[1]; $changeRatio = $oldWidth / $newWidth; $newHeight = round($oldHeight / $changeRatio); $newImage = imagecreatetruecolor($newWidth, $newHeight); $source = $this->load(PUBLIC_ROOT . $image); if ($this->imageType == IMAGETYPE_PNG) { imagealphablending($newImage, false); imagesavealpha($newImage, true); $transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127); imagefilledrectangle($newImage, 0, 0, $newWidth, $newHeight, $transparent); } imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight); header('Content-Type: image/jpeg'); imagejpeg($newImage, $targetName, 100); Debugger::debug($targetName, 'TARGET'); //$this->save($targetName); $this->image = $newImage; imagedestroy($newImage); }
/** * @return ZipInterface */ public function render() { $pathThumbnail = $this->getPresentation()->getPresentationProperties()->getThumbnailPath(); if ($pathThumbnail) { // Size : 128x128 pixel // PNG : 8bit, non-interlaced with full alpha transparency $gdImage = imagecreatefromstring(file_get_contents($pathThumbnail)); if ($gdImage) { list($width, $height) = getimagesize($pathThumbnail); $gdRender = imagecreatetruecolor(128, 128); $colorBgAlpha = imagecolorallocatealpha($gdRender, 0, 0, 0, 127); imagecolortransparent($gdRender, $colorBgAlpha); imagefill($gdRender, 0, 0, $colorBgAlpha); imagecopyresampled($gdRender, $gdImage, 0, 0, 0, 0, 128, 128, $width, $height); imagetruecolortopalette($gdRender, false, 255); imagesavealpha($gdRender, true); ob_start(); imagepng($gdRender); $imageContents = ob_get_contents(); ob_end_clean(); imagedestroy($gdRender); imagedestroy($gdImage); $this->getZip()->addFromString('Thumbnails/thumbnail.png', $imageContents); } } return $this->getZip(); }
function createImage($name, $filename, $new_w, $new_h) { $system2 = explode('.', strtolower(basename($filename))); $system2[1] = $system2[1]; $src_img = imagecreatefromstring(readFileData($name)); $old_w = imageSX($src_img); $old_h = imageSY($src_img); $thumb_w = $new_w; $thumb_h = $new_h; if ($new_w > $old_w) { $thumb_w = $old_w; $thumb_h = $thumb_w / $old_w * $old_h; } else { $thumb_w = $new_w; $thumb_h = $thumb_w / $old_w * $old_h; } if ($thumb_h > $new_h) { $thumb_h = $new_h; $thumb_w = $thumb_h / $old_h * $old_w; } $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h); imagealphablending($dst_img, false); imagesavealpha($dst_img, true); $transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127); imagefilledrectangle($dst_img, 0, 0, $thumb_w, $thumb_h, $transparent); imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_w, $old_h); if (preg_match("/png/", $system2[1])) { imagepng($dst_img, $filename); } else { imagejpeg($dst_img, $filename, 90); } imagedestroy($dst_img); imagedestroy($src_img); }
public function getImage($data) { $pngData = $this->encode($data); if ($pngData == null) { return null; } $h = count($pngData); $w = strlen($pngData[0]); $imgW = $w + 2 * $this->outerFrame; $imgH = $h + 2 * $this->outerFrame; $qrcode_image = imagecreatetruecolor($imgW, $imgH); imagealphablending($qrcode_image, false); $qrBackColor = imagecolorallocatealpha($qrcode_image, 255, 255, 255, 127); imagefill($qrcode_image, 0, 0, $qrBackColor); imagesavealpha($qrcode_image, true); if (!empty($this->fgcolor) && (substr($this->fgcolor, 0, 1) == '#' && strlen(trim($this->fgcolor)) == 7 || strlen(trim($this->fgcolor)) == 6)) { $rgb = str_split(ltrim($this->fgcolor, '#'), 2); $qrColor = imagecolorallocatealpha($qrcode_image, hexdec($rgb[0]), hexdec($rgb[1]), hexdec($rgb[2]), 0); } else { $qrColor = imagecolorallocatealpha($qrcode_image, 0, 0, 0, 0); } for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { if ($pngData[$y][$x] == '1') { imagesetpixel($qrcode_image, $x + $this->outerFrame, $y + $this->outerFrame, $qrColor); } } } return $qrcode_image; }
/** * Draws some text on the handle * * @param GD-object $handle The handle on which the ellipse is drawn * @param Zend_Image_Action_DrawText $textObject The object that with all info */ public function perform($handle, Zend_Image_Action_DrawText $textObject) { // As of ZF2.0 / PHP5.3, this can be made static. $color = Zend_Image_Color::calculateHex($textObject->getColor()); $colorAlphaAlloc = imagecolorallocatealpha($handle, $color['red'], $color['green'], $color['blue'], 127 - $textObject->getAlpha()); return $handle; }
/** * @param int $width Target width * @param int $height Target height * @param bool $toFit If true, image fill fit to given dimensions, if false, it will cover them * @param bool $force If true, image will be resized even if target dimensions are larger than original */ protected function scale($width, $height, $toFit, $force) { if (null === $this->_image) { return; } $rawWidth = $this->_getWidth(); $rawHeight = $this->_getHeight(); $widthOver = $rawWidth / $width; $heightOver = $rawHeight / $height; if ($toFit) { $scalingFactor = max($widthOver, $heightOver); } else { $scalingFactor = min($widthOver, $heightOver); } if ($scalingFactor > 1 || $force) { $destWidth = $rawWidth / $scalingFactor; $destHeight = $rawHeight / $scalingFactor; $destImage = imagecreatetruecolor($destWidth, $destHeight); imagealphablending($destImage, false); imagesavealpha($destImage, true); $transparent = imagecolorallocatealpha($destImage, 255, 255, 255, 127); imagefill($destImage, 0, 0, $transparent); imagecopyresampled($destImage, $this->_image, 0, 0, 0, 0, $destWidth, $destHeight, $rawWidth, $rawHeight); $this->_image = $destImage; } }
/** * {@inherit-doc} * @see Hbarcelos\GdWrapper\Resource\EmptyResource::createRaw() */ protected function createRaw($width, $height) { $raw = imagecreatetruecolor($width, $height); $transparent = imagecolorallocatealpha($raw, 255, 255, 255, 127); imagefilledrectangle($raw, 0, 0, $width, $height, $transparent); return $raw; }
public function resize($width = 0, $height = 0) { if (!$this->info['width'] || !$this->info['height']) { return; } $scale = min($width / $this->info['width'], $height / $this->info['height']); if ($scale == 1 && $this->info['mime'] != 'image/png') { return; } $new_width = (int) ($this->info['width'] * $scale); $new_height = (int) ($this->info['height'] * $scale); $xpos = (int) (($width - $new_width) / 2); $ypos = (int) (($height - $new_height) / 2); $image_old = $this->image; $this->image = imagecreatetruecolor($width, $height); if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') { imagealphablending($this->image, false); imagesavealpha($this->image, true); $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127); imagecolortransparent($this->image, $background); } else { $background = imagecolorallocate($this->image, 255, 255, 255); } imagefilledrectangle($this->image, 0, 0, $width, $height, $background); imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']); imagedestroy($image_old); $this->info['width'] = $width; $this->info['height'] = $height; }
public function createCaptcha() { $captcha = ''; $symbol = '0'; $width = 420; $height = 70; $font = 'fonts/bellb.ttf'; $fontsize = 20; $captchaLength = rand(1, 1); $im = imagecreatetruecolor($width, $height); $bg = imagecolorallocatealpha($im, 0, 0, 0, 127); imagefill($im, 0, 0, $bg); for ($i = 0; $i < $captchaLength; $i++) { $captcha .= $symbol[rand(0, strlen($symbol) - 1)]; $x = ($width - 20) / $captchaLength * $i + 10; $x = rand($x, $x + 4); $y = $height - ($height - $fontsize) / 2; $curcolor = imagecolorallocate($im, rand(0, 100), rand(0, 100), rand(0, 100)); $angle = rand(-25, 25); imagettftext($im, $fontsize, $angle, $x, $y, $curcolor, $font, $captcha[$i]); } session_start(); $_SESSION['captcha'] = $captcha; header('Content-type: image/png'); imagepng($im); imagedestroy($im); }
/** * Add an image to the generator. * * This function adds a source image to the generator. It serves two main purposes: add a source image if one was * not supplied to the constructor and to add additional source images so that different images can be supplied for * different sized images in the resulting ICO file. For instance, a small source image can be used for the small * resolutions while a larger source image can be used for large resolutions. * * @param string $file Path to the source image file. * @param array $sizes Optional. An array of sizes (each size is an array with a width and height) that the source image should be rendered at in the generated ICO file. If sizes are not supplied, the size of the source image will be used. * @return boolean true on success and false on failure. */ public function add_image($file, $sizes = array()) { if (!$this->_has_requirements) { return false; } if (false === ($im = $this->_load_image_file($file))) { return false; } if (empty($sizes)) { $sizes = array(imagesx($im), imagesy($im)); } // If just a single size was passed, put it in array. if (!is_array($sizes[0])) { $sizes = array($sizes); } foreach ((array) $sizes as $size) { list($width, $height) = $size; $new_im = imagecreatetruecolor($width, $height); imagecolortransparent($new_im, imagecolorallocatealpha($new_im, 0, 0, 0, 127)); imagealphablending($new_im, false); imagesavealpha($new_im, true); $source_width = imagesx($im); $source_height = imagesy($im); if (false === imagecopyresampled($new_im, $im, 0, 0, 0, 0, $width, $height, $source_width, $source_height)) { continue; } $this->_add_image_data($new_im); } return true; }
/** * Reduces colors of a given image * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $count = $this->argument(0)->value(); $matte = $this->argument(1)->value(); // get current image size $size = $image->getSize(); // create empty canvas $resource = imagecreatetruecolor($size->width, $size->height); // define matte if (is_null($matte)) { $matte = imagecolorallocatealpha($resource, 255, 255, 255, 127); } else { $matte = $image->getDriver()->parseColor($matte)->getInt(); } // fill with matte and copy original image imagefill($resource, 0, 0, $matte); // set transparency imagecolortransparent($resource, $matte); // copy original image imagecopy($resource, $image->getCore(), 0, 0, 0, 0, $size->width, $size->height); if (is_numeric($count) && $count <= 256) { // decrease colors imagetruecolortopalette($resource, true, $count); } // set new resource $image->setCore($resource); return true; }
/** * 创建图像 * * @return resource */ protected function _createImage() { if (!is_resource($this->_image)) { extract($this->_options); //解出参数到变量 $this->_image = imagecreatetruecolor($width, $height); $color1 = imagecolorallocate($this->_image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255)); $color2 = imagecolorallocate($this->_image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255)); $color1 = imagecolorsforindex($this->_image, $color1); $color2 = imagecolorsforindex($this->_image, $color2); $steps = $width; $r1 = ($color1['red'] - $color2['red']) / $steps; $g1 = ($color1['green'] - $color2['green']) / $steps; $b1 = ($color1['blue'] - $color2['blue']) / $steps; $x1 = 0; $y1 =& $i; $x2 = $width; $y2 =& $i; for ($i = 0; $i <= $steps; $i++) { $r2 = $color1['red'] - floor($i * $r1); $g2 = $color1['green'] - floor($i * $g1); $b2 = $color1['blue'] - floor($i * $b1); $color = imagecolorallocate($this->_image, $r2, $g2, $b2); imageline($this->_image, $x1, $y1, $x2, $y2, $color); } for ($i = 0, $count = mt_rand(10, 20); $i < $count; $i++) { $color = imagecolorallocatealpha($this->_image, mt_rand(20, 255), mt_rand(20, 255), mt_rand(100, 255), mt_rand(80, 120)); imageline($this->_image, mt_rand(0, $width), 0, mt_rand(0, $width), $height, $color); } } return $this->_image; }
/** * Applies filter effects to the given image * * @param Image\Image $image The image to filter. * * @return Image\Image The filtered image. * * @throws FilterException if the image filter algorithm fails. */ public function applyFilter(Image\Image $image) { if ($this->level <= 0) { $gd = $image->getCore(); $width = imagesx($gd); $height = imagesy($gd); for ($x = 0; $x < $width; ++$x) { for ($y = 0; $y < $height; ++$y) { $rgba = imagecolorsforindex($gd, imagecolorat($gd, $x, $y)); $r = $rgba['red']; $g = $rgba['green']; $b = $rgba['blue']; $a = $rgba['alpha']; $level = $this->level * -1; $max = max($r, $g, $b); $avg = ($r + $g + $b) / 3; $amt = abs($max - $avg) * 2 / 255 * $level / 100; if ($r !== $max) { $r += ($max - $r) * $amt; } if ($g !== $max) { $g += ($max - $g) * $amt; } if ($b !== $max) { $b += ($max - $b) * $amt; } imagesetpixel($gd, $x, $y, imagecolorallocatealpha($gd, $r, $g, $b, $a)); } } $image->setCore($gd); } else { $image->filter(new SaturateFilter($this->level)); } return $image; }
/** * Return captcha image * @param number $maxChar */ public function generate($maxChar = 4) { // $characters = '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'; $characters = 'ABCDEFGHKMNPQRST'; $captchaText = ''; for ($i = 0; $i < $maxChar; $i++) { $captchaText .= $characters[rand(0, strlen($characters) - 1)]; } strtoupper(substr(md5(microtime()), 0, 7)); \Session::put('captchaHash', \Hash::make($captchaText)); $image = imagecreate(30 * $maxChar, 35); $background = imagecolorallocatealpha($image, 255, 255, 255, 1); $textColor = imagecolorallocatealpha($image, 206, 33, 39, 1); $x = 5; $y = 20; $angle = 0; for ($i = 0; $i < 7; $i++) { $fontSize = 16; $text = substr($captchaText, $i, 1); imagettftext($image, $fontSize, $angle, $x, $y, $textColor, public_path('/fonts/LibreBaskerville/librebaskerville-regular.ttf'), $text); $x = $x + 17 + mt_rand(1, 10); $y = mt_rand(18, 25); $angle = mt_rand(0, 20); } header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate'); header('Pragma: no-cache'); header('Content-type: image/jpeg'); imagejpeg($image, null, 100); imagedestroy($image); }
function imagemergealpha($i) { //create a new image $s = imagecreatetruecolor(imagesx($i[0]), imagesy($i[1])); $back_color = imagecolorallocate($s, 0xa9, 0xb1, 0xd3); //merge all images imagealphablending($s, true); $z = $i; while ($d = each($z)) { imagecopy($s, $d[1], 0, 0, 0, 0, imagesx($d[1]), imagesy($d[1])); } //restore the transparency imagealphablending($s, false); $w = imagesx($s); $h = imagesy($s); for ($x = 0; $x < $w; $x++) { for ($y = 0; $y < $h; $y++) { $c = imagecolorat($s, $x, $y); $c = imagecolorsforindex($s, $c); $z = $i; $t = 0; while ($d = each($z)) { $ta = imagecolorat($d[1], $x, $y); $ta = imagecolorsforindex($d[1], $ta); $t += 127 - $ta['alpha']; } $t = $t > 127 ? 127 : $t; $t = 127 - $t; $c = imagecolorallocatealpha($s, $c['red'], $c['green'], $c['blue'], $t); imagesetpixel($s, $x, $y, $c); } } imagesavealpha($s, true); return $s; }
namespace common; class upload { //从tmp中移动upload public static function zoom(&$file, &$maxWidth = 0, &$maxHeight = 0) { list($width, $height, $im, $func, $ext) = self::_init($file); if (!$im) { \yk\log::runlog('file upload error: im not found', 'upload'); return false; } if ($maxWidth > 0) { $p = max($width / $maxWidth, $height / $maxHeight); $dstwidth = intval($width / $p); $dstheight = intval($height / $p); } else { $dstwidth = $width; $dstheight = $height; } $maxWidth = $dstwidth; $maxHeight = $dstheight; $dstim = imagecreatetruecolor($dstwidth, $dstheight); imagealphablending($dstim, false); //关闭混杂模式,不可缺少, PHP文档中说明: (在非混色模式下,画笔颜色连同其 alpha 通道信息一起被拷贝,替换掉目标像素。混色模式在画调色板图像时不可用。) 而且是imagesavealpha方法起作用的前置步骤. imagesavealpha($dstim, true); //保存 PNG 图像时保存完整的 alpha 通道信息 $transparent = imagecolorallocatealpha($dstim, 255, 255, 255, 127); //取得一个透明的颜色, 透明度在 0-127 间 imagefill($dstim, 0, 0, $transparent); imagecopyresampled($dstim, $im, 0, 0, 0, 0, $dstwidth, $dstheight, $width, $height); $file = uniqid() . $ext;
/** * Build the CSS sprite in memory * @return Tinyfier_Image_Tool */ public function build() { //Sort images inside the sprite $y = 0; foreach ($this->_images as $image) { $image->top = $y; $image->left = 0; $y += $image->image->height(); } //Draw sprite $w = 0; $h = 0; foreach ($this->_images as $image) { $w = max($w, $image->left + $image->image->width()); $h = max($w, $image->top + $image->image->height()); } $sprite = imagecreatetruecolor($w, $h); imagealphablending($sprite, false); //Soporte de transparencias imagefill($sprite, 0, 0, imagecolorallocatealpha($sprite, 0, 0, 0, 127)); //Fondo transparente foreach ($this->_images as $image) { imagecopy($sprite, $image->image->handle(), $image->left, $image->top, 0, 0, $image->image->width(), $image->image->height()); } return new Tinyfier_Image_Tool($sprite); }
/** * Outputs the Captcha image. * * @param boolean html output * @return mixed */ public function render($html) { // Creates a black image to start from $this->image_create(Captcha::$config['background']); // Add random white/gray arcs, amount depends on complexity setting $count = (Captcha::$config['width'] + Captcha::$config['height']) / 2; $count = $count / 5 * min(10, Captcha::$config['complexity']); for ($i = 0; $i < $count; $i++) { imagesetthickness($this->image, mt_rand(1, 2)); $color = imagecolorallocatealpha($this->image, 255, 255, 255, mt_rand(0, 120)); imagearc($this->image, mt_rand(-Captcha::$config['width'], Captcha::$config['width']), mt_rand(-Captcha::$config['height'], Captcha::$config['height']), mt_rand(-Captcha::$config['width'], Captcha::$config['width']), mt_rand(-Captcha::$config['height'], Captcha::$config['height']), mt_rand(0, 360), mt_rand(0, 360), $color); } // Use different fonts if available $font = Captcha::$config['fontpath'] . Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])]; // Draw the character's white shadows $size = (int) min(Captcha::$config['height'] / 2, Captcha::$config['width'] * 0.8 / strlen($this->response)); $angle = mt_rand(-15 + strlen($this->response), 15 - strlen($this->response)); $x = mt_rand(1, Captcha::$config['width'] * 0.9 - $size * strlen($this->response)); $y = (Captcha::$config['height'] - $size) / 2 + $size; $color = imagecolorallocate($this->image, 255, 255, 255); imagefttext($this->image, $size, $angle, $x + 1, $y + 1, $color, $font, $this->response); // Add more shadows for lower complexities Captcha::$config['complexity'] < 10 and imagefttext($this->image, $size, $angle, $x - 1, $y - 1, $color, $font, $this->response); Captcha::$config['complexity'] < 8 and imagefttext($this->image, $size, $angle, $x - 2, $y + 2, $color, $font, $this->response); Captcha::$config['complexity'] < 6 and imagefttext($this->image, $size, $angle, $x + 2, $y - 2, $color, $font, $this->response); Captcha::$config['complexity'] < 4 and imagefttext($this->image, $size, $angle, $x + 3, $y + 3, $color, $font, $this->response); Captcha::$config['complexity'] < 2 and imagefttext($this->image, $size, $angle, $x - 3, $y - 3, $color, $font, $this->response); // Finally draw the foreground characters $color = imagecolorallocate($this->image, 0, 0, 0); imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response); // Output return $this->image_render($html); }
public function show() { $captchaText = strtoupper(substr(md5(microtime()), 0, 7)); $this->Session->write('captcha', $captchaText); $image = imagecreate(190, 70); $background = imagecolorallocate($image, 255, 255, 255); $textColor = imagecolorallocatealpha($image, 0, 0, 0, 1); $x = 5; $y = 50; for ($i = 0; $i < 3000; $i++) { $fontSize = mt_rand(15, 28); $text = substr($captchaText, $i, 1); imagettftext($image, $fontSize, 0, $x, $y, $textColor, './impact.ttf', $text); $x = $x + 17 + mt_rand(0, 10); $y = mt_rand(40, 65); imagesetpixel($image, rand() % 235, rand() % 85, 3000000); } $line_color = imagecolorallocate($image, 4, rand() % 110, rand() % 220); for ($i = 0; $i < 10; $i++) { imageline($image, 0, rand() % 50, 200, rand() % 120, $line_color); } header("Content-type: application/jpeg"); return imagejpeg($image); imagedestroy($image); }
function embroidery2image($embroidery, $scale_post = 1, $scale_pre = false) { // Create image $im = imagecreatetruecolor(ceil($embroidery->imageWidth * $scale_post), ceil($embroidery->imageHeight * $scale_post)); imagesavealpha($im, true); imagealphablending($im, false); $color = imagecolorallocatealpha($im, 255, 255, 255, 127); imagefill($im, 0, 0, $color); // Draw stitches foreach ($embroidery->blocks as $block) { $color = imagecolorallocate($im, $block->color->r, $block->color->g, $block->color->b); $x = false; foreach ($block->stitches as $stitch) { if ($x !== false) { imageline($im, ($x - $embroidery->min->x) * $scale_post, ($y - $embroidery->min->y) * $scale_post, ($stitch->x - $embroidery->min->x) * $scale_post, ($stitch->y - $embroidery->min->y) * $scale_post, $color); } $x = $stitch->x; $y = $stitch->y; } } // Scale finished image if ($scale_pre) { $im2 = imagecreatetruecolor($embroidery->imageWidth * $scale_post * $scale_pre, $embroidery->imageHeight * $scale_post * $scale_pre); imagesavealpha($im2, true); imagealphablending($im2, false); imagecopyresized($im2, $im, 0, 0, 0, 0, $embroidery->imageWidth * $scale_post * $scale_pre, $embroidery->imageHeight * $scale_post * $scale_pre, $embroidery->imageWidth * $scale_post, $embroidery->imageHeight * $scale_post); imagedestroy($im); $im = $im2; } return $im; }
/** * Generate a gradient image * * @param int $width The new image width in pixels * @param int $height The new image height in pixels * @param int $start The start of the gradient in pixels * @param Color $src_color The start image color * @param Color $dest_color The end image color * * @return \imagemanipulation\ImageResource */ public static function gradient($width, $height, $start, Color $src_color, Color $dest_color) { Args::int($width, 'width')->required()->min(1); Args::int($height, 'height')->required()->min(1); $res = self::create($width, $height, $src_color); $img = $res->getResource(); $srcA = $src_color->getAlpha(); $srcR = $src_color->getRed(); $srcG = $src_color->getGreen(); $srcB = $src_color->getBlue(); $destA = $dest_color->getAlpha(); $destR = $dest_color->getRed(); $destG = $dest_color->getGreen(); $destB = $dest_color->getBlue(); $incA = ($destA - $srcA) / ($width - $start); $incR = ($destR - $srcR) / ($width - $start); $incG = ($destG - $srcG) / ($width - $start); $incB = ($destB - $srcB) / ($width - $start); for ($i = $start; $i < $width; $i++) { $srcA += $incA; $srcB += $incB; $srcG += $incG; $srcR += $incR; imagefilledrectangle($img, $i, 0, $i, $height, imagecolorallocatealpha($img, $srcR, $srcG, $srcB, $srcA)); } return $res; }
public function save() { $maxHeight = 0; $width = 0; foreach ($this->_segmentsArray as $segment) { $maxHeight = max($maxHeight, $segment->height); $width += $segment->width; } // create our canvas $img = imagecreatetruecolor($width, $maxHeight); $background = imagecolorallocatealpha($img, 255, 255, 255, 127); imagefill($img, 0, 0, $background); imagealphablending($img, false); imagesavealpha($img, true); // start placing our images on a single x axis $xPos = 0; foreach ($this->_segmentsArray as $segment) { $tmp = imagecreatefromjpeg($segment->pathToImage); imagecopy($img, $tmp, $xPos, 0, 0, 0, $segment->width, $segment->height); $xPos += $segment->width; imagedestroy($tmp); } // create our final output image. imagepng($img, $this->_saveToPath); }
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; }