From 067e67e08b48bf0c3cde365a8b69def3571a7bae Mon Sep 17 00:00:00 2001 From: Marvin Flock Date: Fri, 28 Mar 2025 14:32:53 +0100 Subject: [PATCH] chore: outsource more image methods into own file --- .../com_sportsmanager/util/image.php | 431 +++++++++++++++++- 1 file changed, 428 insertions(+), 3 deletions(-) diff --git a/src/structure/components/com_sportsmanager/util/image.php b/src/structure/components/com_sportsmanager/util/image.php index b5e056a..f6a0fd4 100644 --- a/src/structure/components/com_sportsmanager/util/image.php +++ b/src/structure/components/com_sportsmanager/util/image.php @@ -1,6 +1,12 @@ get(SiteApplication::class)->input; + + if (empty($id)) + $id = $jInput->get('id', 0, 'INT'); + $fixed_width = $jInput->get('w', 0, 'INT'); + $fixed_height = $jInput->get('h', 0, 'INT'); + $max_width = $jInput->get('mw', 0, 'INT'); + $max_height = $jInput->get('mh', 0, 'INT'); + $no_cache = $jInput->get('nc', 0, 'INT'); + + $pfad = $sportsmanager_joomla_path . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'sportsmanager' . DIRECTORY_SEPARATOR . $typ . DIRECTORY_SEPARATOR . $id . '.'; + if (File::exists($pfad . 'png')) + $ext = "png"; + else if (File::exists($pfad . 'jpg')) + $ext = "jpg"; + else { + ob_end_clean(); // Wegen UTF-8-Zeichen, die in der ausgabe vorhanden sind + header('HTTP/1.1 404 Not Found'); + die(); + } + + $filemtime = filemtime($pfad . $ext); + $last_modified = gmdate('D, d M Y H:i:s', $filemtime) . " GMT"; + $etag = md5($id . '.' . $ext . $filemtime); + + // Prüfung, ob die im Browsercache vorhandene Datei der hiesigen entspricht + if ($_SERVER['HTTP_IF_NONE_MATCH'] == '"' . $etag . '"' || $last_modified == $_SERVER['HTTP_IF_MODIFIED_SINCE']) { + ob_end_clean(); // Wegen UTF-8-Zeichen, die in der ausgabe vorhanden sind + header('HTTP/1.1 304 Not Modified'); + if ($no_cache) + header("Cache-Control: must-revalidate"); // Bewirkt, dass der Browser jedesmal die Datei neu prüft + else + header("Cache-Control: max-age=2592000"); // Bewirkt, dass nach dreißig Tagen die Datei vom Browser auf eine Aktualisierung geprüft wird. + header("Last-Modified: " . $last_modified); + header('ETag: "' . $etag . '"'); + die(); + } + + $image = $ext == "png" ? imagecreatefrompng($pfad . $ext) : imagecreatefromjpeg($pfad . $ext); + if ($image === false) { + ob_end_clean(); // Wegen UTF-8-Zeichen, die in der ausgabe vorhanden sind + header('HTTP/1.1 404 Not Found'); + die(); + } + + ob_end_clean(); // Wegen UTF-8-Zeichen, die in der ausgabe vorhanden sind + header("Content-type: image/" . ($ext == "png" ? "png" : "jpeg")); + if ($no_cache) + header("Cache-Control: must-revalidate"); // Bewirkt, dass der Browser jedesmal die Datei neu prüft + else + header("Cache-Control: max-age=2592000"); // Bewirkt, dass nach dreißig Tagen die Datei vom Browser auf eine Aktualisierung geprüft wird. + header("Last-Modified: " . $last_modified); + header('ETag: "' . $etag . '"'); + + $width = imagesx($image); + $height = imagesy($image); + + if (($fixed_width == 0 || $width == $fixed_width) && ($fixed_height == 0 || $height == $fixed_height) && ($fixed_width != 0 || $max_width == 0 || $width <= $max_width) && ($fixed_height != 0 || $max_height == 0 || $height <= $max_height)) { + if ($ext == "png") { + imagesavealpha($image, true); + imagepng($image); + } else + imagejpeg($image); + die(); + } + + $new_width = $width; + $new_height = $height; + + if ($max_height > 0 && $new_height > $max_height) { + $new_width = max(round($new_width * $max_height / $new_height), 1); + $new_height = $max_height; + } + if ($max_width > 0 && $new_width > $max_width) { + $new_height = max(round($new_height * $max_width / $new_width), 1); + $new_width = $max_width; + } + + if ($max_width > 0 && (($max_width - $new_width) % 2) == 1) // Toleranz bei nur 1 Pixel Unterschied + $new_width += 1; + if ($max_height > 0 && (($max_height - $new_height) % 2) == 1) // Toleranz bei nur 1 Pixel Unterschied + $new_height += 1; + + $image_resized = imagecreatetruecolor(max($fixed_width, $new_width), max($fixed_height, $new_height)); + $color = $ext == "png" ? imagecolorallocatealpha($image_resized, 0, 0, 0, 127) : imagecolorallocate($image_resized, 64, 64, 64); + imagefill($image_resized, 0, 0, $color); + imagecopyresampled($image_resized, $image, round((imagesx($image_resized) - $new_width) / 2), round((imagesy($image_resized) - $new_height) / 2), 0, 0, $new_width, $new_height, $width, $height); + if ($ext == "png") { + imagesavealpha($image_resized, true); + imagepng($image_resized); + } else + imagejpeg($image_resized); + die(); +} + +function bildLoeschen($typ, $id): void +{ + global $sportsmanager_joomla_path; + $typ_exploded = explode("/", $typ); + $typ = $typ_exploded[0]; + $typ_prefix = count($typ_exploded) > 1 ? $typ_exploded[1] : ""; + $bilder_pfad = $sportsmanager_joomla_path . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'sportsmanager' . DIRECTORY_SEPARATOR . $typ; + $pfad = $bilder_pfad . '/' . $typ_prefix . $id . "."; + if (!is_file($pfad . 'png') && !is_file($pfad . 'jpg')) + return; + $alte_bilder = Folder::files($bilder_pfad, '^' . $typ_prefix . $id . '\.|^' . $typ_prefix . 'I' . $id . 'T'); + foreach ($alte_bilder as $fn) + File::delete($bilder_pfad . DIRECTORY_SEPARATOR . $fn); +} + +function bildIdentisch($typ1, $id1, $typ2, $id2): bool +{ + global $sportsmanager_joomla_path; + $typ1_exploded = explode("/", $typ1); + $typ1 = $typ1_exploded[0]; + $typ1_prefix = count($typ1_exploded) > 1 ? $typ1_exploded[1] : ""; + $bilder_pfad1 = $sportsmanager_joomla_path . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'sportsmanager' . DIRECTORY_SEPARATOR . $typ1; + $pfad1 = $bilder_pfad1 . '/' . $typ1_prefix . $id1 . "."; + if (is_file($pfad1 . "png")) + $ext = "png"; + else if (is_file($pfad1 . "jpg")) + $ext = "jpg"; + else + $ext = ""; + $pfad1 .= $ext; + + $typ2_exploded = explode("/", $typ2); + $typ2 = $typ2_exploded[0]; + $typ2_prefix = count($typ2_exploded) > 1 ? $typ2_exploded[1] : ""; + $bilder_pfad2 = $sportsmanager_joomla_path . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'sportsmanager' . DIRECTORY_SEPARATOR . $typ2; + $pfad2 = $bilder_pfad2 . '/' . $typ2_prefix . $id2 . "." . $ext; + + return files_identical($pfad1, $pfad2); +} + +function teamImage($teamId, $vereinId, $width = 240, $height = 240): ?string +{ + $url = bildURL("mannschaften", $teamId, 0, 0, $width, $height); + + if ($url == null) { + $url = bildURL("vereine", $vereinId, 0, 0, $width, $height); + } + + return $url; +} + +function playerImage($playerId, $gender, $width = 180, $height = 240): ?string +{ + $url = bildURL("spieler", $playerId, $width, $height, 0,0, $gender == 'M' ? 'm' : 'w'); + if ($url == null) { + $url = bildURL("mannschaftsmitglieder", $playerId, $width, $height); + } + + return $url; +} + +#[NoReturn] function spielerbild(): void +{ + $db = getDatabase(); + $jInput = Factory::getContainer()->get(SiteApplication::class)->input; + + $spielernr = $db->escape(trim($jInput->get('spielernr', '', 'RAW'))); + $lizenznr = $db->escape(trim($jInput->get('lizenznr', '', 'RAW'))); + + if (empty($spielernr) && empty($lizenznr)) { + ob_end_clean(); // Wegen UTF-8-Zeichen, die in der ausgabe vorhanden sind + header('HTTP/1.1 404 Not Found'); + die(); + } + + $query = "SELECT spieler_id" + . "\n FROM #__sportsmanager_spieler" + . "\n WHERE NOT ISNULL(aktueller_verein_id) AND NOT bild_ausblenden" . (!empty($spielernr) ? " AND spielernr = '$spielernr'" : " AND lizenznr = '$lizenznr'") + . "\n ORDER BY spieler_id DESC"; + $db->setQuery($query); + if (!$db->execute()) { + die($db->stderr(true)); + } + $rows = $db->loadObjectList(); + if (count($rows) < 1) { + ob_end_clean(); // Wegen UTF-8-Zeichen, die in der ausgabe vorhanden sind + header('HTTP/1.1 404 Not Found'); + die(); + } + + $id = $rows[0]->spieler_id; + bildAnpassen("spieler", $id); +} + +function bildURL($typ, $id, $fixed_width = 0, $fixed_height = 0, $max_width = 0, $max_height = 0, $alternativ = ""): ?string +{ + global $sportsmanager_joomla_path; + global $sportsmanager_joomla_url; + + $typ_exploded = explode("/", $typ); + $typ = $typ_exploded[0]; + $typ_prefix = count($typ_exploded) > 1 ? $typ_exploded[1] : ""; + + $pfad = $sportsmanager_joomla_path . "/images/sportsmanager/" . $typ . "/" . $typ_prefix . $id . "."; + if (is_file($pfad . "png")) + $ext = "png"; + else if (is_file($pfad . "jpg")) + $ext = "jpg"; + else if (!empty($alternativ)) { + $id = $alternativ; + $pfad = $sportsmanager_joomla_path . "/images/sportsmanager/" . $typ . "/" . $typ_prefix . $id . "."; + if (is_file($pfad . "png")) + $ext = "png"; + else if (is_file($pfad . "jpg")) + $ext = "jpg"; + else + return null; + } + else + return null; + + $time = filemtime($pfad . $ext); + + if ($fixed_width > 0 && $fixed_height > 0) { + $pfad_angepasst = $sportsmanager_joomla_path . "/images/sportsmanager/" . $typ . "/" . $typ_prefix . "I" . $id . "T" . $time . "W" . $fixed_width . "H" . $fixed_height . "." . $ext; + if (is_file($pfad_angepasst)) { + return $sportsmanager_joomla_url . 'images/sportsmanager/' . $typ . '/' . basename($pfad_angepasst); + } + } + + $size = getimagesize($pfad . $ext); + $width = $size[0]; + $height = $size[1]; + + $max_width = $fixed_width > 0 ? $fixed_width : $max_width; + $max_height = $fixed_height > 0 ? $fixed_height : $max_height; + + if (($fixed_width == 0 || $width == $fixed_width) && ($fixed_height == 0 || $height == $fixed_height) && ($fixed_width != 0 || $max_width == 0 || $width <= $max_width) && ($fixed_height != 0 || $max_height == 0 || $height <= $max_height)) { + $pfad_angepasst = $sportsmanager_joomla_path . "/images/sportsmanager/" . $typ . "/" . $typ_prefix . "I" . $id . "T" . $time . "W" . $width . "H" . $height . "." . $ext; + if (!is_file($pfad_angepasst)) { + if (!copy($pfad . $ext, $pfad_angepasst)) + return null; + } + + return $sportsmanager_joomla_url . 'images/sportsmanager/' . $typ . '/' . basename($pfad_angepasst); + } + + $new_width = $width; + $new_height = $height; + + if ($max_height > 0 && $new_height > $max_height) { + $new_width = max(round($new_width * $max_height / $new_height), 1); + $new_height = $max_height; + } + if ($max_width > 0 && $new_width > $max_width) { + $new_height = max(round($new_height * $max_width / $new_width), 1); + $new_width = $max_width; + } + + if ($max_width > 0 && (($max_width - $new_width) % 2) == 1) // Toleranz bei nur 1 Pixel Unterschied + $new_width += 1; + if ($max_height > 0 && (($max_height - $new_height) % 2) == 1) // Toleranz bei nur 1 Pixel Unterschied + $new_height += 1; + + $pfad_angepasst = $sportsmanager_joomla_path . "/images/sportsmanager/" . $typ . "/" . $typ_prefix . "I" . $id . "T" . $time . "W" . max($fixed_width, $new_width) . "H" . max($fixed_height, $new_height) . "." . $ext; + + if (!is_file($pfad_angepasst)) { + $image = $ext == "png" ? imagecreatefrompng($pfad . $ext) : imagecreatefromjpeg($pfad . $ext); + if ($image === false) + return null; + + $image_resized = imagecreatetruecolor(max($fixed_width, $new_width), max($fixed_height, $new_height)); + $color = $ext == "png" ? imagecolorallocatealpha($image_resized, 0, 0, 0, 127) : imagecolorallocate($image_resized, 64, 64, 64); + imagefill($image_resized, 0, 0, $color); + imagecopyresampled($image_resized, $image, round((imagesx($image_resized) - $new_width) / 2), round((imagesy($image_resized) - $new_height) / 2), 0, 0, $new_width, $new_height, $width, $height); + if ($ext == "png") { + imagesavealpha($image_resized, true); + imagepng($image_resized, $pfad_angepasst); + } + else + imagejpeg($image_resized, $pfad_angepasst); + } + + return $sportsmanager_joomla_url . 'images/sportsmanager/' . $typ . '/' . basename($pfad_angepasst); +} + +/* + * #resize=250 +#resize=250,250,blue +#resize=250,250,blue +#resize=250,250&sizes=60%,80%,200% +#resize=250,250,cover&sizes=60%,80%,200% +#resize=250,250,fill&sizes=60%,80%,200% +#crop=250,250,center,top +#crop=250,250 +#crop=250,250,center,bottom +#crop=250,250,left +#crop=250,250,right + */ +function yoothemeBild($typ, $id, $alternativ, $resize = '', $zusatz = ""): ?string +{ + global $sportsmanager_joomla_path; + + $typ_exploded = explode("/", $typ); + $typ = $typ_exploded[0]; + $typ_prefix = count($typ_exploded) > 1 ? $typ_exploded[1] : ""; + $pfad = $sportsmanager_joomla_path . "/images/sportsmanager/" . $typ . "/" . $typ_prefix . $id . "."; + + if (is_file($pfad . "png")) + $ext = "png"; + else if (is_file($pfad . "jpg")) + $ext = "jpg"; + else if (!empty($alternativ)) { + $id = $alternativ; + $pfad = $sportsmanager_joomla_path . "/images/sportsmanager/" . $typ . "/" . $typ_prefix . $id . "."; + if (is_file($pfad . "png")) + $ext = "png"; + else if (is_file($pfad . "jpg")) + $ext = "jpg"; + else + return null; + } + else + return null; + + $bildpfad = "images/sportsmanager/" . $typ . "/" . $typ_prefix . $id . "." . $ext; + + return ''; +} + +function bildHTML($typ, $id, $fixed_width = 0, $fixed_height = 0, $max_width = 0, $max_height = 0, $zusatz = "", $alternativ = ""): ?string +{ + global $sportsmanager_joomla_path; + global $sportsmanager_joomla_url; + + $typ_exploded = explode("/", $typ); + $typ = $typ_exploded[0]; + $typ_prefix = count($typ_exploded) > 1 ? $typ_exploded[1] : ""; + + $pfad = $sportsmanager_joomla_path . "/images/sportsmanager/" . $typ . "/" . $typ_prefix . $id . "."; + if (is_file($pfad . "png")) + $ext = "png"; + else if (is_file($pfad . "jpg")) + $ext = "jpg"; + else if (!empty($alternativ)) { + $id = $alternativ; + $pfad = $sportsmanager_joomla_path . "/images/sportsmanager/" . $typ . "/" . $typ_prefix . $id . "."; + if (is_file($pfad . "png")) + $ext = "png"; + else if (is_file($pfad . "jpg")) + $ext = "jpg"; + else + return null; + } + else + return null; + + $time = filemtime($pfad . $ext); + + if ($fixed_width > 0 && $fixed_height > 0) { + $pfad_angepasst = $sportsmanager_joomla_path . "/images/sportsmanager/" . $typ . "/" . $typ_prefix . "I" . $id . "T" . $time . "W" . $fixed_width . "H" . $fixed_height . "." . $ext; + if (is_file($pfad_angepasst)) { + return ''; + } + } + + $size = getimagesize($pfad . $ext); + $width = $size[0]; + $height = $size[1]; + + $max_width = $fixed_width > 0 ? $fixed_width : $max_width; + $max_height = $fixed_height > 0 ? $fixed_height : $max_height; + + if (($fixed_width == 0 || $width == $fixed_width) && ($fixed_height == 0 || $height == $fixed_height) && ($fixed_width != 0 || $max_width == 0 || $width <= $max_width) && ($fixed_height != 0 || $max_height == 0 || $height <= $max_height)) { + $pfad_angepasst = $sportsmanager_joomla_path . "/images/sportsmanager/" . $typ . "/" . $typ_prefix . "I" . $id . "T" . $time . "W" . $width . "H" . $height . "." . $ext; + if (!is_file($pfad_angepasst)) { + if (!copy($pfad . $ext, $pfad_angepasst)) + return null; + } + + return ''; + } + + $new_width = $width; + $new_height = $height; + + if ($max_height > 0 && $new_height > $max_height) { + $new_width = max(round($new_width * $max_height / $new_height), 1); + $new_height = $max_height; + } + if ($max_width > 0 && $new_width > $max_width) { + $new_height = max(round($new_height * $max_width / $new_width), 1); + $new_width = $max_width; + } + + if ($max_width > 0 && (($max_width - $new_width) % 2) == 1) // Toleranz bei nur 1 Pixel Unterschied + $new_width += 1; + if ($max_height > 0 && (($max_height - $new_height) % 2) == 1) // Toleranz bei nur 1 Pixel Unterschied + $new_height += 1; + + $pfad_angepasst = $sportsmanager_joomla_path . "/images/sportsmanager/" . $typ . "/" . $typ_prefix . "I" . $id . "T" . $time . "W" . max($fixed_width, $new_width) . "H" . max($fixed_height, $new_height) . "." . $ext; + + if (!is_file($pfad_angepasst)) { + $image = $ext == "png" ? imagecreatefrompng($pfad . $ext) : imagecreatefromjpeg($pfad . $ext); + if ($image === false) + return null; + + $image_resized = imagecreatetruecolor(max($fixed_width, $new_width), max($fixed_height, $new_height)); + $color = $ext == "png" ? imagecolorallocatealpha($image_resized, 0, 0, 0, 127) : imagecolorallocate($image_resized, 64, 64, 64); + imagefill($image_resized, 0, 0, $color); + imagecopyresampled($image_resized, $image, round((imagesx($image_resized) - $new_width) / 2), round((imagesy($image_resized) - $new_height) / 2), 0, 0, $new_width, $new_height, $width, $height); + if ($ext == "png") { + imagesavealpha($image_resized, true); + imagepng($image_resized, $pfad_angepasst); + } + else + imagejpeg($image_resized, $pfad_angepasst); + } + + return ''; +}