static function load($filename) { $info = getimagesize($filename); list($width, $height) = $info; if (!$width || !$height) { return null; } $image = null; switch ($info['mime']) { case 'image/gif': $image = imagecreatefromgif($filename); break; case 'image/jpeg': $image = imagecreatefromjpeg($filename); break; case 'image/png': $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $white); $png = imagecreatefrompng($filename); imagealphablending($png, true); imagesavealpha($png, true); imagecopy($image, $png, 0, 0, 0, 0, $width, $height); imagedestroy($png); break; } if ($image) { return new image($image, $width, $height); } else { return null; } }
/** * @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(); }
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 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; }
/** * 2015-12-08 * Точка отсчёта системы координат [0, 0] — это самая левая верхняя точка холста. * Далее кординаты увеличиваются вниз и вправо. * @override * @see \Df\GoogleFont\Fonts\Png::draw() * @used-by \Df\GoogleFont\Fonts\Png::image() * @param resource $image * @return void */ protected function draw($image) { $r = imagefill($image, 0, 0, $this->colorAllocateAlpha($image, $this->bgColor())); df_assert($r); $r = imagettftext($image, $this->fontSize(), 0, $this->marginLeft(), $this->height() - abs($this->contentBottomY()), $this->colorAllocateAlpha($image, $this->fontColor()), $this->ttfPath(), $this->text()); df_assert($r); }
static function codeimage() { $image = imagecreatetruecolor(100, 30); $bgcolor = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $bgcolor); $code = ''; for ($i = 0; $i < 4; $i++) { $fontsize = 6; $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120)); $data = "abcdefghjkmnpqrstuvwxy3456789"; $fontcontent = substr($data, rand(1, strlen($data) - 1), 1); $code .= $fontcontent; $x = $i * 100 / 4 + rand(5, 10); $y = rand(5, 10); imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } for ($i = 0; $i < 200; $i++) { $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200)); imagesetpixel($image, rand(1, 99), rand(1, 29), $pointcolor); } for ($i = 0; $i < 2; $i++) { $linecolor = imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220)); imageline($image, rand(1, 99), rand(1, 29), rand(1, 99), rand(1, 29), $linecolor); } $return['code'] = $code; $return['image'] = $image; return $return; // header('content-type:image/png'); // imagepng($image); }
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;
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4) { $h = count($frame); $w = strlen($frame[0]); $imgW = $w + 2 * $outerFrame; $imgH = $h + 2 * $outerFrame; $base_image = imagecreatetruecolor($imgW, $imgH); $col[0] = ImageColorAllocate($base_image, 255, 0, 255); $col[1] = ImageColorAllocate($base_image, 0, 0, 0); imagecolortransparent($base_image, $col[0]); imagealphablending($base_image, true); imagesavealpha($base_image, true); // imagefill($base_image, 0, 0, $col[0]); imagefill($base_image, 0, 0, 0x7fff0000); for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { if ($frame[$y][$x] == '1') { ImageSetPixel($base_image, $x + $outerFrame, $y + $outerFrame, $col[1]); } } } $target_image = ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint); ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH); ImageDestroy($base_image); return $target_image; }
function getAuthImage($text) { $this->setpin($text); $im_x = 160; $im_y = 40; $im = imagecreatetruecolor($im_x, $im_y); $text_c = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); $tmpC0 = mt_rand(100, 255); $tmpC1 = mt_rand(100, 255); $tmpC2 = mt_rand(100, 255); $buttum_c = ImageColorAllocate($im, $tmpC0, $tmpC1, $tmpC2); imagefill($im, 16, 13, $buttum_c); $font = PATH_SYS_PUBLIC . 'font-awesome/fonts/verdana.ttf'; for ($i = 0; $i < strlen($text); $i++) { $tmp = substr($text, $i, 1); $array = array(-1, 1); $p = array_rand($array); $an = $array[$p] * mt_rand(1, 10); //角度 $size = 28; imagettftext($im, $size, $an, 15 + $i * $size, 35, $text_c, $font, $tmp); } $distortion_im = imagecreatetruecolor($im_x, $im_y); imagefill($distortion_im, 16, 13, $buttum_c); for ($i = 0; $i < $im_x; $i++) { for ($j = 0; $j < $im_y; $j++) { $rgb = imagecolorat($im, $i, $j); if ((int) ($i + 20 + sin($j / $im_y * 2 * M_PI) * 10) <= imagesx($distortion_im) && (int) ($i + 20 + sin($j / $im_y * 2 * M_PI) * 10) >= 0) { imagesetpixel($distortion_im, (int) ($i + 10 + sin($j / $im_y * 2 * M_PI - M_PI * 0.1) * 4), $j, $rgb); } } } //加入干扰象素; $count = 160; //干扰像素的数量 for ($i = 0; $i < $count; $i++) { $randcolor = ImageColorallocate($distortion_im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagesetpixel($distortion_im, mt_rand() % $im_x, mt_rand() % $im_y, $randcolor); } $rand = mt_rand(5, 30); $rand1 = mt_rand(15, 25); $rand2 = mt_rand(5, 10); for ($yy = $rand; $yy <= +$rand + 2; $yy++) { for ($px = -80; $px <= 80; $px = $px + 0.1) { $x = $px / $rand1; if ($x != 0) { $y = sin($x); } $py = $y * $rand2; imagesetpixel($distortion_im, $px + 80, $py + $yy, $text_c); } } //设置文件头; Header("Content-type: image/JPEG"); //以PNG格式将图像输出到浏览器或文件; ImagePNG($distortion_im); //销毁一图像,释放与image关联的内存; ImageDestroy($distortion_im); ImageDestroy($im); }
function create_image() { // hash: là mật mã $md5_hash = md5(rand(0, 999)); // rand phat sinh 1 số từ 0 - 999 // md5 sẽ mã hóa 1 số thành 1 số khác đến 32 ký tự $security_code = substr($md5_hash, 15, 4); // substr lấy 1 chuỗi con trong md5_hash, lấy từ ký tự thứ 15 và lấy 5 ký tự $_SESSION['security_code'] = $security_code; $width = 100; //Khai báo kích thước captcha $height = 30; $image = imagecreate($width, $height); $while = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); $red = imagecolorallocate($image, 255, 255, 0); imagefill($image, 0, 0, $black); imagestring($image, 5, 30, 6, $security_code, $while); // 5 font size // 30 khoảng cách bên trái //khoảng cách từ trên xuống $captcha = imagecreatefrompng('captcha1.png'); $font = 'arial.ttf'; imagettftext($captcha, 23, 6, 20, 38, $black, $font, $security_code); header("Content-Type:image/jpeg"); // chuyển trang web thành dạng hình jpg imagejpeg($captcha); //Tạo hình imagedestroy($captcha); // Hủy hình gốc vì đã tạo thành trang }
public function __construct($type = 1, $size = 6) { Oraculum_Request::init_sess(); if (is_null(Oraculum_Request::sess("captcha"))) { if ($type == 3) { $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $max = 61; } elseif ($type == 2) { $letters = 'abcdefghijklmnopqrstuvwxyz0123456789'; $max = 35; } else { $letters = '0123456789'; $max = 9; } $string = NULL; for ($i = 0; $i < $size; $i++) { $string .= $letters[mt_rand(0, $max)]; } Oraculum_Request::setsess("captcha", $string); } else { $string = Oraculum_Request::sess("captcha"); } $img = imagecreate(10 * $size, 25); $backcolor = imagecolorallocate($img, 255, 255, 255); $textcolor = imagecolorallocate($img, 00, 00, 00); imagefill($img, 0, 0, $backcolor); imagestring($img, 10, 0, 5, $string, $textcolor); header("Content-type: image/jpeg"); imagejpeg($img); }
protected function _resize_padded($canvas_w, $canvas_h, $canvas_color, $width, $height, $orig_w, $orig_h, $origin_x, $origin_y) { $src_x = $src_y = 0; $src_w = $orig_w; $src_h = $orig_h; $cmp_x = $orig_w / $width; $cmp_y = $orig_h / $height; // calculate x or y coordinate and width or height of source if ($cmp_x > $cmp_y) { $src_w = round($orig_w / $cmp_x * $cmp_y); $src_x = round(($orig_w - $orig_w / $cmp_x * $cmp_y) / 2); } else { if ($cmp_y > $cmp_x) { $src_h = round($orig_h / $cmp_y * $cmp_x); $src_y = round(($orig_h - $orig_h / $cmp_y * $cmp_x) / 2); } } $resized = wp_imagecreatetruecolor($canvas_w, $canvas_h); if ($canvas_color === 'transparent') { $color = imagecolorallocatealpha($resized, 255, 255, 255, 127); } else { $rgb = cnColor::rgb2hex2rgb($canvas_color); $color = imagecolorallocatealpha($resized, $rgb['red'], $rgb['green'], $rgb['blue'], 0); } // Fill the background of the new image with allocated color. imagefill($resized, 0, 0, $color); // Restore transparency. imagesavealpha($resized, TRUE); imagecopyresampled($resized, $this->image, $origin_x, $origin_y, $src_x, $src_y, $width, $height, $src_w, $src_h); if (is_resource($resized)) { $this->update_size($width, $height); return $resized; } return new WP_Error('image_resize_error', __('Image resize failed.', 'connections'), $this->file); }
/** * 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; }
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); }
private function createBg() { $this->img = imagecreatetruecolor($this->width, $this->height); $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255)); //imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color); imagefill($this->img, 0, 0, $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; }
public function index() { ob_clean(); $image_handle = imagecreatetruecolor(150, 60); $white = imagecolorallocate($image_handle, 255, 255, 255); $rndm = imagecolorallocate($image_handle, rand(64, 192), rand(64, 192), rand(64, 192)); imagefill($image_handle, 0, 0, $white); $fontName = PUBLICPATH . "/fonts/elephant.ttf"; $myX = 15; $myY = 30; $angle = 0; for ($x = 0; $x <= 100; $x++) { $myX = rand(1, 148); $myY = rand(1, 58); imageline($image_handle, $myX, $myY, $myX + rand(-5, 5), $myY + rand(-5, 5), $rndm); } $myCryptBase = tep_create_random_value(50, 'digits'); $secure_image_hash_string = ""; for ($x = 0; $x <= 4; $x++) { $dark = imagecolorallocate($image_handle, rand(5, 128), rand(5, 128), rand(5, 128)); $capChar = substr($myCryptBase, rand(1, 35), 1); $secure_image_hash_string .= $capChar; $fs = rand(20, 26); $myX = 15 + ($x * 28 + rand(-5, 5)); $myY = rand($fs + 2, 55); $angle = rand(-30, 30); ImageTTFText($image_handle, $fs, $angle, $myX, $myY, $dark, $fontName, $capChar); } $this->session->set_userdata('secure_image_hash_string', $secure_image_hash_string); header("Content-type: image/jpeg"); imagejpeg($image_handle, "", 95); imagedestroy($image_handle); die; }
private function generateImage() { // prepare image $this->generatedImage = imagecreatetruecolor($this->getPixelRatio() * 5, $this->getPixelRatio() * 5); $rgbBackgroundColor = $this->getBackgroundColor(); if (null === $rgbBackgroundColor) { $background = imagecolorallocate($this->generatedImage, 0, 0, 0); imagecolortransparent($this->generatedImage, $background); } else { $background = imagecolorallocate($this->generatedImage, $rgbBackgroundColor[0], $rgbBackgroundColor[1], $rgbBackgroundColor[2]); imagefill($this->generatedImage, 0, 0, $background); } // prepage color $rgbColor = $this->getColor(); $gdColor = imagecolorallocate($this->generatedImage, $rgbColor[0], $rgbColor[1], $rgbColor[2]); // draw content foreach ($this->getArrayOfSquare() as $lineKey => $lineValue) { foreach ($lineValue as $colKey => $colValue) { if (true === $colValue) { imagefilledrectangle($this->generatedImage, $colKey * $this->getPixelRatio(), $lineKey * $this->getPixelRatio(), ($colKey + 1) * $this->getPixelRatio(), ($lineKey + 1) * $this->getPixelRatio(), $gdColor); } } } return $this; }
/** * Processes incoming text */ public function action_index() { $this->request->headers['Content-type'] = 'image/png'; // Grab text and styles $text = arr::get($_GET, 'text'); $styles = $_GET; $hover = FALSE; try { // Create image $img = new PNGText($text, $styles); foreach ($styles as $key => $value) { if (substr($key, 0, 6) == 'hover-') { // Grab hover associated styles and override existing styles $hover = TRUE; $styles[substr($key, 6)] = $value; } } if ($hover) { // Create new hover image and stack it $hover = new PNGText($text, $styles); $img->stack($hover); } echo $img->draw(); } catch (Exception $e) { if (Kohana::config('pngtext.debug')) { // Dump error message in an image form $img = imagecreatetruecolor(strlen($e->getMessage()) * 6, 16); imagefill($img, 0, 0, imagecolorallocate($img, 255, 255, 255)); imagestring($img, 2, 0, 0, $e->getMessage(), imagecolorallocate($img, 0, 0, 0)); echo imagepng($img); } } }
function create_image() { // *** Generate a passcode using md5 // (it will be all lowercase hex letters and numbers *** $md5 = md5(rand(0, 9999)); $pass = substr($md5, 10, 5); // *** Set the session cookie so we know what the passcode is *** $_SESSION["pass"] = $pass; // *** Create the image resource *** $image = ImageCreatetruecolor(100, 20); // *** We are making two colors, white and black *** $clr_white = ImageColorAllocate($image, 255, 255, 255); $clr_black = ImageColorAllocate($image, 0, 0, 0); // *** Make the background black *** imagefill($image, 0, 0, $clr_black); // *** Set the image height and width *** imagefontheight(15); imagefontwidth(15); // *** Add the passcode in white to the image *** imagestring($image, 5, 30, 3, $pass, $clr_white); // *** Throw in some lines to trick those cheeky bots! *** imageline($image, 5, 1, 50, 20, $clr_white); imageline($image, 60, 1, 96, 20, $clr_white); // *** Return the newly created image in jpeg format *** return imagejpeg($image); // *** Just in case... *** imagedestroy($image); }
protected function createMask(sfImage $image, $w, $h) { // Create a mask png image of the area you want in the circle/ellipse (a 'magicpink' image with a black shape on it, with black set to the colour of alpha transparency) - $mask $mask = $image->getAdapter()->getTransparentImage($w, $h); // Set the masking colours if (false === $this->getColor() || 'image/png' == $image->getMIMEType()) { $mask_black = imagecolorallocate($mask, 0, 0, 0); } else { $mask_black = $image->getAdapter()->getColorByHex($mask, $this->getColor()); } // Cannot use white as transparent mask if color is set to white if ($this->getColor() === '#FFFFFF' || $this->getColor() === false) { $mask_transparent = imagecolorallocate($mask, 255, 0, 0); } else { $mask_color = imagecolorsforindex($mask, imagecolorat($image->getAdapter()->getHolder(), 0, 0)); $mask_transparent = imagecolorallocate($mask, $mask_color['red'], $mask_color['green'], $mask_color['blue']); } imagecolortransparent($mask, $mask_transparent); imagefill($mask, 0, 0, $mask_black); // Draw the rounded rectangle for the mask $this->imagefillroundedrect($mask, 0, 0, $w, $h, $this->getRadius(), $mask_transparent); $mask_image = clone $image; $mask_image->getAdapter()->setHolder($mask); return $mask_image; }
/** * 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); }
function img_resize($src, $dest, $width, $height, $rgb = 0xffffff, $quality = 100) { if (!file_exists($src)) { return false; } $size = getimagesize($src); if ($size === false) { return false; } $format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1)); $icfunc = 'imagecreatefrom' . $format; if (!function_exists($icfunc)) { return false; } $x_ratio = $width / $size[0]; $y_ratio = $height / $size[1]; if ($height == 0) { $y_ratio = $x_ratio; $height = $y_ratio * $size[1]; } elseif ($width == 0) { $x_ratio = $y_ratio; $width = $x_ratio * $size[0]; } $ratio = min($x_ratio, $y_ratio); $use_x_ratio = $x_ratio == $ratio; $new_width = $use_x_ratio ? $width : floor($size[0] * $ratio); $new_height = !$use_x_ratio ? $height : floor($size[1] * $ratio); $new_left = $use_x_ratio ? 0 : floor(($width - $new_width) / 2); $new_top = !$use_x_ratio ? 0 : floor(($height - $new_height) / 2); // если не нужно увеличивать маленькую картинку до указанного размера if ($size[0] < $new_width && $size[1] < $new_height) { $width = $new_width = $size[0]; $height = $new_height = $size[1]; } $isrc = $icfunc($src); $idest = imagecreatetruecolor($width, $height); imagefill($idest, 0, 0, $rgb); imagecopyresampled($idest, $isrc, $new_left, $new_top, 0, 0, $new_width, $new_height, $size[0], $size[1]); $i = strrpos($dest, '.'); if (!$i) { return ''; } $l = strlen($dest) - $i; $ext = substr($dest, $i + 1, $l); switch ($ext) { case 'jpeg': case 'jpg': imagejpeg($idest, $dest, $quality); break; case 'gif': imagegif($idest, $dest); break; case 'png': imagepng($idest, $dest); break; } imagedestroy($isrc); imagedestroy($idest); return true; }
public static function getCode($width = 70, $height = 24, $len = 4) { self::init(); header('content-type:image/png'); $checkWord = ''; $checkChar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ1234567890'; for ($num = 0; $num < $len; $num++) { $char = rand(0, strlen($checkChar) - 1); $checkWord .= $checkChar[$char]; } $_SESSION['code'] = strtolower($checkWord); $image = imagecreate($width, $height); $font = FONTS_PATH . 'ariblk.ttf'; $red = imagecolorallocate($image, 0xf3, 0x61, 0x61); $blue = imagecolorallocate($image, 0x53, 0x68, 0xbd); $green = imagecolorallocate($image, 0x6b, 0xc1, 0x46); $colors = array($red, $blue, $green); $gray = imagecolorallocate($image, 0xf5, 0xf5, 0xf5); imagefill($image, 0, 0, $gray); imageline($image, rand(0, 5), rand(6, 18), rand(65, 70), rand(6, 18), $colors[rand(0, 2)]); for ($num = 0; $num < $len; $num++) { imagettftext($image, rand(12, 16), (rand(0, 60) + 330) % 360, 5 + 15 * $num + rand(0, 4), 18 + rand(0, 4), $colors[rand(0, 2)], $font, $checkWord[$num]); } imagepng($image); imagedestroy($image); }
public static function create() { $image = imagecreatetruecolor(100, 40) or die('cannot create canvas.<br>'); $red = imagecolorallocate($image, 255, 0, 0); $green = imagecolorallocate($image, 0, 255, 0); $blue = imagecolorallocate($image, 0, 0, 255); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); imagefill($image, 0, 0, $white); imagerectangle($image, 1, 1, 99, 39, $black); $color = [$red, $green, $blue]; for ($i = 1; $i <= 100; $i++) { imagesetpixel($image, mt_rand(2, 98), mt_rand(2, 38), $color[mt_rand(0, 2)]); } $source = "abcdefghigklmnopqrstuvwxyz0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ"; $first = $source[mt_rand(0, 61)]; $second = $source[mt_rand(0, 61)]; $third = $source[mt_rand(0, 61)]; $fourth = $source[mt_rand(0, 61)]; $_SESSION['captcha'] = $first . $second . $third . $fourth; $font = dirname(dirname(__DIR__)) . '/../public/assets/fonts/SpicyRice.ttf'; imagettftext($image, 20, mt_rand(-20, 20), 10, 30, $blue, $font, $first); imagettftext($image, 20, mt_rand(-20, 20), 30, 30, $blue, $font, $second); imagettftext($image, 20, mt_rand(-20, 20), 50, 30, $blue, $font, $third); imagettftext($image, 20, mt_rand(-20, 20), 70, 30, $blue, $font, $fourth); return imagejpeg($image); }
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); } } } } }
function get_default_image() { $image = imagecreate(50, 50); $color = imagecolorallocate($image, 250, 50, 50); imagefill($image, 0, 0, $color); return $image; }
/** * 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; }
/** * Generate and display the CAPTCHA image. * @param $captcha object Captcha */ function generateImage(&$captcha) { $width = $this->getWidth(); $height = $this->getHeight(); $length = String::strlen($captcha->getValue()); $value = $captcha->getValue(); $image = imagecreatetruecolor($width, $height); $fg = imagecolorallocate($image, rand(128, 255), rand(128, 255), rand(128, 255)); $bg = imagecolorallocate($image, rand(0, 64), rand(0, 64), rand(0, 64)); imagefill($image, $width / 2, $height / 2, $bg); $xStart = rand($width / 12, $width / 3); $xEnd = rand($width * 2 / 3, $width * 11 / 12); for ($i = 0; $i < $length; $i++) { imagefttext($image, rand(20, 34), rand(-15, 15), $xStart + ($xEnd - $xStart) * $i / $length + rand(-5, 5), rand(40, 60), $fg, Config::getVar('captcha', 'font_location'), String::substr($value, $i, 1)); } // Add some noise to the image. for ($i = 0; $i < 20; $i++) { $color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); for ($j = 0; $j < 20; $j++) { imagesetpixel($image, rand(0, $this->getWidth()), rand(0, $this->getHeight()), $color); } } header('Content-type: ' . $this->getMimeType()); imagepng($image); imagedestroy($image); }
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4) { $h = count($frame); $w = strlen($frame[0]); $imgW = $w + 2 * $outerFrame; $imgH = $h + 2 * $outerFrame; $base_image = ImageCreate($imgW, $imgH); $col[0] = ImageColorAllocate($base_image, 255, 255, 255); $col[1] = ImageColorAllocate($base_image, 0, 0, 0); imagefill($base_image, 0, 0, $col[0]); for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { if ($frame[$y][$x] == '1') { ImageSetPixel($base_image, $x + $outerFrame, $y + $outerFrame, $col[1]); } } } //$target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint); //ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH); //Get Desired size of image. //Insert the following code to generate desired size QR Code //Example:----- (Note: this should be written in the file where we are going to generate the QR Code) //------define('IMAGE_WIDTH',160); //------define('IMAGE_HEIGHT',160); //------QRcode::png(); $targetW = defined('IMAGE_WIDTH') ? IMAGE_WIDTH : $imgW * $pixelPerPoint; $targetH = defined('IMAGE_HEIGHT') ? IMAGE_HEIGHT : $imgH * $pixelPerPoint; $target_image = ImageCreate($targetW, $targetH); ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $targetW, $targetH, $imgW, $imgH); //Transparent Background QR code ImageColorTransparent($target_image, $col[0]); ImageDestroy($base_image); return $target_image; }