/core/barcode/barcodegen.1d-php5.v2.2.0/class/drawer/BCGDrawJPG.php

https://bitbucket.org/unixcrab/colab-lims · PHP · 108 lines · 64 code · 11 blank · 33 comment · 9 complexity · 500dc76cb285190ee43950f951b224de MD5 · raw file

  1. <?php
  2. /**
  3. * BCGDrawJPG.php
  4. *--------------------------------------------------------------------
  5. *
  6. * Image Class to draw JPG images with possibility to set DPI
  7. *
  8. *--------------------------------------------------------------------
  9. * Revision History
  10. * v2.1.0 8 nov 2009 Jean-Sébastien Goupil
  11. *--------------------------------------------------------------------
  12. * $Id: BCGDrawJPG.php,v 1.1 2009/11/09 04:14:37 jsgoupil Exp $
  13. *--------------------------------------------------------------------
  14. * Copyright (C) Jean-Sebastien Goupil
  15. * http://www.barcodephp.com
  16. */
  17. include_once('BCGDraw.php');
  18. if (!function_exists('file_put_contents')) {
  19. function file_put_contents($filename, $data) {
  20. $f = @fopen($filename, 'w');
  21. if (!$f) {
  22. return false;
  23. } else {
  24. $bytes = fwrite($f, $data);
  25. fclose($f);
  26. return $bytes;
  27. }
  28. }
  29. }
  30. class BCGDrawJPG extends BCGDraw {
  31. private $dpi;
  32. private $quality;
  33. /**
  34. * Constructor
  35. *
  36. * @param resource $im
  37. */
  38. public function __construct($im) {
  39. parent::__construct($im);
  40. }
  41. /**
  42. * Sets the DPI
  43. *
  44. * @param int $dpi
  45. */
  46. public function setDPI($dpi) {
  47. if(is_int($dpi)) {
  48. $this->dpi = max(1, $dpi);
  49. } else {
  50. $this->dpi = null;
  51. }
  52. }
  53. /**
  54. * Sets the quality of the JPG
  55. *
  56. * @param int $quality
  57. */
  58. public function setQuality($quality) {
  59. $this->quality = $quality;
  60. }
  61. /**
  62. * Draws the JPG on the screen or in a file
  63. */
  64. public function draw() {
  65. ob_start();
  66. imagejpeg($this->im, null, $this->quality);
  67. $bin = ob_get_contents();
  68. ob_end_clean();
  69. $this->setInternalProperties($bin);
  70. if (empty($this->filename)) {
  71. echo $bin;
  72. } else {
  73. file_put_contents($this->filename, $bin);
  74. }
  75. }
  76. private function setInternalProperties(&$bin) {
  77. $this->internalSetDPI($bin);
  78. $this->internalSetC($bin);
  79. }
  80. private function internalSetDPI(&$bin) {
  81. if($this->dpi !== null) {
  82. $bin = substr_replace($bin, pack("Cnn", 0x01, $this->dpi, $this->dpi), 13, 5);
  83. }
  84. }
  85. private function internalSetC(&$bin) {
  86. if(strcmp(substr($bin, 0, 4), pack('H*', 'FFD8FFE0')) === 0) {
  87. $offset = 4 + (ord($bin[4]) << 8 | ord($bin[5]));
  88. $firstPart = substr($bin, 0, $offset);
  89. $secondPart = substr($bin, $offset);
  90. $cr = pack('H*', 'FFFE004447656E657261746564207769746820426172636F64652047656E657261746F7220666F722050485020687474703A2F2F7777772E626172636F64657068702E636F6D');
  91. $bin = $firstPart;
  92. $bin .= $cr;
  93. $bin .= $secondPart;
  94. }
  95. }
  96. }
  97. ?>