Completed
Push — master ( ca0d40...ae4662 )
by Yannick
07:22
created

getImages.php ➔ hexToRGB()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 9.4285
1
<?php
2
3
function hexToRGB($hex) {
4
	$hex = str_replace("#", "", $hex);
5
	$color = array();
6
	if (strlen($hex) == 3) {
7
	    $color['r'] = hexdec(substr($hex, 0, 1) . substr($hex,0,1));
8
	    $color['g'] = hexdec(substr($hex, 1, 1) . substr($hex,1,1));
9
	    $color['b'] = hexdec(substr($hex, 2, 1) . substr($hex,2,1));
10
	} else if (strlen($hex) == 6) {
11
	    $color['r'] = hexdec(substr($hex, 0, 2));
12
	    $color['g'] = hexdec(substr($hex, 2, 2));
13
	    $color['b'] = hexdec(substr($hex, 4, 2));
14
	}
15
	return $color;
16
}
17
18
19
if (!isset($_GET['color']) || $_GET['color'] == '' || !preg_match('/^([a-fA-F0-9]){3}(([a-fA-F0-9]){3})?\b/',$_GET['color'])) { 
20
    exit(0);
21
}
22
$color = $_GET['color'];
23
if (!isset($_GET['filename']) || !preg_match('/^[a-z0-9-_]+\.png$/', strtolower($_GET['filename']))) {
24
    echo "Incorrect filename";
25
    exit(0);
26
}
27
$filename = $_GET['filename'];
28
if (file_exists(dirname(__FILE__).DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.$color.'-'.$filename)) {
29
    header('Content-type: image/png');
30
    readfile(dirname(__FILE__).DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.$color.'-'.$filename);
31
    exit(0);
32
}
33
$original = dirname(__FILE__).DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'aircrafts'.DIRECTORY_SEPARATOR.'new'.DIRECTORY_SEPARATOR.$filename;
34
if (!file_exists($original)) {
35
    echo "File not found";
36
}
37
38
if (extension_loaded('gd') && function_exists('gd_info')) {
39
    $image = imagecreatefrompng($original);
40
    $index = imagecolorexact($image,26,49,81);
41
    if ($index < 0) {
42
	$index = imagecolorexact($image,25,49,79);
43
    }
44
    if ($index < 0) {
45
	$index = imagecolorexact($image,0,0,0);
46
    }
47
    $c = hexToRGB($color);
48
    imagecolorset($image,$index,$c['r'],$c['g'],$c['b']);
49
 /*
1 ignored issue
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
    $ig = imagecolorat($image, 0, 0);
51
    imagecolortransparent($image, $ig);
52
   */
53
54
55
    header('Content-type: image/png');
56
    imagealphablending($image, false);
57
    imagesavealpha($image, true);
58
    imagepng($image);
59
    if (is_writable('cache')) {
60
        imagepng($image,dirname(__FILE__).DIRECTORY_SEPARATOR.'cache/'.$color.'-'.$filename);
61
    }
62
    
63
    imagedestroy($image);
64
} else {
65
    header('Content-type: image/png');
66
    if ($color == 'FF0000') readfile(dirname(__FILE__).DIRECTORY_SEPARATOR.'images/aircrafts/selected/'.$filename);
67
    else readfile(dirname(__FILE__).DIRECTORY_SEPARATOR.'images/aircrafts/'.$filename);
68
}
69
?>
1 ignored issue
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...