tes'] = $meta['bits'] / 8; $meta['decal'] = 4 - (4 * (($meta['width'] * $meta['bytes'] / 4) - floor($meta['width'] * $meta['bytes'] / 4))); if ($meta['decal'] == 4) { $meta['decal'] = 0; } // obtain imagesize if ($meta['imagesize'] < 1) { $meta['imagesize'] = $meta['filesize'] - $meta['offset']; // in rare cases filesize is equal to offset so we need to read physical size if ($meta['imagesize'] < 1) { $meta['imagesize'] = @filesize($filename) - $meta['offset']; if ($meta['imagesize'] < 1) { trigger_error('imagecreatefrombmp: Can not obtain filesize of ' . $filename . '!', E_USER_WARNING); return false; } } } // calculate colors $meta['colors'] = !$meta['colors'] ? pow(2, $meta['bits']) : $meta['colors']; // read color palette $palette = []; if ($meta['bits'] < 16) { $palette = unpack('l' . $meta['colors'], fread($fh, $meta['colors'] * 4)); // in rare cases the color value is signed if ($palette[1] < 0) { foreach ($palette as $i => $color) { $palette[$i] = $color + 16777216; } } } // ignore extra bitmap headers if ($meta['headersize'] > $bytes_read) { fread($fh, $meta['headersize'] - $bytes_read); } // create gd image $im = imagecreatetruecolor($meta['width'], $meta['height']); $data = fread($fh, $meta['imagesize']); // uncompress data switch ($meta['compression']) { case 1: $data = Helpers::rle8_decode($data, $meta['width']); break; case 2: $data = Helpers::rle4_decode($data, $meta['width']); break; } $p = 0; $vide = chr(0); $y = $meta['height'] - 1; $error = 'imagecreatefrombmp: ' . $filename . ' has not enough data!'; // loop through the image data beginning with the lower left corner while ($y >= 0) { $x = 0; while ($x < $meta['width']) { switch ($meta['bits']) { case 32: case 24: if (!($part = substr($data, $p, 3 /*$meta['bytes']*/))) { trigger_error($error, E_USER_WARNING); return $im; } $color = unpack('V', $part . $vide); break; case 16: if (!($part = substr($data, $p, 2 /*$meta['bytes']*/))) { trigger_error($error, E_USER_WARNING); return $im; } $color = unpack('v', $part); if (empty($meta['rMask']) || $meta['rMask'] != 0xf800) { $color[1] = (($color[1] & 0x7c00) >> 7) * 65536 + (($color[1] & 0x03e0) >> 2) * 256 + (($color[1] & 0x001f) << 3); // 555 } else { $color[1] = (($color[1] & 0xf800) >> 8) * 65536 + (($color[1] & 0x07e0) >> 3) * 256 + (($color[1] & 0x001f) << 3); // 565 } break; case 8: $color = unpack('n', $vide . substr($data, $p, 1)); $color[1] = $palette[$color[1] + 1]; break; case 4: $color = unpack('n', $vide . substr($data, floor($p), 1)); $color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F; $color[1] = $palette[$color[1] + 1]; break; case 1: $color = unpack('n', $vide . substr($data, floor($p), 1)); switch (($p * 8) % 8) { case 0: $color[1] = $color[1] >> 7; break; case 1: $color[1] = ($color[1] & 0x40) >> 6; break; case 2: $color[1] = ($color[1] & 0x20) >> 5; break; case 3: $color[1] = ($color[1] & 0x10) >> 4; break; case 4: $color[1] = ($color[1] & 0x8) >> 3; break; case 5: $color[1] = ($color[1] & 0x4) >> 2; break; case 6: $color[1] = ($color[1] & 0x2) >> 1; break; case 7: $color[1] = ($color[1] & 0x1); break; } $color[1] = $palette[$color[1] + 1]; break; default: trigger_error('imagecreatefrombmp: ' . $filename . ' has ' . $meta['bits'] . ' bits and this is not supported!', E_USER_WARNING); return false; } imagesetpixel($im, $x, $y, $color[1]); $x++; $p += $meta['bytes']; } $y--; $p += $meta['decal']; } fclose($fh); return $im; } /** * Gets the content of the file at the specified path using one of * the following methods, in preferential order: * - file_get_contents: if allow_url_fopen is true or the file is local * - curl: if allow_url_fopen is false and curl is available * * @param string $uri * @param resource $context * @param int $offset * @param int $maxlen * @return string[] */ public static function getFileContent($uri, $context = null, $offset = 0, $maxlen = null) { $content = null; $headers = null; [$protocol] = Helpers::explode_url($uri); $is_local_path = in_array(strtolower($protocol), ["", "file://", "phar://"], true); $can_use_curl = in_array(strtolower($protocol), ["http://", "https://"], true); set_error_handler([self::class, 'record_warnings']); try { if ($is_local_path || ini_get('allow_url_fopen') || !$can_use_curl) { if ($is_local_path === false) { $uri = Helpers::encodeURI($uri); } if (isset($maxlen)) { $result = file_get_contents($uri, false, $context, $offset, $maxlen); } else { $result = file_get_contents($uri, false, $context, $offset); } if ($result !== false) { $content = $result; } if (isset($http_response_header)) { $headers = $http_response_header; } } elseif ($can_use_curl && function_exists('curl_exec')) { $curl = curl_init($uri); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); if ($offset > 0) { curl_setopt($curl, CURLOPT_RESUME_FROM, $offset); } if ($maxlen > 0) { curl_setopt($curl, CURLOPT_BUFFERSIZE, 128); curl_setopt($curl, CURLOPT_NOPROGRESS, false); curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, function ($res, $download_size_total, $download_size, $upload_size_total, $upload_size) use ($maxlen) { return ($download_size > $maxlen) ? 1 : 0; }); } $context_options = []; if (!is_null($context)) { $context_options = stream_context_get_options($context); } foreach ($context_options as $stream => $options) { foreach ($options as $option => $value) { $key = strtolower($stream) . ":" . strtolower($option); switch ($key) { case "curl:curl_verify_ssl_host": curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, !$value ? 0 : 2); break; case "curl:max_redirects": curl_setopt($curl, CURLOPT_MAXREDIRS, $value); break; case "http:follow_location": curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $value); break; case "http:header": if (is_string($value)) { curl_setopt($curl, CURLOPT_HTTPHEADER, [$value]); } else { curl_setopt($curl, CURLOPT_HTTPHEADER, $value); } break; case "http:timeout": curl_setopt($curl, CURLOPT_TIMEOUT, $value); break; case "http:user_agent": curl_setopt($curl, CURLOPT_USERAGENT, $value); break; case "curl:curl_verify_ssl_peer": case "ssl:verify_peer": curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $value); break; } } } $data = curl_exec($curl); if ($data !== false && !curl_errno($curl)) { switch ($http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE)) { case 200: $raw_headers = substr($data, 0, curl_getinfo($curl, CURLINFO_HEADER_SIZE)); $headers = preg_split("/[\n\r]+/", trim($raw_headers)); $content = substr($data, curl_getinfo($curl, CURLINFO_HEADER_SIZE)); break; } } curl_close($curl); } } finally { restore_error_handler(); } return [$content, $headers]; } /** * @param string $str * @return string */ public static function mb_ucwords(string $str): string { $max_len = mb_strlen($str); if ($max_len === 1) { return mb_strtoupper($str); } $str = mb_strtoupper(mb_substr($str, 0, 1)) . mb_substr($str, 1); foreach ([' ', '.', ',', '!', '?', '-', '+'] as $s) { $pos = 0; while (($pos = mb_strpos($str, $s, $pos)) !== false) { $pos++; // Nothing to do if the separator is the last char of the string if ($pos !== false && $pos < $max_len) { // If the char we want to upper is the last char there is nothing to append behind if ($pos + 1 < $max_len) { $str = mb_substr($str, 0, $pos) . mb_strtoupper(mb_substr($str, $pos, 1)) . mb_substr($str, $pos + 1); } else { $str = mb_substr($str, 0, $pos) . mb_strtoupper(mb_substr($str, $pos, 1)); } } } } return $str; } /** * Check whether two lengths should be considered equal, accounting for * inaccuracies in float computation. * * The implementation relies on the fact that we are neither dealing with * very large, nor with very small numbers in layout. Adapted from * https://floating-point-gui.de/errors/comparison/. * * @param float $a * @param float $b * * @return bool */ public static function lengthEqual(float $a, float $b): bool { // The epsilon results in a precision of at least: // * 7 decimal digits at around 1 // * 4 decimal digits at around 1000 (around the size of common paper formats) // * 2 decimal digits at around 100,000 (100,000pt ~ 35.28m) static $epsilon = 1e-8; static $almostZero = 1e-12; $diff = abs($a - $b); if ($a === $b || $diff < $almostZero) { return true; } return $diff < $epsilon * max(abs($a), abs($b)); } /** * Check `$a < $b`, accounting for inaccuracies in float computation. */ public static function lengthLess(float $a, float $b): bool { return $a < $b && !self::lengthEqual($a, $b); } /** * Check `$a <= $b`, accounting for inaccuracies in float computation. */ public static function lengthLessOrEqual(float $a, float $b): bool { return $a <= $b || self::lengthEqual($a, $b); } /** * Check `$a > $b`, accounting for inaccuracies in float computation. */ public static function lengthGreater(float $a, float $b): bool { return $a > $b && !self::lengthEqual($a, $b); } /** * Check `$a >= $b`, accounting for inaccuracies in float computation. */ public static function lengthGreaterOrEqual(float $a, float $b): bool { return $a >= $b || self::lengthEqual($a, $b); } }