diff --git a/examples/convertImage2BMP.php b/examples/convertImage2BMP.php index 8e4c966..1f13ee3 100755 --- a/examples/convertImage2BMP.php +++ b/examples/convertImage2BMP.php @@ -5,10 +5,10 @@ $graph = new Graphics(); -$imgPath = 'tux.png'; +$imgPath = '../images/tux.png'; $graph->load($imgPath); -$imgPathBW = 'tux.bmp'; +$imgPathBW = '../images/tux.bmp'; $graph->save($imgPathBW, 'BMP'); \ No newline at end of file diff --git a/examples/convertImage2BMPBW.php b/examples/convertImage2BMPBW.php index 4a37307..e444be6 100755 --- a/examples/convertImage2BMPBW.php +++ b/examples/convertImage2BMPBW.php @@ -5,10 +5,10 @@ $graph = new Graphics(); -$imgPath = 'tux.png'; +$imgPath = '../images/tux.png'; $graph->load($imgPath); $graph->convertBW(); -$imgPathBW = 'tuxBW.bmp'; +$imgPathBW = '../images/tuxBW.bmp'; $graph->save($imgPathBW, 'BMP'); \ No newline at end of file diff --git a/examples/convertImage2BW.php b/examples/convertImage2BW.php index db66277..588ca6b 100755 --- a/examples/convertImage2BW.php +++ b/examples/convertImage2BW.php @@ -5,11 +5,11 @@ $graph = new Graphics(); -$imgPath = 'tux.png'; +$imgPath = '../images/tux.png'; $graph->load($imgPath); $graph->convertBW(); -$imgPathBW = 'tuxBW.png'; +$imgPathBW = '../images/tuxBW.png'; $graph->save($imgPathBW, 'PNG'); \ No newline at end of file diff --git a/examples/convertImageBMP2JPG.php b/examples/convertImageBMP2JPG.php old mode 100644 new mode 100755 index 8d77f2d..383139a --- a/examples/convertImageBMP2JPG.php +++ b/examples/convertImageBMP2JPG.php @@ -5,10 +5,10 @@ $graph = new Graphics(); -$imgPath = 'tux.bmp'; +$imgPath = '../images/tux.bmp'; $graph->load($imgPath); -$imgPathJPG = 'tux.jpg'; +$imgPathJPG = '../images/tux.jpg'; $graph->save($imgPathJPG, 'JPG', 100); \ No newline at end of file diff --git a/examples/tux.bmp b/examples/tux.bmp deleted file mode 100755 index ff3686e..0000000 Binary files a/examples/tux.bmp and /dev/null differ diff --git a/examples/tux.jpg b/examples/tux.jpg deleted file mode 100644 index cf55383..0000000 Binary files a/examples/tux.jpg and /dev/null differ diff --git a/examples/tux.png b/examples/tux.png deleted file mode 100755 index bdc5b1d..0000000 Binary files a/examples/tux.png and /dev/null differ diff --git a/examples/tuxBW.bmp b/examples/tuxBW.bmp deleted file mode 100755 index 6c44821..0000000 Binary files a/examples/tuxBW.bmp and /dev/null differ diff --git a/examples/tuxBW.png b/examples/tuxBW.png deleted file mode 100755 index e8f8f95..0000000 Binary files a/examples/tuxBW.png and /dev/null differ diff --git a/images/tuxBW.png b/images/tuxBW.png index e8f8f95..a90b2cc 100644 Binary files a/images/tuxBW.png and b/images/tuxBW.png differ diff --git a/src/Graphics/Graphics.php b/src/Graphics/Graphics.php index 81f3081..c62f896 100755 --- a/src/Graphics/Graphics.php +++ b/src/Graphics/Graphics.php @@ -117,17 +117,29 @@ public function load($filename, $width = null, $height = null) /** * Converts a true color image to Black and white + * even if the image have transparency (alpha channel) */ public function convertBW() { $newimg = imagecreatetruecolor($this->imgWidth, $this->imgHeight); imagealphablending($newimg, false); imagesavealpha($newimg, true); - imagecopyresampled($newimg, $this->img, 0, 0, 0, 0, $this->imgWidth, $this->imgHeight, $this->imgWidth, $this->imgHeight); + imagecopyresampled( + $newimg, + $this->img, + 0, + 0, + 0, + 0, + $this->imgWidth, + $this->imgHeight, + $this->imgWidth, + $this->imgHeight + ); $bcg = imagecolorallocate($newimg, 255, 255, 255); imagefill($newimg, 0, 0, $bcg); imagefilter($newimg, IMG_FILTER_GRAYSCALE); - imagefilter($newimg, IMG_FILTER_CONTRAST, -255); + imagefilter($newimg, IMG_FILTER_CONTRAST, -1000); $this->img = $newimg; } @@ -143,7 +155,7 @@ public function save($filename = null, $type = 'PNG', $quality = 75) { $type = strtoupper($type); if ($type == 'BMP') { - $this->convert2BMP($filename); + $this->saveBMP($filename); return true; } $aTypes = ['PNG', 'JPG', 'JPEG', 'GIF']; @@ -154,54 +166,64 @@ public function save($filename = null, $type = 'PNG', $quality = 75) } /** - * Convert a GD image into a BMP string representation + * resizeImage + * Resize an image + * NOTE: the width is always set to the multiple of 8 more + * next, why? printers have a resolution of 8 dots per mm * - * @param string $filename - * @return string + * @param float $width + * @param float $height + * @throws InvalidArgumentException */ - public function convert2BMP($filename = null) + public function resizeImage($width = null, $height = null) { - if (! is_resource($this->img)) { - return ''; + if ($width == null && $height == null) { + throw new InvalidArgumentException("No dimensions was passed."); } - //to remove alpha color and put white instead - $img = $this->img; - $imageX = imagesx($img); - $imageY = imagesy($img); - $bmp = ''; - for ($yInd = ($imageY - 1); $yInd >= 0; $yInd--) { - $thisline = ''; - for ($xInd = 0; $xInd < $imageX; $xInd++) { - $argb = self::getPixelColor($img, $xInd, $yInd); - $thisline .= chr($argb['blue']).chr($argb['green']).chr($argb['red']); - } - while (strlen($thisline) % 4) { - $thisline .= "\x00"; - } - $bmp .= $thisline; + if ($width != null) { + $width = $this->closestMultiple($width); + $razao = $width / $this->imgWidth; + $height = (int) round($razao * $this->imgHeight); + } elseif ($width == null && $height != null) { + $razao = $height / $this->imgHeight; + $width = (int) round($razao * $this->imgWidth); + $width = $this->closestMultiple($width); } - $bmpSize = strlen($bmp) + 14 + 40; - // bitMapHeader [14 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_62uq.asp - $bitMapHeader = 'BM'; // WORD bfType; - $bitMapHeader .= self::littleEndian2String($bmpSize, 4); // DWORD bfSize; - $bitMapHeader .= self::littleEndian2String(0, 2); // WORD bfReserved1; - $bitMapHeader .= self::littleEndian2String(0, 2); // WORD bfReserved2; - $bitMapHeader .= self::littleEndian2String(54, 4); // DWORD bfOffBits; - // bitMapInfoHeader - [40 bytes] http://msdn.microsoft.com/library/en-us/gdi/bitmaps_1rw2.asp - $bitMapInfoHeader = self::littleEndian2String(40, 4); // DWORD biSize; - $bitMapInfoHeader .= self::littleEndian2String($imageX, 4); // LONG biWidth; - $bitMapInfoHeader .= self::littleEndian2String($imageY, 4); // LONG biHeight; - $bitMapInfoHeader .= self::littleEndian2String(1, 2); // WORD biPlanes; - $bitMapInfoHeader .= self::littleEndian2String(24, 2); // WORD biBitCount; - $bitMapInfoHeader .= self::littleEndian2String(0, 4); // DWORD biCompression; - $bitMapInfoHeader .= self::littleEndian2String(0, 4); // DWORD biSizeImage; - $bitMapInfoHeader .= self::littleEndian2String(2835, 4); // LONG biXPelsPerMeter; - $bitMapInfoHeader .= self::littleEndian2String(2835, 4); // LONG biYPelsPerMeter; - $bitMapInfoHeader .= self::littleEndian2String(0, 4); // DWORD biClrUsed; - $bitMapInfoHeader .= self::littleEndian2String(0, 4); // DWORD biClrImportant; - $data = $bitMapHeader.$bitMapInfoHeader.$bmp; - $this->saveImage($filename, $data); - return $data; + $tempimg = imagecreatetruecolor($width, $height); + imagecopyresampled($tempimg, $this->img, 0, 0, 0, 0, $width, $height, $this->imgWidth, $this->imgHeight); + $this->img = $tempimg; + $this->getDimImage(); + } + + /** + * Creates a GD QRCode image + * + * @param string $dataText + * @param int $width + * @param int $padding + * @param string $errCorretion LOW, MEDIUM, QUARTILE, HIGH + * @return void + */ + public function imageQRCode( + $dataText = 'NADA NADA NADA NADA NADA NADA NADA NADA NADA NADA NADA NADA', + $width = 200, + $padding = 10, + $errCorretion = 'MEDIUM' + ) { + //adjust width for a closest multiple of 8 + $width = $this->closestMultiple($width, 8); + $qrCode = new QrCode(); + $qrCode->setText($dataText) + ->setImageType('png') + ->setSize($width) + ->setPadding($padding) + ->setErrorCorrection($errCorretion) + ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0)) + ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0)) + ->setLabel('') + ->setLabelFontSize(8); + $this->img = $qrCode->getImage(); + $this->getDimImage(); } /** @@ -211,7 +233,7 @@ public function convert2BMP($filename = null) * @param string $filename * @return boolean */ - public function loadBMP($filename) + protected function loadBMP($filename) { //open file as binary if (! $f1 = fopen($filename, "rb")) { @@ -270,133 +292,56 @@ public function loadBMP($filename) $this->img = $res; return true; } - - /** - * Get byte color form BMP - * - * @param integer $bpp bytes_per_pixel - * @param string $img bytes read of file - * @param string $vide - * @param integer $p - * @param integer $palette - * @return integer|boolean - */ - private function getBMPColor($bpp, $img, $vide, $p, $palette) - { - switch ($bpp) { - case 24: - return unpack("V", substr($img, $p, 3).$vide); - break; - case 16: - $color = unpack("v", substr($img, $p, 2)); - $blue = ($color[1] & 0x001f) << 3; - $green = ($color[1] & 0x07e0) >> 3; - $red = ($color[1] & 0xf800) >> 8; - $color[1] = $red * 65536 + $green * 256 + $blue; - return $color; - break; - case 8: - $color = unpack("n", $vide.substr($img, $p, 1)); - $color[1] = $palette[$color[1]+1]; - return $color; - break; - case 4: - $color = unpack("n", $vide.substr($img, floor($p), 1)); - if (($p*2)%2 == 0) { - $color[1] = ($color[1] >> 4) ; - } else { - $color[1] = ($color[1] & 0x0F); - } - $color[1] = $palette[$color[1]+1]; - return $color; - break; - case 1: - $color = unpack("n", $vide.substr($img, floor($p), 1)); - if (($p*8)%8 == 0) { - $color[1] = $color[1]>>7; - } elseif (($p*8)%8 == 1) { - $color[1] = ($color[1] & 0x40)>>6; - } elseif (($p*8)%8 == 2) { - $color[1] = ($color[1] & 0x20)>>5; - } elseif (($p*8)%8 == 3) { - $color[1] = ($color[1] & 0x10)>>4; - } elseif (($P*8)%8 == 4) { - $color[1] = ($color[1] & 0x8)>>3; - } elseif (($p*8)%8 == 5) { - $color[1] = ($color[1] & 0x4)>>2; - } elseif (($p*8)%8 == 6) { - $color[1] = ($color[1] & 0x2)>>1; - } elseif (($p*8)%8 == 7) { - $color[1] = ($color[1] & 0x1); - } - $color[1] = $palette[$color[1]+1]; - return $color; - break; - default: - return false; - } - } - /** - * resizeImage - * Resize an image - * NOTE: the width is always set to the multiple of 8 more - * next, why? printers have a resolution of 8 dots per mm + * Convert a GD image into a BMP string representation * - * @param float $width - * @param float $height - * @throws InvalidArgumentException + * @param string $filename + * @return string */ - public function resizeImage($width = null, $height = null) + protected function saveBMP($filename = null) { - if ($width == null && $height == null) { - throw new InvalidArgumentException("No dimensions was passed."); + if (! is_resource($this->img)) { + return ''; } - if ($width != null) { - $width = $this->closestMultiple($width); - $razao = $width / $this->imgWidth; - $height = (int) round($razao * $this->imgHeight); - } elseif ($width == null && $height != null) { - $razao = $height / $this->imgHeight; - $width = (int) round($razao * $this->imgWidth); - $width = $this->closestMultiple($width); + //to remove alpha color and put white instead + $img = $this->img; + $imageX = imagesx($img); + $imageY = imagesy($img); + $bmp = ''; + for ($yInd = ($imageY - 1); $yInd >= 0; $yInd--) { + $thisline = ''; + for ($xInd = 0; $xInd < $imageX; $xInd++) { + $argb = self::getPixelColor($img, $xInd, $yInd); + $thisline .= chr($argb['blue']).chr($argb['green']).chr($argb['red']); + } + while (strlen($thisline) % 4) { + $thisline .= "\x00"; + } + $bmp .= $thisline; } - $tempimg = imagecreatetruecolor($width, $height); - imagecopyresampled($tempimg, $this->img, 0, 0, 0, 0, $width, $height, $this->imgWidth, $this->imgHeight); - $this->img = $tempimg; - $this->getDimImage(); - } - - /** - * Creates a GD QRCode image - * - * @param string $dataText - * @param int $width - * @param int $padding - * @param string $errCorretion LOW, MEDIUM, QUARTILE, HIGH - * @return void - */ - public function imageQRCode( - $dataText = 'NADA NADA NADA NADA NADA NADA NADA NADA NADA NADA NADA NADA', - $width = 200, - $padding = 10, - $errCorretion = 'MEDIUM' - ) { - //adjust width for a closest multiple of 8 - $width = $this->closestMultiple($width, 8); - $qrCode = new QrCode(); - $qrCode->setText($dataText) - ->setImageType('png') - ->setSize($width) - ->setPadding($padding) - ->setErrorCorrection($errCorretion) - ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0)) - ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0)) - ->setLabel('') - ->setLabelFontSize(8); - $this->img = $qrCode->getImage(); - $this->getDimImage(); + $bmpSize = strlen($bmp) + 14 + 40; + // bitMapHeader [14 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_62uq.asp + $bitMapHeader = 'BM'; // WORD bfType; + $bitMapHeader .= self::littleEndian2String($bmpSize, 4); // DWORD bfSize; + $bitMapHeader .= self::littleEndian2String(0, 2); // WORD bfReserved1; + $bitMapHeader .= self::littleEndian2String(0, 2); // WORD bfReserved2; + $bitMapHeader .= self::littleEndian2String(54, 4); // DWORD bfOffBits; + // bitMapInfoHeader - [40 bytes] http://msdn.microsoft.com/library/en-us/gdi/bitmaps_1rw2.asp + $bitMapInfoHeader = self::littleEndian2String(40, 4); // DWORD biSize; + $bitMapInfoHeader .= self::littleEndian2String($imageX, 4); // LONG biWidth; + $bitMapInfoHeader .= self::littleEndian2String($imageY, 4); // LONG biHeight; + $bitMapInfoHeader .= self::littleEndian2String(1, 2); // WORD biPlanes; + $bitMapInfoHeader .= self::littleEndian2String(24, 2); // WORD biBitCount; + $bitMapInfoHeader .= self::littleEndian2String(0, 4); // DWORD biCompression; + $bitMapInfoHeader .= self::littleEndian2String(0, 4); // DWORD biSizeImage; + $bitMapInfoHeader .= self::littleEndian2String(2835, 4); // LONG biXPelsPerMeter; + $bitMapInfoHeader .= self::littleEndian2String(2835, 4); // LONG biYPelsPerMeter; + $bitMapInfoHeader .= self::littleEndian2String(0, 4); // DWORD biClrUsed; + $bitMapInfoHeader .= self::littleEndian2String(0, 4); // DWORD biClrImportant; + $data = $bitMapHeader.$bitMapInfoHeader.$bmp; + $this->saveImage($filename, $data); + return $data; } /** @@ -493,7 +438,7 @@ protected function convertRaster() * @throws InvalidArgumentException * @throws RuntimeException */ - public function saveImage($filename = null, $data = null, $type = 'PNG', $quality = 75) + protected function saveImage($filename = null, $data = null, $type = 'PNG', $quality = 75) { if (empty($filename) || empty($data)) { return false; @@ -528,6 +473,72 @@ public function saveImage($filename = null, $data = null, $type = 'PNG', $qualit } return true; } + + /** + * Get byte color form BMP + * + * @param integer $bpp bytes_per_pixel + * @param string $img bytes read of file + * @param string $vide + * @param integer $p + * @param integer $palette + * @return integer|boolean + */ + private function getBMPColor($bpp, $img, $vide, $p, $palette) + { + switch ($bpp) { + case 24: + return unpack("V", substr($img, $p, 3).$vide); + break; + case 16: + $color = unpack("v", substr($img, $p, 2)); + $blue = ($color[1] & 0x001f) << 3; + $green = ($color[1] & 0x07e0) >> 3; + $red = ($color[1] & 0xf800) >> 8; + $color[1] = $red * 65536 + $green * 256 + $blue; + return $color; + break; + case 8: + $color = unpack("n", $vide.substr($img, $p, 1)); + $color[1] = $palette[$color[1]+1]; + return $color; + break; + case 4: + $color = unpack("n", $vide.substr($img, floor($p), 1)); + if (($p*2)%2 == 0) { + $color[1] = ($color[1] >> 4) ; + } else { + $color[1] = ($color[1] & 0x0F); + } + $color[1] = $palette[$color[1]+1]; + return $color; + break; + case 1: + $color = unpack("n", $vide.substr($img, floor($p), 1)); + if (($p*8)%8 == 0) { + $color[1] = $color[1]>>7; + } elseif (($p*8)%8 == 1) { + $color[1] = ($color[1] & 0x40)>>6; + } elseif (($p*8)%8 == 2) { + $color[1] = ($color[1] & 0x20)>>5; + } elseif (($p*8)%8 == 3) { + $color[1] = ($color[1] & 0x10)>>4; + } elseif (($P*8)%8 == 4) { + $color[1] = ($color[1] & 0x8)>>3; + } elseif (($p*8)%8 == 5) { + $color[1] = ($color[1] & 0x4)>>2; + } elseif (($p*8)%8 == 6) { + $color[1] = ($color[1] & 0x2)>>1; + } elseif (($p*8)%8 == 7) { + $color[1] = ($color[1] & 0x1); + } + $color[1] = $palette[$color[1]+1]; + return $color; + break; + default: + return false; + } + } /** * Converts Litte Endian Bytes do String