1<?php 2/* 3** Zabbix 4** Copyright (C) 2001-2021 Zabbix SIA 5** 6** This program is free software; you can redistribute it and/or modify 7** it under the terms of the GNU General Public License as published by 8** the Free Software Foundation; either version 2 of the License, or 9** (at your option) any later version. 10** 11** This program is distributed in the hope that it will be useful, 12** but WITHOUT ANY WARRANTY; without even the implied warranty of 13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14** GNU General Public License for more details. 15** 16** You should have received a copy of the GNU General Public License 17** along with this program; if not, write to the Free Software 18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19**/ 20 21 22function get_default_image() { 23 $image = imagecreate(50, 50); 24 $color = imagecolorallocate($image, 250, 50, 50); 25 imagefill($image, 0, 0, $color); 26 27 return $image; 28} 29 30/** 31 * Get image data from db, cache is used 32 * @param $imageid 33 * @return array image data from db 34 */ 35function get_image_by_imageid($imageid) { 36 static $images = []; 37 38 if (!isset($images[$imageid])) { 39 $row = DBfetch(DBselect('SELECT i.* FROM images i WHERE i.imageid='.zbx_dbstr($imageid))); 40 $row['image'] = zbx_unescape_image($row['image']); 41 $images[$imageid] = $row; 42 } 43 return $images[$imageid]; 44} 45 46function zbx_unescape_image($image) { 47 global $DB; 48 49 $result = $image ? $image : 0; 50 if ($DB['TYPE'] == ZBX_DB_POSTGRESQL) { 51 $result = pg_unescape_bytea($image); 52 } 53 return $result; 54} 55 56/** 57 * Resizes the given image resource to the specified size keeping the original 58 * proportions of the image. 59 * 60 * @param resource $source 61 * @param int $thumbWidth 62 * @param int $thumbHeight 63 * 64 * @return resource 65 */ 66function imageThumb($source, $thumbWidth = 0, $thumbHeight = 0) { 67 $srcWidth = imagesx($source); 68 $srcHeight = imagesy($source); 69 70 if ($srcWidth > $thumbWidth || $srcHeight > $thumbHeight) { 71 if ($thumbWidth == 0) { 72 $thumbWidth = $thumbHeight * $srcWidth / $srcHeight; 73 } 74 elseif ($thumbHeight == 0) { 75 $thumbHeight = $thumbWidth * $srcHeight / $srcWidth; 76 } 77 else { 78 $a = $thumbWidth / $thumbHeight; 79 $b = $srcWidth / $srcHeight; 80 81 if ($a > $b) { 82 $thumbWidth = $b * $thumbHeight; 83 } 84 else { 85 $thumbHeight = $thumbWidth / $b; 86 } 87 } 88 89 if (function_exists('imagecreatetruecolor') && @imagecreatetruecolor(1, 1)) { 90 $thumb = imagecreatetruecolor($thumbWidth, $thumbHeight); 91 } 92 else { 93 $thumb = imagecreate($thumbWidth, $thumbHeight); 94 } 95 96 // preserve png transparency 97 imagealphablending($thumb, false); 98 imagesavealpha($thumb, true); 99 100 imagecopyresampled( 101 $thumb, $source, 102 0, 0, 103 0, 0, 104 $thumbWidth, $thumbHeight, 105 $srcWidth, $srcHeight); 106 107 imagedestroy($source); 108 $source = $thumb; 109 } 110 return $source; 111} 112 113/** 114 * Creates an image from a string preserving PNG transparency. 115 * 116 * @param $imageString 117 * 118 * @return resource 119 */ 120function imageFromString($imageString) { 121 $image = imagecreatefromstring($imageString); 122 123 // preserve PNG transparency 124 imagealphablending($image, false); 125 imagesavealpha($image, true); 126 return $image; 127} 128