[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 3 require_once dirname(__FILE__) . '/../Image.php'; 4 5 /** 6 * This class implements the Horde_Image:: API for PNG images. It 7 * mainly provides some utility functions, such as the ability to make 8 * pixels or solid images for now. 9 * 10 * $Horde: framework/Image/Image/png.php,v 1.19.10.8 2006/05/31 17:06:36 selsky Exp $ 11 * 12 * Copyright 2003-2006 Mike Cochrane <mike@graftonhall.co.nz> 13 * 14 * See the enclosed file COPYING for license information (LGPL). If you 15 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 16 * 17 * @author Mike Cochrane <mike@graftonhall.co.nz> 18 * @since Horde 3.0 19 * @package Horde_Image 20 */ 21 class Horde_Image_png extends Horde_Image { 22 23 /** 24 * The array of pixel data. 25 * 26 * @var array 27 */ 28 var $_img = array(); 29 30 /** 31 * Color depth (only 8 and 16 implemented). 32 * 33 * @var integer 34 */ 35 var $_colorDepth = 8; 36 37 /** 38 * Color type (only 2 (true color) implemented). 39 * 40 * @var integer 41 */ 42 var $_colorType = 2; 43 44 /** 45 * Compression method (0 is the only current valid value). 46 * 47 * @var integer 48 */ 49 var $_compressionMethod = 0; 50 51 /** 52 * Filter method (0 is the only current valid value). 53 * 54 * @var integer 55 */ 56 var $_filterMethod = 0; 57 58 /** 59 * Interlace method (only 0 (no interlace) implemented). 60 * 61 * @var integer 62 */ 63 var $_interlaceMethod = 0; 64 65 /** 66 * PNG image constructor. 67 */ 68 function Horde_Image_png($params) 69 { 70 parent::Horde_Image($params); 71 72 if (!empty($params['width'])) { 73 $this->rectangle(0, 0, $params['width'], $params['height'], $this->_background, $this->_background); 74 } 75 } 76 77 function getContentType() 78 { 79 return 'image/png'; 80 } 81 82 /** 83 * Return the raw data for this image. 84 * 85 * @return string The raw image data. 86 */ 87 function raw() 88 { 89 return 90 $this->_header() . 91 $this->_IHDR() . 92 93 /* Say what created the image file. */ 94 $this->_tEXt('Software', 'Horde Framework Image_png Class') . 95 96 /* Set the last modified date/time. */ 97 $this->_tIME() . 98 99 $this->_IDAT() . 100 $this->_IEND(); 101 } 102 103 /** 104 * Reset the image data. 105 */ 106 function reset() 107 { 108 parent::reset(); 109 $this->_img = array(); 110 } 111 112 /** 113 * Draw a rectangle. 114 * 115 * @param integer $x The left x-coordinate of the rectangle. 116 * @param integer $y The top y-coordinate of the rectangle. 117 * @param integer $width The width of the rectangle. 118 * @param integer $height The height of the rectangle. 119 * @param string $color The line color of the rectangle. 120 * @param string $fill The color to fill the rectangle with. 121 */ 122 function rectangle($x, $y, $width, $height, $color = 'black', $fill = 'none') 123 { 124 list($r, $g, $b) = $this->getRGB($color); 125 if ($fill != 'none') { 126 list($fR, $fG, $fB) = $this->getRGB($fill); 127 } 128 129 $x2 = $x + $width; 130 $y2 = $y + $height; 131 132 for ($h = $y; $h <= $y2; $h++) { 133 for ($w = $x; $w <= $x2; $w++) { 134 // See if we're on an edge. 135 if ($w == $x || $h == $y || $w == $x2 || $h == $y2) { 136 $this->_img[$h][$w] = array('r' => $r, 'g' => $g, 'b' => $b); 137 } elseif ($fill != 'none') { 138 $this->_img[$h][$w] = array('r' => $fR, 'g' => $fG, 'b' => $fB); 139 } 140 } 141 } 142 } 143 144 /** 145 * Create the PNG file header. 146 */ 147 function _header() 148 { 149 return pack('CCCCCCCC', 137, 80, 78, 71, 13, 10, 26, 10); 150 } 151 152 /** 153 * Create Image Header block. 154 */ 155 function _IHDR() 156 { 157 $data = pack('a4NNCCCCC', 'IHDR', $this->_width, $this->_height, $this->_colorDepth, $this->_colorType, $this->_compressionMethod, $this->_filterMethod, $this->_interlaceMethod); 158 return pack('Na' . strlen($data) . 'N', strlen($data) - 4, $data, crc32($data)); 159 } 160 161 /** 162 * Create IEND block. 163 */ 164 function _IEND() 165 { 166 $data = 'IEND'; 167 return pack('Na' . strlen($data) . 'N', strlen($data) - 4, $data, crc32($data)); 168 } 169 170 /** 171 * Create Image Data block. 172 */ 173 function _IDAT() 174 { 175 $data = ''; 176 $prevscanline = null; 177 $filter = 0; 178 for ($i = 0; $i < $this->_height; $i++) { 179 $scanline = array(); 180 $data .= chr($filter); 181 for ($j = 0; $j < $this->_width; $j++) { 182 if ($this->_colorDepth == 8) { 183 $scanline[$j] = pack('CCC', $this->_img[$i][$j]['r'], $this->_img[$i][$j]['g'], $this->_img[$i][$j]['b']); 184 } elseif ($this->_colorDepth == 16) { 185 $scanline[$j] = pack('nnn', $this->_img[$i][$j]['r'] << 8, $this->_img[$i][$j]['g'] << 8, $this->_img[$i][$j]['b'] << 8); 186 } 187 188 if ($filter == 0) { 189 /* No Filter. */ 190 $data .= $scanline[$j]; 191 } elseif ($filter == 2) { 192 /* Up Filter. */ 193 $pixel = $scanline[$j] - $prevscanline[$j]; 194 if ($this->_colorDepth == 8) { 195 $data .= pack('CCC', $pixel >> 16, ($pixel >> 8) & 0xFF, $pixel & 0xFF); 196 } elseif ($this->_colorDepth == 16) { 197 $data .= pack('nnn', ($pixel >> 32), ($pixel >> 16) & 0xFFFF, $pixel & 0xFFFF); 198 } 199 } 200 } 201 $prevscanline = $scanline; 202 } 203 $compressed = gzdeflate($data, 9); 204 205 $data = 'IDAT' . pack('CCa' . strlen($compressed) . 'a4', 0x78, 0x01, $compressed, $this->_Adler32($data)); 206 return pack('Na' . strlen($data) . 'N', strlen($data) - 4, $data, crc32($data)); 207 } 208 209 /** 210 * Create tEXt block. 211 */ 212 function _tEXt($keyword, $text) 213 { 214 $data = 'tEXt' . $keyword . "\0" . $text; 215 return pack('Na' . strlen($data) . 'N', strlen($data) - 4, $data, crc32($data)); 216 } 217 218 /** 219 * Create last modified time block. 220 */ 221 function _tIME($date = null) 222 { 223 if (is_null($date)) { 224 $date = time(); 225 } 226 227 $data = 'tIME' . pack('nCCCCC', intval(date('Y', $date)), intval(date('m', $date)), intval(date('j', $date)), intval(date('G', $date)), intval(date('i', $date)), intval(date('s', $date))); 228 return pack('Na' . strlen($data) . 'N', strlen($data) - 4, $data, crc32($data)); 229 } 230 231 /** 232 * Calculate an Adler32 checksum for a string. 233 */ 234 function _Adler32($input) 235 { 236 $s1 = 1; 237 $s2 = 0; 238 $iMax = strlen($input); 239 for ($i = 0; $i < $iMax; $i++) { 240 $s1 = ($s1 + ord($input[$i])) % 0xFFF1; 241 $s2 = ($s2 + $s1) % 0xFFF1; 242 } 243 return pack('N', (($s2 << 16) | $s1)); 244 } 245 246 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |