Created
March 16, 2012 10:47
-
-
Save quis/2049544 to your computer and use it in GitHub Desktop.
Wordpress responsive image proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ImageProxy { | |
function __construct($request) { | |
$this->sourceFolder = "/wp-content/uploads/"; | |
$this->path = dirname($request); | |
$this->partialFileName = str_replace($this->path."/", "", $request); | |
$this->serve($this->findImage()); | |
} | |
private function getDirectoryListing() { | |
$directoryListing = Array(); | |
if ($handle = opendir($this->sourceFolder.$this->path)) { | |
while (false !== ($file = readdir($handle))) { | |
if ($file === "." || $file === "..") continue; | |
$directoryListing[] = $file; | |
} | |
closedir($handle); | |
} | |
return $directoryListing; | |
} | |
private function findImage() { | |
$allImages = $this->getDirectoryListing($this->path); | |
foreach ($allImages as $image) { | |
if (false !== strpos($image, $this->partialFileName)) { | |
return $this->path."/".$image; | |
} | |
} | |
return false; | |
} | |
private function serve($path) { | |
if (!$path) { | |
header("HTTP/1.0 404 Not Found"); | |
} else { | |
header("Content-Type: image/jpg"); | |
header("HTTP/1.1 301 Moved Permanently"); | |
header("Location: ".$this->sourceFolder.$path); | |
} | |
exit(); | |
} | |
} | |
if (isSet($_GET["path"])) { | |
new ImageProxy($_GET["path"]); | |
} else { | |
header("HTTP/1.0 404 Not Found"); | |
echo "Path not set"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment