protected function render($code, $x, $y, $method) { $file = $this->items->where('code', $code)->fetch(); $this->sourcepath = FILESTORAGE_DIR . '/' . $file->filepath; $this->cachepath = TEMP_DIR . '/imagecache/' . $file->id; $this->imagepath = $this->cachepath . '/' . $method . "-" . $x . "x" . $y . '.' . $file->filename; $expires = 60 * 60 * 24 * 31; $etag = '"' . md5($this->imagepath) . '"'; if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) { header('HTTP/1.1 304 Not Modified'); header('Content-Length: 0'); exit; } header('ETag: ' . $etag); header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT'); header("Pragma: public"); // required header("Cache-Control: maxage=" . $expires); header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT'); header("Cache-Control: cache"); header("Cache-Control: private", false); // required for certain browsers header("Content-Transfer-Encoding: binary"); if (!file_exists($this->imagepath) || $file->changed > $file->created) { try { if (file_exists($this->sourcepath)) { $image = Image::fromFile($this->sourcepath); } else { $image = Image::fromBlank($x, $y, array('red' => 240, 'green' => 240, 'blue' => 240)); } @$image->resize($x, $y, $method); $image->sharpen(); if ($method == 5) { $image->crop(0, 0, $x, $y); } if (!file_exists($this->cachepath)) { mkdir($this->cachepath, 0777, true); chmod($this->cachepath, 0777); } if (file_exists($this->sourcepath)) { $image->save($this->imagepath, 66, Image::JPEG); } else { $image->string(2, 5, $y / 2 - 7, 'No image found', 1); } $image->send(); } catch (Exception $e) { } } else { header('Content-Disposition: inline; filename="' . $this->imagepath . '"'); header("Content-Type: image/jpeg"); header("Content-Length: " . @filesize($this->imagepath)); readfile($this->imagepath); } }
/** * @param array $report See ScreenshotMaker::$report */ public function generate(array $report) { foreach (Finder::findFiles('*.png')->in($this->outDir) as $image) { /** @var \SplFileInfo $image */ $blank = Image::fromBlank(120, 120, array('green' => 255, 'blue' => 255, 'red' => 255)); $blank->place(Image::fromFile($image->getPathname())->resize(120, 120)); $blank->save(dirname($image->getPathname()) . '/' . $image->getBasename('.png') . '.thumb.png'); } $tpl = new Nette\Templating\FileTemplate(__DIR__ . '/../templates/report.latte'); $tpl->registerFilter(new Nette\Latte\Engine()); $tpl->registerHelperLoader('Nette\\Templating\\Helpers::loader'); $tpl->registerHelper('resultName', function ($result) { static $names = array(StepEvent::PASSED => 'passed', StepEvent::SKIPPED => 'skipped', StepEvent::PENDING => 'pending', StepEvent::UNDEFINED => 'undefined', StepEvent::FAILED => 'failed'); return isset($names[$result]) ? $names[$result] : '[unknown]'; }); $tpl->registerHelper('resultClass', function ($result) { static $names = array(StepEvent::PASSED => 'passed', StepEvent::SKIPPED => 'skipped', StepEvent::PENDING => 'pending', StepEvent::UNDEFINED => 'undefined', StepEvent::FAILED => 'failed'); return isset($names[$result]) ? $names[$result] : '[unknown]'; }); // render $tpl->report = $report; file_put_contents($this->outDir . '/index.html', $tpl->__toString()); }
public function resizeAndSaveImage($imagePath, $dimensions, $cutToFit = false) { // first check on s3 service if (!$this->awsService->isFile($imagePath)) { return array($imagePath, null, null); } $info = pathinfo($imagePath); $imageDir = $info['dirname'] . '/'; $imageName = basename($imagePath); //////////////////////////////////////// // read dimensions //////////////////////////////////////// $dim = explode('x', $dimensions); $newWidth = isset($dim[0]) ? $dim[0] : null; $newHeight = isset($dim[1]) ? $dim[1] : null; try { $resizedImageName = Strings::webalize(basename($imagePath, '.' . $info['extension'])) . '-' . $newWidth . 'x' . $newHeight . '.' . $info['extension']; $resizedImagePath = $imageDir . $resizedImageName; // check if resized image is already on s3, if yes, return, if not, generate and save if ($this->awsService->isFile($resizedImagePath)) { $resizedImagePath = $this->baseUrl . $resizedImagePath; $result = array($resizedImagePath, $newWidth, $newHeight); return $result; } // need to download the original image and convert the sizes $tempImagePath = $this->tempDir . $imageName; // download image from s3 to temporary directory $this->awsService->downloadFile($imagePath, $tempImagePath); // load and resize image $image = Image::fromFile($tempImagePath); if ($cutToFit) { if ($image->height > $image->width) { $image->resize($newWidth, $newHeight, Image::FILL); $image->sharpen(); $blank = Image::fromBlank($newWidth, $newHeight, Image::rgb(255, 255, 255)); $blank->place($image, 0, '20%'); $image = $blank; } else { $image->resize($newWidth, $newHeight, Image::FILL | Image::EXACT); $image->sharpen(); } } else { $image->resize((int) $newWidth, (int) $newHeight, Image::SHRINK_ONLY); $image->sharpen(); } // generate new image $image->save($tempImagePath, 85); // 85% quality // upload file to s3 storage $this->awsService->uploadFile($tempImagePath, $resizedImagePath); $result = array($this->baseUrl . $resizedImagePath, $image->width, $image->height); // free memory $image = null; unset($image); // remove temporary file if (file_exists($tempImagePath)) { unlink($tempImagePath); } return $result; } catch (\Exception $e) { //$this->logger->logError($e); return array($this->baseUrl . $imagePath, null, null); } }
/** * Resizes the given image * * @param resource $p_image * The image resource handler * @param int $p_maxWidth * The maximum width of the resized image * @param int $p_maxHeight * The maximum height of the resized image * @param bool $p_keepRatio * If true keep the image ratio * @param int $type * Image type * @return int * Return the new image resource handler. */ public static function ResizeImage($p_image, $p_maxWidth, $p_maxHeight, $p_keepRatio = true, $type = IMAGETYPE_JPEG) { require_once APPLICATION_PATH . '/../library/Nette/Image.php'; $origImageWidth = imagesx($p_image); $origImageHeight = imagesy($p_image); if ($origImageWidth <= 0 || $origImageHeight <= 0) { return new PEAR_Error(getGS("The file uploaded is not an image.")); } $p_maxWidth = is_numeric($p_maxWidth) ? (int) $p_maxWidth : 0; $p_maxHeight = is_numeric($p_maxHeight) ? (int) $p_maxHeight : 0; if ($p_maxWidth <= 0 || $p_maxHeight <= 0) { return new PEAR_Error(getGS("Invalid resize width/height.")); } if ($p_keepRatio) { $ratioOrig = $origImageWidth / $origImageHeight; $ratioNew = $p_maxWidth / $p_maxHeight; if ($ratioNew > $ratioOrig) { $newImageWidth = $p_maxHeight * $ratioOrig; $newImageHeight = $p_maxHeight; } else { $newImageWidth = $p_maxWidth; $newImageHeight = $p_maxWidth / $ratioOrig; } } else { $newImageWidth = $p_maxWidth; $newImageHeight = $p_maxHeight; } $newImage = \Nette\Image::fromBlank($newImageWidth, $newImageHeight); $newImage->alphaBlending(false); $newImage->saveAlpha(true); imagecopyresampled($newImage->getImageResource(), $p_image, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $origImageWidth, $origImageHeight); return $newImage->getImageResource(); }
/** * Nette\Image drawing example. * @author David Grudl */ require __DIR__ . '/../../Nette/loader.php'; use Nette\Image, Nette\Diagnostics\Debugger; Debugger::enable(); $image = Image::fromBlank(300, 300); // white background $image->filledRectangle(0, 0, 299, 299, Image::rgb(255, 255, 255)); // black border $image->rectangle(0, 0, 299, 299, Image::rgb(0, 0, 0)); // three ellipses $image->filledEllipse(100, 75, 150, 150, Image::rgb(255, 255, 0, 75)); $image->filledEllipse(120, 168, 150, 150, Image::rgb(255, 0, 0, 75)); $image->filledEllipse(187, 125, 150, 150, Image::rgb(0, 0, 255, 75)); $image->send();
/** * Draw captcha image and encode to base64 string * @return string */ private function drawImage() { $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')) { $second->filter(IMG_FILTER_SMOOTH, $this->getFilterSmooth()); } if (defined('IMG_FILTER_CONTRAST')) { $second->filter(IMG_FILTER_CONTRAST, $this->getFilterContrast()); } // start buffering ob_start(); imagepng($second->getImageResource()); $contents = ob_get_contents(); ob_end_clean(); return base64_encode($contents); }
private function _createTinyPlaceholderForGallery($galleryId, $fontFile, $fontSize) { $config = $this->getConfig(); $pattern = $config['tinyPlaceholderPattern']; $placeholderFilename = sprintf($pattern, $galleryId); $placeholderFilePath = $this->getTempDir() . '/' . $placeholderFilename; if (!is_file($placeholderFilePath)) { // create placeholder $folder = $this->getFolder($galleryId); $filename = $this->getGalleryIconPath($galleryId); $image = Nette\Image::fromFile($filename); $blank = Nette\Image::fromBlank(350, 100, Nette\Image::rgb(220, 220, 220)); $blank->place($image, 8, 8); $rgb = $blank->rgb(0, 0, 0); $imageResource = $blank->getImageResource(); $color = imagecolorallocatealpha($imageResource, $rgb['red'], $rgb['green'], $rgb['blue'], $rgb['alpha']); $x = 100; $y = 8 + $fontSize; $text = 'Galerie'; imagettftext($imageResource, $fontSize, 0, $x, $y, $color, $fontFile, $text); $x = 100; $y = 8 + 3 * $fontSize; $text = $folder['name']; imagettftext($imageResource, $fontSize, 0, $x, $y, $color, $fontFile, $text); $resultImage = new Nette\Image($imageResource); $resultImage->save($placeholderFilePath); } return $this->getTempPath() . '/' . $placeholderFilename; }