Skip to content

Instantly share code, notes, and snippets.

@JossWhittle
Created July 24, 2012 12:23
Show Gist options
  • Save JossWhittle/3169661 to your computer and use it in GitHub Desktop.
Save JossWhittle/3169661 to your computer and use it in GitHub Desktop.
For joe.
<?php
$src = '../'.$_GET['url'];
$w = $_GET['w'];
$c = $_GET['c'];
if ($c != 1) {
$c = 0;
}
if ($w % 1 == 0)
{
list($fw, $fh) = getimagesize($src);
$ext = substr($src, strrpos($src, '.'));
header('Content-Type: image/jpeg');
if ($w >= $fw) {
if ($ext == '.png')
{
header('Content-Type: image/png');
}
$fp = fopen($src, 'rb');
}
else
{
$h = $_GET['h'];
if ($h == NULL) {
$ratio = $fh / $fw;
$h = round($w * $ratio);
}
$img = @imagecreatefromjpeg($src) or
$img = @imagecreatefrompng($src) or
$img = false;
if (!$img) {
if ($ext == '.png')
{
header('Content-Type: image/png');
}
$fp = fopen($src, 'rb');
} else {
$file = substr($src, strrpos($src, '/'));
$cache = '../content/cache' . substr($file, 0, strpos($file, '.')).'_'.$w.'_'.$h.'_'.$c.'.jpg';
if(!file_exists($cache) ) {
@mkdir('../content/cache/');
@chmod('../content/cache', 0777);
if ($c == 1) {
$ratio_orig = $fw/$fh;
if ($w/$h > $ratio_orig) {
$new_height = $w/$ratio_orig;
$new_width = $w;
} else {
$new_width = $h*$ratio_orig;
$new_height = $h;
}
$new_width = round($new_width);
$new_height = round($new_height);
$x_mid = $new_width/2;
$y_mid = $new_height/2;
$process = @imagecreatetruecolor($new_width, $new_height);
@imagecopyresampled($process, $img, 0, 0, 0, 0, $new_width, $new_height, $fw, $fh);
$result = @imagecreatetruecolor($w, $h);
@imagecopyresampled($result, $process, 0, 0, ($x_mid-($w/2)), ($y_mid-($h/2)), $w, $h, $w, $h);
@imagejpeg($result, $cache);
@imagedestroy($process);
} else {
$result = @imagecreatetruecolor($w, $h);
@imagecopyresampled($result, $img, 0, 0, 0, 0, $w, $h, $fw, $fh);
@imagejpeg($result, $cache);
}
@imagedestroy($result);
}
$fp = fopen($cache, 'rb');
}
}
fpassthru($fp);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment