private function getColor($hex) { if (strlen($hex) == 3) { $r = hexdec(substr($hex, 0, 1) . substr($hex, 0, 1)); $g = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1)); $b = hexdec(substr($hex, 2, 1) . substr($hex, 2, 1)); } else { $r = hexdec(substr($hex, 0, 2)); $g = hexdec(substr($hex, 2, 2)); $b = hexdec(substr($hex, 4, 2)); } return Image::rgb($r, $g, $b); }
/** * @return BaseForm */ public function build() { $this->addGroup('Registration'); $this->addText('username', 'Name:')->setRequired('Please enter your username.'); $this->addText('email', 'Email:')->setRequired('Please enter your email.')->addRule(Form::EMAIL, 'Check out email please'); $this->addPassword('password', 'Password:'******'Please enter your password.')->addRule(Form::PATTERN, 'Password must have more than 8 numerals and consists of numbers 0-9, lowercase or uppercase letters a-z or A-Z, underscore', '([0-9a-zA-Z_]){8}'); $this->addPassword('passwordRe', 'Repeat password:'******'Please enter your password once again.')->addRule(Form::EQUAL, 'Password missmatch', $this['password']); $this->addCheckbox('remember', 'Keep me signed in after registration'); /* reCaptcha : you should choose certen pictures $this->addReCaptcha('captcha') ->addRule(ReCaptchaControl\ReCaptchaControl::VALID, 'Prokažte prosím svou nerobotičnost.');*/ $this->addCaptcha('captcha')->addRule(Form::FILLED, "Rewrite text from image.")->addRule($this["captcha"]->getValidator(), 'Try it again.')->setLength(5)->setTextMargin(5)->setTextColor(Image::rgb(0, 0, 0))->setBackgroundColor(Image::rgb(214, 235, 249))->setImageHeight(50)->setImageWidth(0)->setExpire(10)->setFilterSmooth(false)->setFilterContrast(false)->useNumbers(true); // bool or void $this->addSubmit('submit', 'Sign in'); return $this; }
/** * @param string $url * @param int $width * @param int $height * @param int $crop * @return string * @register fitThumb * @todo refactoring fit/thumb helpers, move common parts of code to separate methods */ public function fitThumbImage($url, $width, $height, $crop = NULL) { $origName = substr($url, strrpos($url, '/') + 1); $origPath = ($this->domainDir ? $this->domainDir : $this->cacheDir) . $url; if (!file_exists($origPath)) { return $url; } $a = explode('.', $origPath); $ext = strtolower(array_pop($a)); $thumbName = $this->getThumbName($origName, $width, $height, filemtime($origPath), $crop); $origDirectory = explode('/', $url); array_pop($origDirectory); $origDirectory = implode('/', $origDirectory); $name = md5(($this->domainDirUrl ? $this->domainDirUrl : $this->cacheDirUrl) . '/' . $origDirectory . '/fitThumbs/' . $thumbName) . ".{$ext}"; $thumbPath = ($this->domainDir ? $this->domainDir : $this->cacheDir) . $origDirectory . '/fitThumbs/' . $name; $thumbUrl = ($this->domainDirUrl ? $this->domainDirUrl : $this->cacheDirUrl) . $origDirectory . '/fitThumbs/' . $name; FileSystem::createDir(($this->domainDir ? $this->domainDir : $this->cacheDir) . $origDirectory . '/fitThumbs/'); // thumb already exits if (is_file($thumbPath)) { return $thumbUrl; } try { if (class_exists('Imagick')) { $canvas = new \Imagick(); $canvas->newImage($width, $height, "rgba(0, 0, 0, 0)"); $image = new Imagick($origPath); $origWidth = $image->getImageWidth(); $origHeight = $image->getImageHeight(); if ($origWidth > $width || $origHeight > $height) { if ($origHeight > $height) { // use orig height, crop width $image->resizeImage(0, $height, Imagick::FILTER_LANCZOS, 1); } else { // use orig width, crop height $image->resizeImage($width, 0, Imagick::FILTER_LANCZOS, 1); } switch ($crop) { case 'top': $x = (int) (($image->getImageWidth() - $width) / 2); $y = 0; break; case 'bottom': $x = (int) (($image->getImageWidth() - $width) / 2); $y = (int) ($image->getImageHeight() - $height); break; case 'left': $x = 0; $y = (int) (($image->getImageHeight() - $height) / 2); break; case 'right': $x = (int) ($image->getImageWidth() - $width); $y = (int) (($image->getImageHeight() - $height) / 2); break; case 'center': default: $x = (int) (($image->getImageWidth() - $width) / 2); $y = (int) (($image->getImageHeight() - $height) / 2); } $image->cropImage($width, $height, $x, $y); } $left = $width / 2 - $image->getImageWidth() / 2; $top = $height / 2 - $image->getImageHeight() / 2; $canvas->compositeImage($image, Imagick::COMPOSITE_ADD, $left, $top); $canvas->writeImage($thumbPath); $image->destroy(); $canvas->destroy(); } else { $canvas = Image::fromBlank($width, $height, Image::rgb(0, 0, 0, 127)); $image = Image::fromFile($origPath); $origWidth = $image->getWidth(); $origHeight = $image->getHeight(); if ($origWidth > $width || $origHeight > $height) { // zachovani pruhlednosti u PNG $image->alphaBlending(FALSE); $image->saveAlpha(TRUE); if ($origHeight > $height) { // use orig height, crop width $image->resize(NULL, $height); } else { // use orig width, crop height $image->resize($width, NULL); } switch ($crop) { case 'top': $x = (int) (($image->getWidth() - $width) / 2); $y = 0; break; case 'bottom': $x = (int) (($image->getWidth() - $width) / 2); $y = (int) ($image->getHeight() - $height); break; case 'left': $x = 0; $y = (int) (($image->getHeight() - $height) / 2); break; case 'right': $x = (int) ($image->getWidth() - $width); $y = (int) (($image->getHeight() - $height) / 2); break; case 'center': default: $x = (int) (($image->getWidth() - $width) / 2); $y = (int) (($image->getHeight() - $height) / 2); } $image->crop($x, $y, $width, $height); if ($ext !== 'png') { $image->sharpen(); } } $left = $width / 2 - $image->getWidth() / 2; $top = $height / 2 - $image->getHeight() / 2; $canvas->place($image, $left, $top); $canvas->save($thumbPath); } return $thumbUrl; } catch (\Exception $e) { return ($this->domainDirUrl ? $this->domainDirUrl : $this->cacheDirUrl) . $url; } }
/** * Sets background color * * @param array $rgb [red 0-255, green 0-255, blue 0-255] * @throws \Nette\InvalidArgumentException * @return self */ public function setBackgroundColor(array $rgb) { if (!isset($rgb['red']) || !isset($rgb['green']) || !isset($rgb['blue'])) { throw new InvalidArgumentException('BackgroundColor must be valid RGB array, see Nette\\Utils\\Image::rgb().'); } $this->backgroundColor = Image::rgb($rgb['red'], $rgb['green'], $rgb['blue']); return $this; }
/** * @param array red 0-255, green 0-255, blue 0-255 * @return CaptchaControl provides a fluent interface * @throws \Nette\InvalidArgumentException */ public function setBackgroundColor($rgb) { if (!isset($rgb["red"]) || !isset($rgb["green"]) || !isset($rgb["blue"])) { throw new \Nette\InvalidArgumentException("BackgroundColor must be valid rgb array, see Nette\\Utils\\Image::rgb()"); } $this->backgroundColor = Image::rgb($rgb["red"], $rgb["green"], $rgb["blue"]); return $this; }