img-resizen

Gesponsorde koppelingen

PHP script bestanden

  1. img-resizen

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php

// image.php?mode=<scale_mode>&img=<image_name>&height=<canvas_height>&width=<canvas_width>&quality=<image_quality>&bw=<image_blackwhite>

// <canvas_height>                        niet opgegeven: waarde is waarde originele afbeelding.
// <canvas_width>                        niet opgegeven: waarde is waarde originele afbeelding.
// <image_name>                         naam (of eventueel path + naam)
// <image_quality>                       kwaliteit jpg van 1-100;  niet opgegeven: waarde is default waarde hieronder.
// <image_blackwhite>                   true or false (default = false) afbeelding in zwartwit.
// <scale_mode> = "crop" of "fit"        fit: opgegeven img past geheel binnen canvas,    
//                                        crop: img wordt uitgevuld op canvas, uitstekende delen worden gecropt.


// defaults

$quality = 70;
$mode = "crop"; // mode is "crop" of "fit"
$bw = false;

// The file
$filename = htmlentities($_GET['img']);
//if(!is_file($filename)){ exit("no file"); }
//de controle hier boven ken je aan of uit zetten net wat je wilt :)

// Get original dimensions

if( !(list($width, $height, $type, $attr) = @getimagesize($filename)) ){ exit; }

// overwrite default stettings
if(!empty($_GET['quality']) && is_numeric($_GET['quality']) && $_GET['quality'] > 0 && $_GET['quality'] <= 100){ $quality = htmlentities($_GET['quality']); }
if(isset($_GET['mode'])){ $mode = htmlentities($_GET['mode']); }
if($_GET['bw'] === "true"){ $bw = true; }

// new canvas size
$thumb_height = $height;
$thumb_width = $width;
if(!empty($_GET['height']) && is_numeric($_GET['height']) && $_GET['height'] < $height && $_GET['height'] > 19){ $thumb_height = htmlentities($_GET['height']); }
if(!empty($_GET['width']) && is_numeric($_GET['width']) && $_GET['width'] < $width && $_GET['width'] > 19){ $thumb_width = htmlentities($_GET['width']); }


        
    //Creates yiq function
    function yiq($r,$g,$b)
    {

        return (($r*0.299)+($g*0.587)+($b*0.114));
    }

    

//////////////////////////////////////////////////
// ---------------- rescale  --------------------


if($mode == "crop"){

    // scale and crop to canvas
    if($height/$width < $thumb_height/$thumb_width){
        // original image is wider than canvas
        $new_height = $thumb_height;
        $new_width = $width * ($thumb_height/$height);
        $new_y = 0;
        $new_x = ($new_width - $thumb_width)/2;
    }
else{
        // original image is higher than canvas
        $new_width = $thumb_width;
        $new_height = $height * ($thumb_width/$width);
        $new_x = 0;
        $new_y = ($new_height - $thumb_height)/2;
    }
    
}
else{
    // mode is "fit"
    
    // fit to canvas

    if($height/$width < $thumb_height/$thumb_width){
        // original image is wider than canvas
        $new_width = $thumb_width;
        $new_height = ($thumb_width/$width) * $height;
        $thumb_height = $new_height;
        $new_x = 0;
        $new_y = 0;
    }
else{
        // original image is higher than canvas
        $new_height = $thumb_height;
        $new_width = ($thumb_height/$height) * $width;
        $thumb_width = $new_width;
        $new_y = 0;
        $new_x = 0;
    }
}


// ---------------- rescale  --------------------
//////////////////////////////////////////////////



if($type == 1){ // img is GIF
    // Content type

    header("Content-type: image/gif");
    
    // Resample
    $image_p = imagecreatetruecolor($thumb_width, $thumb_height);
    $image = imagecreatefromgif($filename);
    imagecopyresampled($image_p, $image, 0, 0, $new_x, $new_y, $new_width, $new_height, $width, $height);
    
    // Output
    imagegif($image_p, null, $quality);

}
elseif($type == 2){ // img is JPG
    // Content type

    header('Content-type: image/jpeg');
    
    // Resample
    $image_p = imagecreatetruecolor($thumb_width, $thumb_height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, $new_x, $new_y, $new_width, $new_height, $width, $height);
    
    // create black and white image
    if($bw && $thumb_width < 800 && $thumb_height < 800){
                
        // Creating the Canvas
        $bwimage= imagecreatetruecolor($thumb_width, $thumb_height);
        
        //Creates the 256 color palette
        for ($c=0;$c<256;$c++)
        {

            $palette[$c] = imagecolorallocate($bwimage,$c,$c,$c);
        }

        
        //Reads the origonal colors pixel by pixel
        for ($y=0;$y<$thumb_height;$y++)
        {

            for ($x=0;$x<$thumb_width;$x++)
            {

                $rgb = imagecolorat($image_p,$x,$y);
                $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >> 8) & 0xFF;
                $b = $rgb & 0xFF;
                
                //This is where we actually use yiq to modify our rbg values, and then convert them to our grayscale palette
                $gs = yiq($r,$g,$b);
                imagesetpixel($bwimage,$x,$y,$palette[$gs]);
            }
        }

    
        // black and white Output
        imagejpeg($bwimage, null, $quality);
        
        
    }
else{
    
        // color Output
        imagejpeg($image_p, null, $quality);
        
    }
    
}


?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.