Contao\GdImage::copyTo PHP Method

copyTo() public method

Save the GD image to a file
public copyTo ( self $gdImage, integer $x, integer $y, integer $width, integer $height ) : static
$gdImage self The target GD image
$x integer The target X coordinate
$y integer The target Y coordinate
$width integer The target width
$height integer The target height
return static
    public function copyTo(self $gdImage, $x, $y, $width, $height)
    {
        imagecopyresampled($gdImage->getResource(), $this->gdResource, $x, $y, 0, 0, $width, $height, imagesx($this->gdResource), imagesy($this->gdResource));
        return $this;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Tests the copyTo() method.
  */
 public function testCopyTo()
 {
     $image = imagecreatetruecolor(100, 100);
     // Whole image black
     imagefill($image, 0, 0, imagecolorallocatealpha($image, 0, 0, 0, 0));
     $image = new GdImage($image);
     $target = GdImage::fromDimensions(100, 100);
     $image->copyTo($target, 10, 10, 80, 80);
     $this->assertEquals(['red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => 0], imagecolorsforindex($target->getResource(), imagecolorat($target->getResource(), 50, 50)), 'Center should be black');
     $this->assertEquals(['red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => 0], imagecolorsforindex($target->getResource(), imagecolorat($target->getResource(), 10, 10)), '10 pixel from left top should be black');
     $this->assertEquals(['red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => 0], imagecolorsforindex($target->getResource(), imagecolorat($target->getResource(), 89, 89)), '10 pixel from right bottom should be black');
     $this->assertEquals(127, imagecolorsforindex($target->getResource(), imagecolorat($target->getResource(), 0, 0))['alpha'], 'Left top pixel should be transparent');
     $this->assertEquals(127, imagecolorsforindex($target->getResource(), imagecolorat($target->getResource(), 99, 99))['alpha'], 'Bottom right pixel should be transparent');
     $this->assertEquals(127, imagecolorsforindex($target->getResource(), imagecolorat($target->getResource(), 9, 9))['alpha'], '9 pixel from left top should be transparent');
     $this->assertEquals(127, imagecolorsforindex($target->getResource(), imagecolorat($target->getResource(), 90, 90))['alpha'], '9 pixel from bottom right should be transparent');
 }