imagefile.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Abstraction for an image file
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Image
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Zach Copley <zach@status.net>
  26. * @copyright 2008-2009 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('GNUSOCIAL')) { exit(1); }
  31. /**
  32. * A wrapper on uploaded files
  33. *
  34. * Makes it slightly easier to accept an image file from upload.
  35. *
  36. * @category Image
  37. * @package StatusNet
  38. * @author Evan Prodromou <evan@status.net>
  39. * @author Zach Copley <zach@status.net>
  40. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  41. * @link http://status.net/
  42. */
  43. class ImageFile
  44. {
  45. var $id;
  46. var $filepath;
  47. var $filename;
  48. var $type;
  49. var $height;
  50. var $width;
  51. var $rotate=0; // degrees to rotate for properly oriented image (extrapolated from EXIF etc.)
  52. var $animated = null; // Animated image? (has more than 1 frame). null means untested
  53. function __construct($id=null, $filepath=null, $type=null, $width=null, $height=null)
  54. {
  55. $this->id = $id;
  56. $this->filepath = $filepath;
  57. $this->filename = basename($filepath);
  58. $info = @getimagesize($this->filepath);
  59. if (!(
  60. ($info[2] == IMAGETYPE_GIF && function_exists('imagecreatefromgif')) ||
  61. ($info[2] == IMAGETYPE_JPEG && function_exists('imagecreatefromjpeg')) ||
  62. $info[2] == IMAGETYPE_BMP ||
  63. ($info[2] == IMAGETYPE_WBMP && function_exists('imagecreatefromwbmp')) ||
  64. ($info[2] == IMAGETYPE_XBM && function_exists('imagecreatefromxbm')) ||
  65. ($info[2] == IMAGETYPE_PNG && function_exists('imagecreatefrompng')))) {
  66. // TRANS: Exception thrown when trying to upload an unsupported image file format.
  67. throw new UnsupportedMediaException(_('Unsupported image format.'), $this->filepath);
  68. }
  69. $this->type = ($info) ? $info[2]:$type;
  70. $this->width = ($info) ? $info[0]:$width;
  71. $this->height = ($info) ? $info[1]:$height;
  72. if ($this->type == IMAGETYPE_JPEG && function_exists('exif_read_data')) {
  73. // Orientation value to rotate thumbnails properly
  74. $exif = exif_read_data($this->filepath);
  75. if (is_array($exif) && isset($exif['Orientation'])) {
  76. switch ((int)$exif['Orientation']) {
  77. case 1: // top is top
  78. $this->rotate = 0;
  79. break;
  80. case 3: // top is bottom
  81. $this->rotate = 180;
  82. break;
  83. case 6: // top is right
  84. $this->rotate = -90;
  85. break;
  86. case 8: // top is left
  87. $this->rotate = 90;
  88. break;
  89. }
  90. // If we ever write this back, Orientation should be set to '1'
  91. }
  92. } elseif ($this->type === IMAGETYPE_GIF) {
  93. $this->animated = $this->isAnimatedGif();
  94. }
  95. Event::handle('FillImageFileMetadata', array($this));
  96. }
  97. public static function fromFileObject(File $file)
  98. {
  99. $imgPath = null;
  100. $media = common_get_mime_media($file->mimetype);
  101. if (Event::handle('CreateFileImageThumbnailSource', array($file, &$imgPath, $media))) {
  102. if (empty($file->filename)) {
  103. throw new UnsupportedMediaException(_('File without filename could not get a thumbnail source.'));
  104. }
  105. switch ($media) {
  106. case 'image':
  107. $imgPath = $file->getPath();
  108. break;
  109. default:
  110. throw new UnsupportedMediaException(_('Unsupported media format.'), $file->getPath());
  111. }
  112. }
  113. if (!file_exists($imgPath)) {
  114. throw new ServerException(sprintf('Image not available locally: %s', $imgPath));
  115. }
  116. try {
  117. $image = new ImageFile($file->id, $imgPath);
  118. } catch (UnsupportedMediaException $e) {
  119. // Avoid deleting the original
  120. if ($imgPath != $file->getPath()) {
  121. unlink($imgPath);
  122. }
  123. throw $e;
  124. }
  125. return $image;
  126. }
  127. public function getPath()
  128. {
  129. if (!file_exists($this->filepath)) {
  130. throw new ServerException('No file in ImageFile filepath');
  131. }
  132. return $this->filepath;
  133. }
  134. static function fromUpload($param='upload')
  135. {
  136. switch ($_FILES[$param]['error']) {
  137. case UPLOAD_ERR_OK: // success, jump out
  138. break;
  139. case UPLOAD_ERR_INI_SIZE:
  140. case UPLOAD_ERR_FORM_SIZE:
  141. // TRANS: Exception thrown when too large a file is uploaded.
  142. // TRANS: %s is the maximum file size, for example "500b", "10kB" or "2MB".
  143. throw new Exception(sprintf(_('That file is too big. The maximum file size is %s.'), ImageFile::maxFileSize()));
  144. case UPLOAD_ERR_PARTIAL:
  145. @unlink($_FILES[$param]['tmp_name']);
  146. // TRANS: Exception thrown when uploading an image and that action could not be completed.
  147. throw new Exception(_('Partial upload.'));
  148. case UPLOAD_ERR_NO_FILE:
  149. // No file; probably just a non-AJAX submission.
  150. default:
  151. common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " . $_FILES[$param]['error']);
  152. // TRANS: Exception thrown when uploading an image fails for an unknown reason.
  153. throw new Exception(_('System error uploading file.'));
  154. }
  155. $info = @getimagesize($_FILES[$param]['tmp_name']);
  156. if (!$info) {
  157. @unlink($_FILES[$param]['tmp_name']);
  158. // TRANS: Exception thrown when uploading a file as image that is not an image or is a corrupt file.
  159. throw new UnsupportedMediaException(_('Not an image or corrupt file.'), '[deleted]');
  160. }
  161. return new ImageFile(null, $_FILES[$param]['tmp_name']);
  162. }
  163. /**
  164. * Compat interface for old code generating avatar thumbnails...
  165. * Saves the scaled file directly into the avatar area.
  166. *
  167. * @param int $size target width & height -- must be square
  168. * @param int $x (default 0) upper-left corner to crop from
  169. * @param int $y (default 0) upper-left corner to crop from
  170. * @param int $w (default full) width of image area to crop
  171. * @param int $h (default full) height of image area to crop
  172. * @return string filename
  173. */
  174. function resize($size, $x = 0, $y = 0, $w = null, $h = null)
  175. {
  176. $targetType = $this->preferredType();
  177. $outname = Avatar::filename($this->id,
  178. image_type_to_extension($targetType),
  179. $size,
  180. common_timestamp());
  181. $outpath = Avatar::path($outname);
  182. $this->resizeTo($outpath, array('width'=>$size, 'height'=>$size,
  183. 'x'=>$x, 'y'=>$y,
  184. 'w'=>$w, 'h'=>$h));
  185. return $outname;
  186. }
  187. /**
  188. * Copy the image file to the given destination.
  189. *
  190. * This function may modify the resulting file. Please use the
  191. * returned ImageFile object to read metadata (width, height etc.)
  192. *
  193. * @param string $outpath
  194. * @return ImageFile the image stored at target path
  195. */
  196. function copyTo($outpath)
  197. {
  198. return new ImageFile(null, $this->resizeTo($outpath));
  199. }
  200. /**
  201. * Create and save a thumbnail image.
  202. *
  203. * @param string $outpath
  204. * @param array $box width, height, boundary box (x,y,w,h) defaults to full image
  205. * @return string full local filesystem filename
  206. */
  207. function resizeTo($outpath, array $box=array())
  208. {
  209. $box['width'] = isset($box['width']) ? intval($box['width']) : $this->width;
  210. $box['height'] = isset($box['height']) ? intval($box['height']) : $this->height;
  211. $box['x'] = isset($box['x']) ? intval($box['x']) : 0;
  212. $box['y'] = isset($box['y']) ? intval($box['y']) : 0;
  213. $box['w'] = isset($box['w']) ? intval($box['w']) : $this->width;
  214. $box['h'] = isset($box['h']) ? intval($box['h']) : $this->height;
  215. if (!file_exists($this->filepath)) {
  216. // TRANS: Exception thrown during resize when image has been registered as present, but is no longer there.
  217. throw new Exception(_('Lost our file.'));
  218. }
  219. // Don't rotate/crop/scale if it isn't necessary
  220. if ($box['width'] === $this->width
  221. && $box['height'] === $this->height
  222. && $box['x'] === 0
  223. && $box['y'] === 0
  224. && $box['w'] === $this->width
  225. && $box['h'] === $this->height
  226. && $this->type == $this->preferredType()) {
  227. if ($this->rotate == 0) {
  228. // No rotational difference, just copy it as-is
  229. @copy($this->filepath, $outpath);
  230. return $outpath;
  231. } elseif (abs($this->rotate) == 90) {
  232. // Box is rotated 90 degrees in either direction,
  233. // so we have to redefine x to y and vice versa.
  234. $tmp = $box['width'];
  235. $box['width'] = $box['height'];
  236. $box['height'] = $tmp;
  237. $tmp = $box['x'];
  238. $box['x'] = $box['y'];
  239. $box['y'] = $tmp;
  240. $tmp = $box['w'];
  241. $box['w'] = $box['h'];
  242. $box['h'] = $tmp;
  243. }
  244. }
  245. if (Event::handle('StartResizeImageFile', array($this, $outpath, $box))) {
  246. $this->resizeToFile($outpath, $box);
  247. }
  248. if (!file_exists($outpath)) {
  249. throw new UseFileAsThumbnailException($this->id);
  250. }
  251. return $outpath;
  252. }
  253. protected function resizeToFile($outpath, array $box)
  254. {
  255. switch ($this->type) {
  256. case IMAGETYPE_GIF:
  257. $image_src = imagecreatefromgif($this->filepath);
  258. break;
  259. case IMAGETYPE_JPEG:
  260. $image_src = imagecreatefromjpeg($this->filepath);
  261. break;
  262. case IMAGETYPE_PNG:
  263. $image_src = imagecreatefrompng($this->filepath);
  264. break;
  265. case IMAGETYPE_BMP:
  266. $image_src = imagecreatefrombmp($this->filepath);
  267. break;
  268. case IMAGETYPE_WBMP:
  269. $image_src = imagecreatefromwbmp($this->filepath);
  270. break;
  271. case IMAGETYPE_XBM:
  272. $image_src = imagecreatefromxbm($this->filepath);
  273. break;
  274. default:
  275. // TRANS: Exception thrown when trying to resize an unknown file type.
  276. throw new Exception(_('Unknown file type'));
  277. }
  278. if ($this->rotate != 0) {
  279. $image_src = imagerotate($image_src, $this->rotate, 0);
  280. }
  281. $image_dest = imagecreatetruecolor($box['width'], $box['height']);
  282. if ($this->type == IMAGETYPE_GIF || $this->type == IMAGETYPE_PNG || $this->type == IMAGETYPE_BMP) {
  283. $transparent_idx = imagecolortransparent($image_src);
  284. if ($transparent_idx >= 0) {
  285. $transparent_color = imagecolorsforindex($image_src, $transparent_idx);
  286. $transparent_idx = imagecolorallocate($image_dest, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
  287. imagefill($image_dest, 0, 0, $transparent_idx);
  288. imagecolortransparent($image_dest, $transparent_idx);
  289. } elseif ($this->type == IMAGETYPE_PNG) {
  290. imagealphablending($image_dest, false);
  291. $transparent = imagecolorallocatealpha($image_dest, 0, 0, 0, 127);
  292. imagefill($image_dest, 0, 0, $transparent);
  293. imagesavealpha($image_dest, true);
  294. }
  295. }
  296. imagecopyresampled($image_dest, $image_src, 0, 0, $box['x'], $box['y'], $box['width'], $box['height'], $box['w'], $box['h']);
  297. switch ($this->preferredType()) {
  298. case IMAGETYPE_GIF:
  299. imagegif($image_dest, $outpath);
  300. break;
  301. case IMAGETYPE_JPEG:
  302. imagejpeg($image_dest, $outpath, common_config('image', 'jpegquality'));
  303. break;
  304. case IMAGETYPE_PNG:
  305. imagepng($image_dest, $outpath);
  306. break;
  307. default:
  308. // TRANS: Exception thrown when trying resize an unknown file type.
  309. throw new Exception(_('Unknown file type'));
  310. }
  311. imagedestroy($image_src);
  312. imagedestroy($image_dest);
  313. }
  314. /**
  315. * Several obscure file types should be normalized to PNG on resize.
  316. *
  317. * @fixme consider flattening anything not GIF or JPEG to PNG
  318. * @return int
  319. */
  320. function preferredType()
  321. {
  322. if($this->type == IMAGETYPE_BMP) {
  323. //we don't want to save BMP... it's an inefficient, rare, antiquated format
  324. //save png instead
  325. return IMAGETYPE_PNG;
  326. } else if($this->type == IMAGETYPE_WBMP) {
  327. //we don't want to save WBMP... it's a rare format that we can't guarantee clients will support
  328. //save png instead
  329. return IMAGETYPE_PNG;
  330. } else if($this->type == IMAGETYPE_XBM) {
  331. //we don't want to save XBM... it's a rare format that we can't guarantee clients will support
  332. //save png instead
  333. return IMAGETYPE_PNG;
  334. }
  335. return $this->type;
  336. }
  337. function unlink()
  338. {
  339. @unlink($this->filepath);
  340. }
  341. static function maxFileSize()
  342. {
  343. $value = ImageFile::maxFileSizeInt();
  344. if ($value > 1024 * 1024) {
  345. $value = $value/(1024*1024);
  346. // TRANS: Number of megabytes. %d is the number.
  347. return sprintf(_m('%dMB','%dMB',$value),$value);
  348. } else if ($value > 1024) {
  349. $value = $value/1024;
  350. // TRANS: Number of kilobytes. %d is the number.
  351. return sprintf(_m('%dkB','%dkB',$value),$value);
  352. } else {
  353. // TRANS: Number of bytes. %d is the number.
  354. return sprintf(_m('%dB','%dB',$value),$value);
  355. }
  356. }
  357. static function maxFileSizeInt()
  358. {
  359. return min(ImageFile::strToInt(ini_get('post_max_size')),
  360. ImageFile::strToInt(ini_get('upload_max_filesize')),
  361. ImageFile::strToInt(ini_get('memory_limit')));
  362. }
  363. static function strToInt($str)
  364. {
  365. $unit = substr($str, -1);
  366. $num = substr($str, 0, -1);
  367. switch(strtoupper($unit)){
  368. case 'G':
  369. $num *= 1024;
  370. case 'M':
  371. $num *= 1024;
  372. case 'K':
  373. $num *= 1024;
  374. }
  375. return $num;
  376. }
  377. public function scaleToFit($maxWidth=null, $maxHeight=null, $crop=null)
  378. {
  379. return self::getScalingValues($this->width, $this->height,
  380. $maxWidth, $maxHeight, $crop, $this->rotate);
  381. }
  382. /*
  383. * Gets scaling values for images of various types. Cropping can be enabled.
  384. *
  385. * Values will scale _up_ to fit max values if cropping is enabled!
  386. * With cropping disabled, the max value of each axis will be respected.
  387. *
  388. * @param $width int Original width
  389. * @param $height int Original height
  390. * @param $maxW int Resulting max width
  391. * @param $maxH int Resulting max height
  392. * @param $crop int Crop to the size (not preserving aspect ratio)
  393. */
  394. public static function getScalingValues($width, $height,
  395. $maxW=null, $maxH=null,
  396. $crop=null, $rotate=0)
  397. {
  398. $maxW = $maxW ?: common_config('thumbnail', 'width');
  399. $maxH = $maxH ?: common_config('thumbnail', 'height');
  400. if ($maxW < 1 || ($maxH !== null && $maxH < 1)) {
  401. throw new ServerException('Bad parameters for ImageFile::getScalingValues');
  402. } elseif ($maxH === null) {
  403. // if maxH is null, we set maxH to equal maxW and enable crop
  404. $maxH = $maxW;
  405. $crop = true;
  406. }
  407. // Because GD doesn't understand EXIF orientation etc.
  408. if (abs($rotate) == 90) {
  409. $tmp = $width;
  410. $width = $height;
  411. $height = $tmp;
  412. }
  413. // Cropping data (for original image size). Default values, 0 and null,
  414. // imply no cropping and with preserved aspect ratio (per axis).
  415. $cx = 0; // crop x
  416. $cy = 0; // crop y
  417. $cw = null; // crop area width
  418. $ch = null; // crop area height
  419. if ($crop) {
  420. $s_ar = $width / $height;
  421. $t_ar = $maxW / $maxH;
  422. $rw = $maxW;
  423. $rh = $maxH;
  424. // Source aspect ratio differs from target, recalculate crop points!
  425. if ($s_ar > $t_ar) {
  426. $cx = floor($width / 2 - $height * $t_ar / 2);
  427. $cw = ceil($height * $t_ar);
  428. } elseif ($s_ar < $t_ar) {
  429. $cy = floor($height / 2 - $width / $t_ar / 2);
  430. $ch = ceil($width / $t_ar);
  431. }
  432. } else {
  433. $rw = $maxW;
  434. $rh = ceil($height * $rw / $width);
  435. // Scaling caused too large height, decrease to max accepted value
  436. if ($rh > $maxH) {
  437. $rh = $maxH;
  438. $rw = ceil($width * $rh / $height);
  439. }
  440. }
  441. return array(intval($rw), intval($rh),
  442. intval($cx), intval($cy),
  443. is_null($cw) ? $width : intval($cw),
  444. is_null($ch) ? $height : intval($ch));
  445. }
  446. /**
  447. * Animated GIF test, courtesy of frank at huddler dot com et al:
  448. * http://php.net/manual/en/function.imagecreatefromgif.php#104473
  449. * Modified so avoid landing inside of a header (and thus not matching our regexp).
  450. */
  451. protected function isAnimatedGif()
  452. {
  453. if (!($fh = @fopen($this->filepath, 'rb'))) {
  454. return false;
  455. }
  456. $count = 0;
  457. //an animated gif contains multiple "frames", with each frame having a
  458. //header made up of:
  459. // * a static 4-byte sequence (\x00\x21\xF9\x04)
  460. // * 4 variable bytes
  461. // * a static 2-byte sequence (\x00\x2C)
  462. // In total the header is maximum 10 bytes.
  463. // We read through the file til we reach the end of the file, or we've found
  464. // at least 2 frame headers
  465. while(!feof($fh) && $count < 2) {
  466. $chunk = fread($fh, 1024 * 100); //read 100kb at a time
  467. $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00\x2C#s', $chunk, $matches);
  468. // rewind in case we ended up in the middle of the header, but avoid
  469. // infinite loop (i.e. don't rewind if we're already in the end).
  470. if (!feof($fh) && ftell($fh) >= 9) {
  471. fseek($fh, -9, SEEK_CUR);
  472. }
  473. }
  474. fclose($fh);
  475. return $count > 1;
  476. }
  477. }
  478. //PHP doesn't (as of 2/24/2010) have an imagecreatefrombmp so conditionally define one
  479. if(!function_exists('imagecreatefrombmp')){
  480. //taken shamelessly from http://www.php.net/manual/en/function.imagecreatefromwbmp.php#86214
  481. function imagecreatefrombmp($p_sFile)
  482. {
  483. // Load the image into a string
  484. $file = fopen($p_sFile,"rb");
  485. $read = fread($file,10);
  486. while(!feof($file)&&($read<>""))
  487. $read .= fread($file,1024);
  488. $temp = unpack("H*",$read);
  489. $hex = $temp[1];
  490. $header = substr($hex,0,108);
  491. // Process the header
  492. // Structure: http://www.fastgraph.com/help/bmp_header_format.html
  493. if (substr($header,0,4)=="424d")
  494. {
  495. // Cut it in parts of 2 bytes
  496. $header_parts = str_split($header,2);
  497. // Get the width 4 bytes
  498. $width = hexdec($header_parts[19].$header_parts[18]);
  499. // Get the height 4 bytes
  500. $height = hexdec($header_parts[23].$header_parts[22]);
  501. // Unset the header params
  502. unset($header_parts);
  503. }
  504. // Define starting X and Y
  505. $x = 0;
  506. $y = 1;
  507. // Create newimage
  508. $image = imagecreatetruecolor($width,$height);
  509. // Grab the body from the image
  510. $body = substr($hex,108);
  511. // Calculate if padding at the end-line is needed
  512. // Divided by two to keep overview.
  513. // 1 byte = 2 HEX-chars
  514. $body_size = (strlen($body)/2);
  515. $header_size = ($width*$height);
  516. // Use end-line padding? Only when needed
  517. $usePadding = ($body_size>($header_size*3)+4);
  518. // Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption
  519. // Calculate the next DWORD-position in the body
  520. for ($i=0;$i<$body_size;$i+=3)
  521. {
  522. // Calculate line-ending and padding
  523. if ($x>=$width)
  524. {
  525. // If padding needed, ignore image-padding
  526. // Shift i to the ending of the current 32-bit-block
  527. if ($usePadding)
  528. $i += $width%4;
  529. // Reset horizontal position
  530. $x = 0;
  531. // Raise the height-position (bottom-up)
  532. $y++;
  533. // Reached the image-height? Break the for-loop
  534. if ($y>$height)
  535. break;
  536. }
  537. // Calculation of the RGB-pixel (defined as BGR in image-data)
  538. // Define $i_pos as absolute position in the body
  539. $i_pos = $i*2;
  540. $r = hexdec($body[$i_pos+4].$body[$i_pos+5]);
  541. $g = hexdec($body[$i_pos+2].$body[$i_pos+3]);
  542. $b = hexdec($body[$i_pos].$body[$i_pos+1]);
  543. // Calculate and draw the pixel
  544. $color = imagecolorallocate($image,$r,$g,$b);
  545. imagesetpixel($image,$x,$height-$y,$color);
  546. // Raise the horizontal position
  547. $x++;
  548. }
  549. // Unset the body / free the memory
  550. unset($body);
  551. // Return image-object
  552. return $image;
  553. }
  554. } // if(!function_exists('imagecreatefrombmp'))