/** * Test convertHexToRGB */ public function testConvertHexToRGB() { // First test $hex = 'ffffff'; $rgb = ImageWorkshopLib::convertHexToRGB($hex); $this->assertTrue(getType($rgb) === 'array', 'Expect $rgb to be an array'); $this->assertTrue((array_key_exists('R', $rgb) && array_key_exists('G', $rgb) && array_key_exists('B', $rgb)) == 3, 'Expect $rgb to have the 3 array keys: "R", "G", and "B"'); $this->assertTrue($rgb['R'] === 255, 'Expect $rgb["R"] to be an integer of value 255'); $this->assertTrue($rgb['G'] === 255, 'Expect $rgb["G"] to be an integer of value 255'); $this->assertTrue($rgb['B'] === 255, 'Expect $rgb["B"] to be an integer of value 255'); // Second test $hex = '000000'; $rgb = ImageWorkshopLib::convertHexToRGB($hex); $this->assertTrue($rgb['R'] === 0, 'Expect $rgb["R"] to be an integer of value 0'); $this->assertTrue($rgb['G'] === 0, 'Expect $rgb["G"] to be an integer of value 0'); $this->assertTrue($rgb['B'] === 0, 'Expect $rgb["B"] to be an integer of value 0'); }
/** * Initialize a new virgin layer * * @param integer $width * @param integer $height * @param string $backgroundColor * * @return ImageWorkshopLayer */ public static function initVirginLayer($width = 100, $height = 100, $backgroundColor = null) { $opacity = 0; if (!$backgroundColor || $backgroundColor == 'transparent') { $opacity = 127; $backgroundColor = 'ffffff'; } return new ImageWorkshopLayer(ImageWorkshopLib::generateImage($width, $height, $backgroundColor, $opacity)); }
/** * Resize the background of a layer. * * @param int $newWidth * @param int $newHeight */ public function resizeBackground($newWidth, $newHeight) { $oldWidth = $this->width; $oldHeight = $this->height; $this->width = $newWidth; $this->height = $newHeight; $virginLayoutImage = ImageWorkshopLib::generateImage($this->width, $this->height); imagecopyresampled($virginLayoutImage, $this->image, 0, 0, 0, 0, $this->width, $this->height, $oldWidth, $oldHeight); unset($this->image); $this->image = $virginLayoutImage; }
/** * Generate a new image resource var * * @param integer $width * @param integer $height * @param string $color * @param integer $opacity * * @return resource */ public static function generateImage($width = 100, $height = 100, $color = 'ffffff', $opacity = 127) { $RGBColors = ImageWorkshopLib::convertHexToRGB($color); $image = imagecreatetruecolor($width, $height); imagesavealpha($image, true); $color = imagecolorallocatealpha($image, $RGBColors['R'], $RGBColors['G'], $RGBColors['B'], $opacity); imagefill($image, 0, 0, $color); return $image; }