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
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2021 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
function get_default_image() {
$image = imagecreate(50, 50);
$color = imagecolorallocate($image, 250, 50, 50);
imagefill($image, 0, 0, $color);
return $image;
}
/**
* Get image data from db, cache is used
* @param $imageid
* @return array image data from db
*/
function get_image_by_imageid($imageid) {
static $images = [];
if (!isset($images[$imageid])) {
$row = DBfetch(DBselect('SELECT i.* FROM images i WHERE i.imageid='.zbx_dbstr($imageid)));
$images[$imageid] = $row;
}
return $images[$imageid];
}
/**
* Resizes the given image resource to the specified size keeping the original
* proportions of the image.
*
* @param resource $source
* @param int $thumbWidth
* @param int $thumbHeight
*
* @return resource
*/
function imageThumb($source, $thumbWidth = 0, $thumbHeight = 0) {
$srcWidth = imagesx($source);
$srcHeight = imagesy($source);
if ($srcWidth > $thumbWidth || $srcHeight > $thumbHeight) {
if ($thumbWidth == 0) {
$thumbWidth = $thumbHeight * $srcWidth / $srcHeight;
}
elseif ($thumbHeight == 0) {
$thumbHeight = $thumbWidth * $srcHeight / $srcWidth;
}
else {
$a = $thumbWidth / $thumbHeight;
$b = $srcWidth / $srcHeight;
if ($a > $b) {
$thumbWidth = $b * $thumbHeight;
}
else {
$thumbHeight = $thumbWidth / $b;
}
}
if (function_exists('imagecreatetruecolor') && @imagecreatetruecolor(1, 1)) {
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
}
else {
$thumb = imagecreate($thumbWidth, $thumbHeight);
}
// preserve png transparency
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
imagecopyresampled(
$thumb, $source,
0, 0,
0, 0,
$thumbWidth, $thumbHeight,
$srcWidth, $srcHeight);
imagedestroy($source);
$source = $thumb;
}
return $source;
}
/**
* Creates an image from a string preserving PNG transparency.
*
* @param $imageString
*
* @return resource
*/
function imageFromString($imageString) {
$image = imagecreatefromstring($imageString);
// preserve PNG transparency
imagealphablending($image, false);
imagesavealpha($image, true);
return $image;
}
|