/var/www/hkosl.com/oneMotorChunHing_delete20240201/html/bcclass/drawer/BCGDrawJPG.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
 * BCGDrawJPG.php
 *--------------------------------------------------------------------
 *
 * Image Class to draw JPG images with possibility to set DPI
 *
 *--------------------------------------------------------------------
 * Revision History
 * v2.1.0    8  nov    2009    Jean-Sébastien Goupil
 *--------------------------------------------------------------------
 * $Id: BCGDrawJPG.php,v 1.1 2009/11/09 04:14:37 jsgoupil Exp $
 *--------------------------------------------------------------------
 * Copyright (C) Jean-Sebastien Goupil
 * http://www.barcodephp.com
 */
include_once('BCGDraw.php');

if (!
function_exists('file_put_contents')) {
    function 
file_put_contents($filename$data) {
        
$f = @fopen($filename'w');
        if (!
$f) {
            return 
false;
        } else {
            
$bytes fwrite($f$data);
            
fclose($f);
            return 
$bytes;
        }
    }
}

class 
BCGDrawJPG extends BCGDraw {
    private 
$dpi;
    private 
$quality;

    
/**
     * Constructor
     *
     * @param resource $im
     */
    
public function __construct($im) {
        
parent::__construct($im);
    }

    
/**
     * Sets the DPI
     *
     * @param int $dpi
     */
    
public function setDPI($dpi) {
        if(
is_int($dpi)) {
            
$this->dpi max(1$dpi);
        } else {
            
$this->dpi null;
        }
    }

    
/**
     * Sets the quality of the JPG
     *
     * @param int $quality
     */
    
public function setQuality($quality) {
        
$this->quality $quality;
    }

    
/**
     * Draws the JPG on the screen or in a file
     */
    
public function draw() {
        
ob_start();
        
imagejpeg($this->imnull$this->quality);
        
$bin ob_get_contents();
        
ob_end_clean();

        
$this->setInternalProperties($bin);

        if (empty(
$this->filename)) {
            echo 
$bin;
        } else {
            
file_put_contents($this->filename$bin);
        }
    }

    private function 
setInternalProperties(&$bin) {
        
$this->internalSetDPI($bin);
        
$this->internalSetC($bin);
    }

    private function 
internalSetDPI(&$bin) {
        if(
$this->dpi !== null) {
            
$bin substr_replace($binpack("Cnn"0x01$this->dpi$this->dpi), 135);
        }
    }

    private function 
internalSetC(&$bin) {
        if(
strcmp(substr($bin04), pack('H*''FFD8FFE0')) === 0) {
            
$offset + (ord($bin[4]) << ord($bin[5]));
            
$firstPart substr($bin0$offset);
            
$secondPart substr($bin$offset);
            
$cr pack('H*''FFFE004447656E657261746564207769746820426172636F64652047656E657261746F7220666F722050485020687474703A2F2F7777772E626172636F64657068702E636F6D');
            
$bin $firstPart;
            
$bin .= $cr;
            
$bin .= $secondPart;
        }
    }
}
?>