Last active
December 13, 2015 16:59
-
-
Save Zenger/4944337 to your computer and use it in GitHub Desktop.
Sometimes when working with WordPress web-sites, when moving them you don't really need to download all the uploads folder.
Place this script in /wp-content and run it once.
It will create a .htaccess file and will take any incoming image requests and render appropriate images.
Currently handles filenames like : myimage-150x150.jpg and myimage10…
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 | |
/* | |
Place this script inside the wp-content folder. | |
Any incoming request for image will be taken over if the requested file doesn't exist. | |
And will setup a .htacces file to take all incoming requests and send them to this file. | |
Run it once to setup. | |
Configure the constants below. | |
*/ | |
/* Setup Constants */ | |
define("MAX_DRAWS", 5); // How many polygons to draw | |
define("TRY_SMART_SIZES", true); // will attempt to get any information on the requested images and assume a size if no size is specified by params or link | |
define("MIN_W", 50); // in case no requested sizes were detected. | |
define("MIN_H", 50); // in case no requested sizes were detected. | |
/* Application, will install itself */ | |
if (empty($_GET)) | |
{ | |
/* Generate .htaccess */ | |
$self = basename(__FILE__); | |
$html = <<<EOT | |
RewriteEngine On | |
RewriteRule ^(.*)\.png$ {$self}?request=$1&ext=png [NC] | |
RewriteRule ^(.*)\.jpg$ {$self}?request=$1&ext=jpg [NC] | |
RewriteRule ^(.*)\.jpeg$ {$self}?request=$1&ext=jpeg [NC] | |
RewriteRule ^(.*)\.gif$ {$self}?request=$1&ext=gif [NC] | |
EOT; | |
$fh = fopen(".htaccess","w"); | |
fwrite($fh, $html); | |
fclose($fh); | |
echo "If you see this message and no errros, everything is setup!"; | |
} | |
else if (isset($_GET['request'])) | |
{ | |
function sendHeader($file, $mime = "image/jpeg") | |
{ | |
header("Content-Type: $mime"); | |
die(file_get_contents($file)); | |
} | |
$r = $_GET['request']; | |
$ext = $_GET['ext']; | |
if ($ext == "png") | |
{ | |
$content = "image/png"; | |
} | |
else if ($ext == "jpg" || $ext == "jpeg") | |
{ | |
$content = "image/jpeg"; | |
} | |
else | |
{ | |
$content = "image/gif"; | |
} | |
$path = str_replace("/", DIRECTORY_SEPARATOR , $r) . "." . $ext; | |
if (file_exists( $path ) ) | |
{ | |
sendHeader($path, $content); | |
} | |
else | |
{ | |
function random_color(){ | |
mt_srand((double)microtime()*1000000); | |
$c = ''; | |
while(strlen($c)<6){ | |
$c .= sprintf("%02X", mt_rand(0, 255)); | |
} | |
return $c; | |
} | |
function create_image($width, $height, $bg_color, $txt_color ) | |
{ | |
//Define the text to show | |
$text = "$width X $height"; | |
//Create the image resource | |
$image = ImageCreate($width, $height); | |
//We are making two colors one for BackGround and one for ForGround | |
$bg_color = ImageColorAllocate($image, base_convert(substr($bg_color, 0, 2), 16, 10), | |
base_convert(substr($bg_color, 2, 2), 16, 10), | |
base_convert(substr($bg_color, 4, 2), 16, 10)); | |
$txt_color = ImageColorAllocate($image,base_convert(substr($txt_color, 0, 2), 16, 10), | |
base_convert(substr($txt_color, 2, 2), 16, 10), | |
base_convert(substr($txt_color, 4, 2), 16, 10)); | |
//Fill the background color | |
ImageFill($image, 0, 0, $bg_color); | |
//Calculating (Actually astimationg :) ) font size | |
$fontsize = ($width>$height)? ($height / 10) : ($width / 10) ; | |
/* Draw Polygones */ | |
for ($i = 0; $i < MAX_DRAWS; $i++) | |
{ | |
$values = array(); | |
$values[] = mt_rand(0, $width); | |
$values[] = mt_rand(0, $height); | |
$values[] = mt_rand(0, $width); | |
$values[] = mt_rand(0, $height); | |
$values[] = mt_rand(0, $width); | |
$values[] = mt_rand(0, $height); | |
$values[] = mt_rand(0, $width); | |
$values[] = mt_rand(0, $height); | |
$values[] = mt_rand(0, $width); | |
$values[] = mt_rand(0, $height); | |
$values[] = mt_rand(0, $width); | |
$values[] = mt_rand(0, $height); | |
$rnd_color = ImageColorAllocate($image, | |
base_convert(substr(random_color(), 0, 2), 16, 10), | |
base_convert(substr(random_color(), 2, 2), 16, 10), | |
base_convert(substr(random_color(), 4, 2), 16, 10)); | |
imagefilledpolygon($image, $values, 6, $rnd_color); | |
} | |
//Write the text .. with some alignment astimations | |
imagestring ($image,$fontsize,($width/2) - ($fontsize * 2.75),($height/2) + ($fontsize* 0.2),$text,$txt_color); | |
//Tell the browser what kind of file is come in | |
header("Content-Type: image/png"); | |
//Output the newly created image in png format | |
imagepng($image); | |
//Free up resources | |
ImageDestroy($image); | |
} | |
if (!isset($_GET['w']) && !isset($_GET['h'])) | |
{ | |
if (preg_match('/\d*x\d*/i', $r , $matches)) | |
{ | |
list($w, $h) = explode('x', $matches[0]); | |
} | |
if (TRY_SMART_SIZES) | |
{ | |
$s = preg_match('/(\d*$|\d*px)/i', $r, $matches); | |
$w = $matches[0]; | |
$h = $matches[0]; | |
} | |
} | |
else | |
{ | |
$w = intval($_GET['w']); | |
$h = intval($_GET['h']); | |
} | |
if ($w < 1) $w = MIN_W; | |
if ($h < 1) $h = MIN_H; | |
create_image($w, $h, 'dfdfdf', '000000'); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment