php img imagecreatefromjpeg tmp fmQPg3ubOC0 jpg img imagecreatefrompng

 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
<?php
$img = imagecreatefromjpeg("/tmp/fmQPg3ubOC0.jpg");
$img = imagecreatefrompng("http://flaticons.net/gd/download-png.php?size=256&padding=0&icon=icons/Sports/Heart.png&in=Heart.png&bgShape=&bgColorR=255&bgColorG=255&bgColorB=255&iconColorR=255&iconColorG=41&iconColorB=248");
$img = image_resize($img, 19, 19);
imagedither($img);
imagepng($img, "/tmp/out.png");
function imagedither($img) {
$w = imagesx($img);
$h = imagesy($img);
$lum_data = array();
for ($y = 0; $y < $h; ++$y) {
for ($x = 0; $x < $w; ++$x) {
$color = imagecolorat($img, $x, $y);
$a = $color >> 24 & 0xFF;
if ($a > 0) {
$r = $g = $b = 0xFF;
} else {
$r = $color >> 16 & 0xFF;
$g = $color >> 8 & 0xFF;
$b = $color & 0xFF;
}
$lum_data[] = floor($r * 0.299 + $g * 0.587 + $b * 0.114);
}
}
$pixels = count($lum_data);
for ($i = 0; $i < $pixels; ++$i) {
$new_pixel = $lum_data[$i] < 129 ? 0 : 0xFF;
$err = floor(($lum_data[$i] - $new_pixel) / 16);
$lum_data[$i] = $new_pixel;
if ($i + 1 < $pixels) {
$lum_data[$i + 1] += $err * 7;
if ($i + $w - 1 < $pixels) {
$lum_data[$i + $w - 1] += $err * 3;
if ($i + $w < $pixels) {
$lum_data[$i + $w] += $err * 5;
if ($i + $w + 1 < $pixels)
$lum_data[$i + $w + 1] += $err * 1;
}
}
}
}
for ($i = 0; $i < $pixels; ++$i) {
$y = floor($i / $w);
$x = $i - $y * $w;
$c = $lum_data[$i] < 129 ? 0 : 0xFFFFFF;
imageline($img, $x, $y, $x, $y, $c);
}
}
function image_resize($img, $width, $height) {
$new_img = imagecreatetruecolor($width, $height);
imagecopyresampled($new_img, $img, 0, 0, 0, 0, $width, $height, imagesx($img), imagesy($img));
return $new_img;
}