/
Img.php
152 lines (139 loc) · 4.73 KB
/
Img.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
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
<?php
/**
* Created by PhpStorm.
* User: ec
* Date: 03.06.15
* Time: 11:42
* Project: gd2-php-ocr
* @author: Evgeny Pynykh bpteam22@gmail.com
*/
namespace bpteam\phpOCR;;
class Img {
public static function open($img)
{
$format = false;
if (strlen($img) < 255 && file_exists($img)) {
$info = getimagesize($img);
$format = $info[2];
}
switch ($format) {
case IMAGETYPE_PNG :
$img = self::openPNG($img);
break;
case IMAGETYPE_JPEG :
$img = imagecreatefromjpeg($img);
break;
case IMAGETYPE_GIF :
$img = imagecreatefromgif($img);
break;
default:
$img = self::openUnknown($img);
break;
}
return $img;
}
protected static function openPNG($file)
{
$tmpImg2 = imagecreatefrompng($file);
$wight = imagesx($tmpImg2);
$height = imagesy($tmpImg2);
$tmpImg = imagecreatetruecolor($wight, $height);
$white = imagecolorallocate($tmpImg, 255, 255, 255);
imagefill($tmpImg, 0, 0, $white);
imagecopy($tmpImg, $tmpImg2, 0, 0, 0, 0, $wight, $height);
imagedestroy($tmpImg2);
return $tmpImg;
}
protected static function openUnknown($img)
{
if ($tmpImg2 = imagecreatefromstring($img)) {
$wight = imagesx($tmpImg2);
$height = imagesy($tmpImg2);
$tmpImg = imagecreatetruecolor($wight, $height);
$white = imagecolorallocate($tmpImg, 255, 255, 255);
imagefill($tmpImg, 0, 0, $white);
imagecopy($tmpImg, $tmpImg2, 0, 0, 0, 0, $wight, $height);
imagedestroy($tmpImg2);
} else {
$tmpImg = imagecreatefromgd($img);
}
return $tmpImg;
}
/**
* Прапорциональное изменение размера изображения
* @param resource $img изображение
* @param int $width ширина
* @param int $height высота
* @return resource
*/
public static function resize($img, $width, $height)
{
$sizeX = imagesx($img);
$sizeY = imagesy($img);
$newImg = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($newImg, Recognizer::$background['red'], Recognizer::$background['green'], Recognizer::$background['blue']);
imagefill($newImg, 0, 0, $white);
if ($sizeX < $sizeY) {
$width = $sizeX * ($height / $sizeY);
} else {
$height = $sizeY * ($width / $sizeX);
}
imagecopyresampled($newImg, $img, 0, 0, 0, 0, $width, $height, $sizeX, $sizeY);
return $newImg;
}
public static function show($img, $extension = 'png', $prefix = 0, $dirToSave = false)
{
//return false;
$home = $_SERVER["DOCUMENT_ROOT"] . '/';
$subDir = str_replace($home, '', __DIR__);
if (!$dirToSave)
$dirToSave = '/' . $subDir .'/example/tmp/';
if (is_array($img)) {
foreach ($img as $key => $value) {
self::show($value, $extension);
}
} else {
$random = rand();
$picName = $dirToSave . 'img' . $prefix . $random . '.' . $extension;
$fileHead = fopen( $home . $picName, 'w+');
fwrite($fileHead, '');
fclose($fileHead);
imagepng($img, $home . $picName, 9);
echo "<img src='" . $picName . "'></br>\n";
}
}
/**
* Подсчитывает средний цвет из массива индексов
* @param resource $img
* @param array $arrayIndexes
* @return array
*/
public static function getMidColorFromIndexes($img, $arrayIndexes)
{
$midColor['red'] = 0;
$midColor['green'] = 0;
$midColor['blue'] = 0;
foreach ($arrayIndexes as $key => $value) {
$color = imagecolorsforindex($img, $key);
$midColor['red'] += $color['red'];
$midColor['green'] += $color['green'];
$midColor['blue'] += $color['blue'];
}
$count = count($arrayIndexes);
$midColor['red'] /= $count;
$midColor['green'] /= $count;
$midColor['blue'] /= $count;
return $midColor;
}
/**
* Вычисляем яркость цвета по его индексу
* @param resource $img
* @param int $colorIndex
* @return int
*/
public static function getBrightnessFromIndex($img, $colorIndex)
{
$color = imagecolorsforindex($img, $colorIndex);
return ($color['red'] + $color['green'] + $color['blue']) / 3;
}
}