summaryrefslogtreecommitdiffstats
path: root/editors/ckeditor_4_4/uploader/imageresizer.class.php
blob: 0415345ba39f40c1c624ab35e4ef086cac090a2a (plain) (blame)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
//this class is not part of Resize and upload image class.
//this class is property of some one else. 
//I get this from phpclasses.org but forget the name of author.
//sorry for that.

class Image
{
    var $FileName;
    var $FileSize;
    var $FileType;
    var $AllowedExtentions = array("image/png", "image/gif", "image/jpeg", "image/pjpeg", "image/jpg");
    var $newWidth = 100; // new width
    var $newHeight = 100; //new height
    var $TmpName;
    var $PicDir;  //store uploaded images
    var $MaxFileSize = 15000000;  //kbytes
    var $ImageQuality = 95;  // image compression (max value 100)

    function Image($FileName)
    {
        $this->FileName = $FileName;
    }

    function GetInfo()
    {
        $out = '  <br><br>Upload success!<br>
            			Name: ' . $this->FileName . '<br>
                    file size: ' . $this->FileSize . ' byte<br>
                    file type: ' . $this->FileType . '<br>
					<img src=' . dirname($_SERVER['PHP_SELF']) . '/' . $this->PicDir . $this->FileName . '><br><br>';

        return $out;
    }


    function GetFileExtention($FileName)
    {
        if (in_array($this->FileType, $this->AllowedExtentions)) {
            return true;
        } else {
            return false;
        }

    }

    function ExistFile()
    {
        $fileexist = $_SERVER['DOCUMENT_ROOT'] .
            dirname($_SERVER['PHP_SELF']) .
            '/' . $this->PicDir .
            $this->FileName;
        if (file_exists($fileexist)) {
            return true;
        }
    }

    function GetError($error)
    {

        switch ($error) {
            case 0 :
                return "Error: Invalid file type <b>$this->FileType</b>! Allow type: .jpg, .jpeg, .gif, .png  <b>$this->FileName</b><br>";
                break;

            case 1 :
                return "Error: File <b>$this->FileSize</b> is too large! You must upload 1000 MB file<br>";
                break;

            case 2 :
                return "Error: Please, select a file for uploading!<br>";
                break;

            case 3 :
                return "Error: File <b>$this->FileName</b> already exist!<br>";
                break;
        }

    }


    function Resize()
    {
        // header('Content-Type: image/gif');
        if (empty  ($this->TmpName)) {
            return $this->GetError(2);
        } else if ($this->FileSize > $this->MaxFileSize) {
            return $this->GetError(1);
        } else if ($this->GetFileExtention($this->FileName) == false) {
            return $this->GetError(0);
        } else if ($this->ExistFile()) {
            return $this->GetError(3);
        } else {

            $ext = explode(".", $this->FileName);
            $ext = end($ext);
            $ext = strtolower($ext);

            // Get new sizes
            list($width_orig, $height_orig) = getimagesize($this->TmpName);

            $ratio_orig = $width_orig / $height_orig;

            if ($this->newWidth / $this->newHeight > $ratio_orig) {
                $this->newWidth = $this->newHeight * $ratio_orig;
            } else {
                $this->newHeight = $this->newWidth / $ratio_orig;
            }

            $normal = imagecreatetruecolor($this->newWidth, $this->newHeight);

            if ($ext == "jpg") {
                $source = imagecreatefromjpeg($this->TmpName);
            } else if ($ext == "gif") {
                $source = imagecreatefromgif($this->TmpName);
            } else if ($ext == "png") {
                $this->ImageQuality = 9;
                $source = imagecreatefrompng($this->TmpName);
            }

            imagecopyresampled($normal, $source, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $width_orig, $height_orig);


            if ($ext == "jpg") {
                //ob_start();
                imagejpeg($normal, "$this->PicDir/$this->FileName", "$this->ImageQuality");
                //$binaryThumbnail = ob_get_contents();
                //ob_end_clean();
            } else if ($ext == "gif") {
                imagegif($normal, "$this->PicDir/$this->FileName", "$this->ImageQuality");
            } else if ($ext == "png") {
                imagepng($normal, "$this->PicDir/$this->FileName", "$this->ImageQuality");
            }

            imagedestroy($source);

            //echo $this -> GetInfo();

        }

    }

    function Save()
    {
        if (empty  ($this->TmpName)) {
            return $this->GetError(2);
        } else if ($this->FileSize > $this->MaxFileSize) {
            return $this->GetError(1);
        } else if ($this->GetFileExtention($this->FileName) == false) {
            return $this->GetError(0);
        } else if ($this->ExistFile()) {
            return $this->GetError(3);
        } else {
            copy($this->TmpName, "$this->PicDir/$this->FileName"); //direktorij, kamor shranjujemo
        }
    }
}

?>