protected function createWeb() { Helpers::purge($this->wwwDir); Helpers::purge($this->wwwDir . '/images'); $image = Image::fromBlank(500, 500); $image->save($this->wwwDir . '/images/image.jpg'); }
public function getCaptchaImage($color, $fontSize) { $string = $this->sessSect->question; $fontSize = $fontSize ? $fontSize : 14; $fontFile = __DIR__ . '/OpenSans-Semibold.ttf'; $typeSpace = imagettfbbox($fontSize, 0, $fontFile, $string); $imageWidth = abs($typeSpace[4] - $typeSpace[0]) + 10; $imageHeight = abs($typeSpace[5] - $typeSpace[1]) + 10; if ($color) { $background = $this->getColor($color); } else { $background = Image::rgb(255, 255, 255); } $image = Image::fromBlank($imageWidth, $imageHeight, $background); $image->ttfText($fontSize, 0, 0, 20, Image::rgb(0, 0, 0), $fontFile, $string); $image->send(Image::PNG); }
/** * @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; } }
/** * Draws captcha image * * @return string */ protected function getImageData() { $word = $this->getWord(); $font = $this->getFontFile(); $size = $this->getFontSize(); $textColor = $this->getTextColor(); $bgColor = $this->getBackgroundColor(); $box = $this->getDimensions(); $width = $this->getImageWidth(); $height = $this->getImageHeight(); $first = Image::fromBlank($width, $height, $bgColor); $second = Image::fromBlank($width, $height, $bgColor); $x = ($width - $box['width']) / 2; $y = ($height + $box['height']) / 2; $first->fttext($size, 0, $x, $y, $textColor, $font, $word); $frequency = $this->getRandom(0.05, 0.1); $amplitude = $this->getRandom(2, 4); $phase = $this->getRandom(0, 6); for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $sy = round($y + sin($x * $frequency + $phase) * $amplitude); $sx = round($x + sin($y * $frequency + $phase) * $amplitude); $color = $first->colorat($x, $y); $second->setpixel($sx, $sy, $color); } } $first->destroy(); if (defined('IMG_FILTER_SMOOTH') === TRUE) { $second->filter(IMG_FILTER_SMOOTH, $this->getFilterSmooth()); } if (defined('IMG_FILTER_CONTRAST') === TRUE) { $second->filter(IMG_FILTER_CONTRAST, $this->getFilterContrast()); } ob_start(); imagepng($second->getImageResource()); $contents = ob_get_contents(); ob_end_clean(); return $contents; }