/** * Generates the image file. * * @param string $path if not specified image will be printed on screen * @param string $output mime type for output image (image/png, image/gif, image/jpeg) * @return true on success. Otherwise false */ public function generate($path = null, $output = null) { try { $this->image->generate($path, $output); return true; } catch (Exception $e) { array_push($this->errors, $e->getMessage()); return false; } }
*******************/ $image = new Image($image_file); // vertical [or y, or v], horizontal [or x, or h] // check out Normalize::flip to see all the allowed possibilities $image->flip('vertical')->generate(OUTPUT . 'image5-flip.jpg'); /*********************** *** CROPPING IMAGES *** ***********************/ // Usefull for cropping plugins like https://github.com/tapmodo/Jcrop $image = new Image($image_file); // Values from the cropper // check out Normalize::crop to see all the allowed possibilities $image->crop(['width' => 500, 'height' => 500, 'x' => 50, 'y' => 80])->generate(OUTPUT . 'image6-crop.jpg'); /************************ *** APPLYING FILTERS *** ************************/ $image = new Image($image_file); $image->blur()->sepia()->pixelate(3, true)->vignette()->generate(OUTPUT . 'image7-effects.jpg'); /******************************** *** DIRECTLY TREATING IMAGES *** ********************************/ $image = new Image($image_file); // Get the resource image $resource = $image->getImage(); // Add a string as a note in the top left side $color = imagecolorallocate($resource, 0, 0, 0); imagestring($resource, 5, 10, 10, "My cat, peke", $color); // Return the image resource to the Image instance $image->setImage($resource)->generate(OUTPUT . 'image8-treating-images.jpg'); echo "All examples are now available under the 'output' folder\n"; // END OF FILE
public function testCalculateCropMeasures() { $this->testClass->load("{$this->files_path}peke.jpg"); $cropped = new Image("{$this->files_path}peke.jpg"); $cropped->resizeMin(500, 500); $this->assertArraySubset([41, 41, 205, 205, 164, 164], $this->testClass->calculateCropMeasures($cropped, 20, 20, 100, 100)); $this->testClass->load("{$this->files_path}peke.jpg"); $this->assertArraySubset([26, 26, 128, 128, 102, 102], $this->testClass->calculateCropMeasures("{$this->files_path}peke.gif", ['ox' => 20, 'oy' => 20, 'dx' => 100, 'dy' => 100])); }
/** * @covers Elboletaire\Watimage\Watermark::calculatePosition * @uses Elboletaire\Watimage\Image */ public function testCalculatePosition() { $size_image = 200; $size_watermark = 10; $image = new Image(); $image->create($size_image); $this->testClass->create($size_watermark); $this->testClass->fill('#f00'); $instance = $this->testClass->setPosition('bottom right')->apply($image); $metadata_img = $image->getMetadata(); $calculatePosition = $this->getMethod('calculatePosition'); $position = $calculatePosition->invoke($this->testClass, $metadata_img); $this->assertArraySubset([190, 190], $position); // Setting exact position should return array $this->setProperty('position', [200, 200]); $position = $calculatePosition->invoke($this->testClass, $metadata_img); $this->assertArraySubset([200, 200], $position); // When position is null or size is `full` should set position to `center center` $this->setProperty('position', null); $position = $calculatePosition->invoke($this->testClass, $metadata_img); $this->assertEquals('center center', $this->getProperty('position')); $this->setProperty('position', null); $this->testClass->setSize('full'); $calculatePosition->invoke($this->testClass, $metadata_img); $this->assertEquals('center center', $this->getProperty('position')); // Position calculated without margin $this->testClass->destroy()->create($size_watermark)->fill('#f00')->setPosition('top left'); $position = $calculatePosition->invoke($this->testClass, $metadata_img); $this->assertArraySubset([0, 0], $position); $this->testClass->destroy()->create($size_watermark)->fill('#f00')->setPosition('top center'); $position = $calculatePosition->invoke($this->testClass, $metadata_img); $this->assertArraySubset([95, 0], $position); $this->testClass->destroy()->create($size_watermark)->fill('#f00')->setPosition('top right'); $position = $calculatePosition->invoke($this->testClass, $metadata_img); $this->assertArraySubset([190, 0], $position); $this->testClass->destroy()->create($size_watermark)->fill('#f00')->setPosition('bottom left'); $position = $calculatePosition->invoke($this->testClass, $metadata_img); $this->assertArraySubset([0, 190], $position); $this->testClass->destroy()->create($size_watermark)->fill('#f00')->setPosition('bottom center'); $position = $calculatePosition->invoke($this->testClass, $metadata_img); $this->assertArraySubset([95, 190], $position); $this->testClass->destroy()->create($size_watermark)->fill('#f00')->setPosition('bottom right'); $position = $calculatePosition->invoke($this->testClass, $metadata_img); $this->assertArraySubset([190, 190], $position); // Position calculated with margin $this->testClass->destroy()->create($size_watermark)->fill('#f00')->setPosition('center center')->setMargin(10); $position = $calculatePosition->invoke($this->testClass, $metadata_img); $this->assertArraySubset([105, 105], $position); $this->testClass->destroy()->create($size_watermark)->fill('#f00')->setPosition('center left')->setMargin(10, 0); $position = $calculatePosition->invoke($this->testClass, $metadata_img); $this->assertArraySubset([10, 95], $position); $this->testClass->destroy()->create($size_watermark)->fill('#f00')->setPosition('center right')->setMargin(-10, 10); $position = $calculatePosition->invoke($this->testClass, $metadata_img); $this->assertArraySubset([180, 105], $position); }