/** * (non-PHPdoc) * @see \imagemanipulation\filter\IImageFilter::applyFilter() */ public function applyFilter(ImageResource $aResource) { $watermarkRes = new ImageImageResource($this->watermark); imagealphablending($aResource->getResource(), true); imagealphablending($watermarkRes->getResource(), true); if ($this->position == 'random') { $this->position = rand(1, 8); } if ($this->watermarkOpacity) { $opacityFilter = new ImageFilterOpacity($this->watermarkOpacity); $opacityFilter->applyFilter($watermarkRes); } $destWidth = $aResource->getX(); $destHeight = $aResource->getY(); $watermarkWidth = $watermarkRes->getX(); $watermarkHeight = $watermarkRes->getY(); switch ($this->position) { case 'top-right': case 'right-top': case 1: $result = imagecopy($aResource->getResource(), $watermarkRes->getResource(), $destWidth - $watermarkWidth, 0, 0, 0, $watermarkWidth, $watermarkHeight); break; case 'top-left': case 2: $result = imagecopy($aResource->getResource(), $watermarkRes->getResource(), 0, 0, 0, 0, $watermarkWidth, $watermarkHeight); break; case 'bottom-right': case 3: $result = imagecopy($aResource->getResource(), $watermarkRes->getResource(), $destWidth - $watermarkWidth, $destHeight - $watermarkHeight, 0, 0, $watermarkWidth, $watermarkHeight); break; case 'bottom-left': case 4: $result = imagecopy($aResource->getResource(), $watermarkRes->getResource(), 0, $destHeight - $watermarkHeight, 0, 0, $watermarkWidth, $watermarkHeight); break; case 'center': case 5: $result = imagecopy($aResource->getResource(), $watermarkRes->getResource(), $destWidth / 2 - $watermarkWidth / 2, $destHeight / 2 - $watermarkHeight / 2, 0, 0, $watermarkWidth, $watermarkHeight); break; case 'top': case 6: $result = imagecopy($aResource->getResource(), $watermarkRes->getResource(), $destWidth / 2 - $watermarkWidth / 2, 0, 0, 0, $watermarkWidth, $watermarkHeight); break; case 'bottom': case 7: $result = imagecopy($aResource->getResource(), $watermarkRes->getResource(), $destWidth / 2 - $watermarkWidth / 2, $destHeight - $watermarkHeight, 0, 0, $watermarkWidth, $watermarkHeight); break; case 'left': case 8: $result = imagecopy($aResource->getResource(), $watermarkRes->getResource(), 0, $destHeight / 2 - $watermarkHeight / 2, 0, 0, $watermarkWidth, $watermarkHeight); break; case 'right': case 9: $result = imagecopy($aResource->getResource(), $watermarkRes->getResource(), $destWidth - $watermarkWidth, $destHeight / 2 - $watermarkHeight / 2, 0, 0, $watermarkWidth, $watermarkHeight); break; imagedestroy($watermarkRes->getResource()); if (!$result) { throw new \Exception('Applying watermark failed'); } } }
/** * Saves the image to disk * * @param \SplFileInfo $file the filename of the new image. Leave null with overwrite = false to automatically create a new file with the .copy pre-extension. the .copy file will be overwritten on next change... * @param string $aOverwrite wether or not to overwrite the file * * @return \imagemanipulation\ImageBuilder $this for chaining */ public function save(\SplFileInfo $file = null, $aOverwrite = false) { $search = "." . $this->file->getExtension(); if ($file === null && $aOverwrite === false && !strstr($this->file->getPathname(), '.copy' . $search)) { $file = new \SplFileInfo(str_replace($search, ".copy" . $search, $this->file->getPathname())); } else { if ($file === null) { $file = $this->file; } } $this->applyFilters(); $this->res->setIsOverwrite($aOverwrite); $this->res->setOutputPath($file); $this->res->createImage(); return $this; }
<?php use imagemanipulation\ImageImageResource; use imagemanipulation\reflection\ImageFilterReflection; use imagemanipulation\ImageType; // create the image resource $resource = new ImageImageResource(new \SplFileInfo(__DIR__ . DIRECTORY_SEPARATOR . 'uglydog.png')); $resource->filter(new ImageFilterReflection(300)); $resource->render(ImageType::PNG, 80);
protected function assertColorQ4(ImageImageResource $aRes, $aColor) { $coord = Coordinate::create($aRes->getWidth() - self::OFFSET, $aRes->getHeight() - self::OFFSET); $testColor = $aRes->getColorAt($coord)->getHexColor(); $this->assertEquals($aColor, $testColor, "Checking color in quadrant 4 {$coord} {$aColor} vs {$testColor}"); }
/** * * @param $aFile \SplFileInfo The orifinal image file * @param $aIdentifier string The identifier to use for caching purposes * * @return \imagemanipulation\ImageImageResource */ public function getImageRes(\SplFileInfo $aFile, $identifier) { $folder = get_called_class(); $folder = str_replace("::", "", $folder); $folder = str_replace("\\", "", $folder); $cacheDir = $this->getCacheDir(); if (strstr($identifier, "::")) { $expl = explode("::", $identifier); $identifier = $expl[1]; } $dir = $cacheDir . DIRECTORY_SEPARATOR . $folder; if (!is_dir($dir)) { mkdir($dir); } $testFile = new \SplFileInfo($dir . DIRECTORY_SEPARATOR . $identifier . "." . $aFile->getExtension()); if ($testFile->isFile()) { $path = $testFile->getPathname(); unset($path); } $res = new ImageImageResource($aFile); $res->setOutputPath($testFile); return $res; }
<?php use imagemanipulation\ImageImageResource; use imagemanipulation\ImageType; use imagemanipulation\rotate\ImageFilterRotate; // create the image resource $resource = new ImageImageResource(new \SplFileInfo(__DIR__ . DIRECTORY_SEPARATOR . 'uglydog.png')); // rotate the image by 45 degrees and fill missing background with #0190D2 $resource->filter(new ImageFilterRotate(45, '#0190D2')); // render the image $resource->render(ImageType::PNG, 80);
protected function assertColorQ4(ImageImageResource $aRes, $aColor) { $testColor = $aRes->getColorAt(new Coordinate($aRes->getX() - 2, $aRes->getY() - 2))->getHexColor(); $this->assertEquals($aColor, $testColor, "Checking color in quadrant 4 " . $aColor . ' vs ' . $testColor); }
<?php use imagemanipulation\ImageImageResource; use imagemanipulation\filter\ImageFilterSobelEdgeDetect; use imagemanipulation\ImageType; use imagemanipulation\filter\ImageFilterSobelEdgeEnhance; use imagemanipulation\filter\ImageFilterNegative; use imagemanipulation\filter\ImageFilterLineDetection; use imagemanipulation\filter\ImageFilterGrayScale; // create the image resource $resource = new ImageImageResource(new \SplFileInfo(__DIR__ . DIRECTORY_SEPARATOR . 'uglydog.jpg')); $filter = new ImageFilterLineDetection(); $resource->filter($filter); $resource->save(__DIR__ . DIRECTORY_SEPARATOR . get_class($filter) . '6.png', ImageType::PNG, 80); $resource->render(ImageType::PNG, 80);
<?php use imagemanipulation\overlay\ImageFilterOverlay; use imagemanipulation\ImageImageResource; use imagemanipulation\ImageType; // create the image resource $resource = new ImageImageResource(new \SplFileInfo(__DIR__ . DIRECTORY_SEPARATOR . 'uglydog.png')); $overlayResource = new ImageImageResource(new \SplFileInfo(__DIR__ . DIRECTORY_SEPARATOR . 'monkey.png')); $overlay = new ImageFilterOverlay($overlayResource, 20, 0, 0, true); $resource->filter($overlay); $resource->render(ImageType::PNG, 80);
/** * * @param $aFile \SplFileInfo The orifinal image file * @param $aIdentifier string The identifier to use for caching purposes * * @return \imagemanipulation\ImageImageResource */ public function getImageRes(\SplFileInfo $aFile, $aIdentifier) { $aIdentifier = str_replace("::", "", $aIdentifier); $aIdentifier = str_replace("\\", "", $aIdentifier); $cacheDir = $this->getCacheDir(); $testFile = new \SplFileInfo($cacheDir . DIRECTORY_SEPARATOR . $aIdentifier . '-' . $aFile->getFilename()); if ($testFile->isFile()) { $path = $testFile->getPathname(); unset($path); } $res = new ImageImageResource($aFile); $res->setOutputPath($testFile); return $res; }