/** * Tests the JImage::crop() method. To test this we create an image that contains a red rectangle * of a certain size [Rectangle1]. Inside of that rectangle [Rectangle1] we draw a white * rectangle [Rectangle2] that is exactly two pixels smaller in width and height than its parent * rectangle [Rectangle1]. Then we crop the image to the exact coordinates of Rectangle1 and * verify both it's corners and the corners inside of it. * * @param mixed $startHeight The original image height. * @param mixed $startWidth The original image width. * @param integer $cropHeight The height of the cropped image. * @param integer $cropWidth The width of the cropped image. * @param integer $cropTop The pixel offset from the top for the cropped image. * @param integer $cropLeft The pixel offset from the left for the cropped image. * @param boolean $transparent True to add transparency to the image. * * @return void * * @dataProvider getCropData * @since 11.3 */ public function testCrop($startHeight, $startWidth, $cropHeight, $cropWidth, $cropTop, $cropLeft, $transparent = false) { // Create a image handle of the correct size. $imageHandle = imagecreatetruecolor($startWidth, $startHeight); // If the transparent flag is true set black to transparent. if ($transparent) { imagecolortransparent($imageHandle, imagecolorallocate($imageHandle, 0, 0, 0)); } // Define red and white. $red = imagecolorallocate($imageHandle, 255, 0, 0); $white = imagecolorallocate($imageHandle, 255, 255, 255); // Draw a red rectangle in the crop area. imagefilledrectangle($imageHandle, $cropLeft, $cropTop, $cropLeft + $cropWidth, $cropTop + $cropHeight, $red); // Draw a white rectangle one pixel inside the crop area. imagefilledrectangle($imageHandle, $cropLeft + 1, $cropTop + 1, $cropLeft + $cropWidth - 2, $cropTop + $cropHeight - 2, $white); // Create a new JImageInspector from the image handle. $image = new JImageInspector($imageHandle); // Crop the image to specifications. $image->crop($cropWidth, $cropHeight, $cropLeft, $cropTop, false); // Verify that the cropped image is the correct size. $this->assertEquals($cropHeight, imagesy($image->getClassProperty('handle'))); $this->assertEquals($cropWidth, imagesx($image->getClassProperty('handle'))); // Validate the correct pixels for the corners. // Top/Left $this->assertEquals($red, imagecolorat($image->getClassProperty('handle'), 0, 0)); $this->assertEquals($white, imagecolorat($image->getClassProperty('handle'), 1, 1)); // Top/Right $this->assertEquals($red, imagecolorat($image->getClassProperty('handle'), 0, $cropHeight - 1)); $this->assertEquals($white, imagecolorat($image->getClassProperty('handle'), 1, $cropHeight - 2)); // Bottom/Left $this->assertEquals($red, imagecolorat($image->getClassProperty('handle'), $cropWidth - 1, 0)); $this->assertEquals($white, imagecolorat($image->getClassProperty('handle'), $cropWidth - 2, 1)); // Bottom/Right $this->assertEquals($red, imagecolorat($image->getClassProperty('handle'), $cropWidth - 1, $cropHeight - 1)); $this->assertEquals($white, imagecolorat($image->getClassProperty('handle'), $cropWidth - 2, $cropHeight - 2)); }