first commit
This commit is contained in:
commit
cd91bddd03
|
|
@ -0,0 +1,335 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
Changelog:
|
||||||
|
|
||||||
|
[2026-04-13]: v1.0 - Added the ability to create a webp version from the original without -scaled
|
||||||
|
|
||||||
|
*/
|
||||||
|
/******************** SETTINGS ********************/
|
||||||
|
|
||||||
|
$masterServer = ""; // Leave empty if is a master machine
|
||||||
|
$masterHost = ""; // Leave empty if is a master machine
|
||||||
|
|
||||||
|
header("ta-machine-404: " . php_uname('n') . " - " . date("Y-m-d H:i:s"));
|
||||||
|
|
||||||
|
|
||||||
|
function renderImageFromMaster( $path_parts ){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderAlternativeImageVersion( $path_parts ){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/******************** START ********************/
|
||||||
|
|
||||||
|
$path_parts = pathinfo($_SERVER['REDIRECT_URL']);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Check if is requested the webP version of the image
|
||||||
|
if ( $path_parts["extension"] == "webp" ) {
|
||||||
|
|
||||||
|
// Remove the extension and check if the image exists
|
||||||
|
$imagePath = sprintf("/tauser/www%s/%s",
|
||||||
|
$path_parts["dirname"],
|
||||||
|
$path_parts["filename"]
|
||||||
|
);
|
||||||
|
|
||||||
|
$newImagePath = $imagePath . ".webp";
|
||||||
|
|
||||||
|
if ( file_exists($imagePath) ) {
|
||||||
|
|
||||||
|
// Create the webP version of the image and return it
|
||||||
|
|
||||||
|
$im = imagecreatefromjpeg($imagePath);
|
||||||
|
|
||||||
|
if ( $im == false ) {
|
||||||
|
|
||||||
|
$im = imagecreatefrompng($imagePath);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//Quality of the new webp image. 1-100.
|
||||||
|
//Reduce this to decrease the file size.
|
||||||
|
$quality = 70;
|
||||||
|
|
||||||
|
|
||||||
|
if ( $im != false ) {
|
||||||
|
|
||||||
|
|
||||||
|
//Create the webp image and save it
|
||||||
|
imagewebp($im, $newImagePath, $quality);
|
||||||
|
|
||||||
|
//Clean up / Destroy the image object.
|
||||||
|
imagedestroy($im);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// We need to check if the image is create correctly
|
||||||
|
if ( file_exists($newImagePath) && filesize($newImagePath) > 0 ) {
|
||||||
|
|
||||||
|
header("HTTP/1.1 200 OK", true);
|
||||||
|
header('Status: 200 OK', true);
|
||||||
|
|
||||||
|
//Set the content-type
|
||||||
|
header('Content-Type: image/webp');
|
||||||
|
|
||||||
|
echo file_get_contents($newImagePath);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// There is a problem with the image
|
||||||
|
// We generate the webp by copying the original image
|
||||||
|
copy(
|
||||||
|
$imagePath,
|
||||||
|
$newImagePath
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
// We take the original file and we save it as .webp
|
||||||
|
header("HTTP/1.1 200 OK", true);
|
||||||
|
header('Status: 200 OK', true);
|
||||||
|
|
||||||
|
//Set the content-type
|
||||||
|
header('Content-Type: image/webp');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
echo file_get_contents($newImagePath);
|
||||||
|
|
||||||
|
//var_dump( file_exists($imagePath), $imagePath );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
die();
|
||||||
|
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// The image not exists, we need to get
|
||||||
|
if ( $masterServer != "" && $masterHost != "" ) {
|
||||||
|
|
||||||
|
$masterUrl = $masterServer.$_SERVER['REDIRECT_URL'];
|
||||||
|
|
||||||
|
|
||||||
|
// var_dump($masterUrl, $masterHost);die();
|
||||||
|
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $masterUrl);
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: '.$masterHost));
|
||||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
||||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
|
||||||
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
|
||||||
|
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
|
||||||
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
|
||||||
|
|
||||||
|
$result = curl_exec($ch);
|
||||||
|
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
|
||||||
|
|
||||||
|
// var_dump( $httpcode, $result);die();
|
||||||
|
|
||||||
|
if ($httpcode != 200) {
|
||||||
|
|
||||||
|
$seconds_to_cache = 5;
|
||||||
|
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
|
||||||
|
header("Expires: $ts");
|
||||||
|
header("Pragma: cache");
|
||||||
|
header("Cache-Control: max-age=$seconds_to_cache");
|
||||||
|
echo( $result );
|
||||||
|
die();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
error_log("404.php: request of ".$_SERVER['REDIRECT_URL'], 0);
|
||||||
|
|
||||||
|
header("HTTP/1.1 200 OK", true);
|
||||||
|
header('Status: 200 OK', true);
|
||||||
|
// header("Content-type: image/".(($path_parts["extension"]=="jpg")?"jpeg":$path_parts["extension"]), false);
|
||||||
|
header('Content-Type: image/webp');
|
||||||
|
file_put_contents($newImagePath, $result);
|
||||||
|
|
||||||
|
echo( $result );
|
||||||
|
|
||||||
|
} else {
|
||||||
|
$filenamePartsArr = explode("-", $path_parts["filename"]);
|
||||||
|
if ( count( $filenamePartsArr ) > 0 && preg_match('/\d+x\d+\.\w+/', $filenamePartsArr[ count( $filenamePartsArr ) - 1 ], $match ) ) {
|
||||||
|
$filenamePartsArr_extension = explode(".", $filenamePartsArr[ count($filenamePartsArr)-1 ]);
|
||||||
|
$imagePath_without_dimension = str_replace("-". $filenamePartsArr_extension[0], "", $imagePath);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$imagePath_without_scaled = str_replace("-scaled", "", $imagePath);
|
||||||
|
|
||||||
|
if(file_exists($imagePath_without_scaled)){
|
||||||
|
// Create the webP version of the image and return it
|
||||||
|
$newImagePath = $imagePath . ".webp";
|
||||||
|
$im = imagecreatefromjpeg($imagePath_without_scaled);
|
||||||
|
|
||||||
|
if ( $im == false ) {
|
||||||
|
|
||||||
|
$im = imagecreatefrompng($imagePath_without_scaled);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//The path that we want to save our webp file to.
|
||||||
|
|
||||||
|
|
||||||
|
//Quality of the new webp image. 1-100.
|
||||||
|
//Reduce this to decrease the file size.
|
||||||
|
$quality = 70;
|
||||||
|
|
||||||
|
header("HTTP/1.1 200 OK", true);
|
||||||
|
header('Status: 200 OK', true);
|
||||||
|
//Set the content-type
|
||||||
|
header('Content-Type: image/webp');
|
||||||
|
|
||||||
|
//Create the webp image and save it
|
||||||
|
imagewebp($im, $newImagePath, $quality);
|
||||||
|
|
||||||
|
//Clean up / Destroy the image object.
|
||||||
|
imagedestroy($im);
|
||||||
|
|
||||||
|
echo file_get_contents($newImagePath);
|
||||||
|
|
||||||
|
die();
|
||||||
|
}elseif ( file_exists($imagePath_without_dimension) ) {
|
||||||
|
// Create the webP version of the image and return it
|
||||||
|
$newImagePath = $imagePath . ".webp";
|
||||||
|
$im = imagecreatefromjpeg($imagePath_without_dimension);
|
||||||
|
|
||||||
|
if ( $im == false ) {
|
||||||
|
|
||||||
|
$im = imagecreatefrompng($imagePath_without_dimension);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//The path that we want to save our webp file to.
|
||||||
|
|
||||||
|
|
||||||
|
//Quality of the new webp image. 1-100.
|
||||||
|
//Reduce this to decrease the file size.
|
||||||
|
$quality = 70;
|
||||||
|
|
||||||
|
header("HTTP/1.1 200 OK", true);
|
||||||
|
header('Status: 200 OK', true);
|
||||||
|
//Set the content-type
|
||||||
|
header('Content-Type: image/webp');
|
||||||
|
|
||||||
|
//Create the webp image and save it
|
||||||
|
imagewebp($im, $newImagePath, $quality);
|
||||||
|
|
||||||
|
//Clean up / Destroy the image object.
|
||||||
|
imagedestroy($im);
|
||||||
|
|
||||||
|
echo file_get_contents($newImagePath);
|
||||||
|
|
||||||
|
die();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if ($path_parts["extension"]=="jpg" ||
|
||||||
|
$path_parts["extension"]=="gif" ||
|
||||||
|
$path_parts["extension"]=="png" ||
|
||||||
|
$path_parts["extension"]=="jpeg"
|
||||||
|
) {
|
||||||
|
|
||||||
|
//echo "curl to $masterUrl\n";
|
||||||
|
|
||||||
|
// Is an image, check if are present the resize dimensions
|
||||||
|
$filenamePartsArr = explode("-", $path_parts["filename"]);
|
||||||
|
|
||||||
|
if ( count( $filenamePartsArr ) > 0 ) {
|
||||||
|
|
||||||
|
$newFilename = sprintf("%s.%s",
|
||||||
|
str_replace("-". $filenamePartsArr[ count($filenamePartsArr)-1 ], "", $path_parts["filename"]),
|
||||||
|
$path_parts["extension"]
|
||||||
|
);
|
||||||
|
|
||||||
|
$newFilePath = "/tauser/www" . $path_parts["dirname"] . "/" . $newFilename;
|
||||||
|
|
||||||
|
if ( file_exists($newFilePath) ) {
|
||||||
|
|
||||||
|
// The file exists, return it in the response
|
||||||
|
$seconds_to_cache = 5;
|
||||||
|
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
|
||||||
|
header("Expires: $ts");
|
||||||
|
header("Pragma: cache");
|
||||||
|
header("Cache-Control: max-age=$seconds_to_cache");
|
||||||
|
header("HTTP/1.1 200 OK", true);
|
||||||
|
header('Status: 200 OK', true);
|
||||||
|
header("Content-type: ". mime_content_type($newFilePath) , false);
|
||||||
|
|
||||||
|
echo( file_get_contents($newFilePath) );
|
||||||
|
|
||||||
|
die();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( $masterServer != "" && $masterHost != "" ) {
|
||||||
|
|
||||||
|
$masterUrl = $masterServer.$_SERVER['REDIRECT_URL'];
|
||||||
|
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $masterUrl);
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: '.$masterHost));
|
||||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
||||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
|
||||||
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
|
||||||
|
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
|
||||||
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
|
||||||
|
|
||||||
|
$result = curl_exec($ch);
|
||||||
|
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
|
||||||
|
|
||||||
|
if ($httpcode != 200) {
|
||||||
|
$seconds_to_cache = 5;
|
||||||
|
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
|
||||||
|
header("Expires: $ts");
|
||||||
|
header("Pragma: cache");
|
||||||
|
header("Cache-Control: max-age=$seconds_to_cache");
|
||||||
|
} else {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
header("HTTP/1.1 200 OK", true);
|
||||||
|
header('Status: 200 OK', true);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
error_log("404.php: request of ".$_SERVER['REDIRECT_URL'], 0);
|
||||||
|
|
||||||
|
|
||||||
|
header("Content-type: image/".(($path_parts["extension"]=="jpg")?"jpeg":$path_parts["extension"]), false);
|
||||||
|
|
||||||
|
echo( $result );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// If te request image not exists, check
|
||||||
|
|
||||||
|
|
||||||
|
die();
|
||||||
Loading…
Reference in New Issue