Created
December 24, 2011 00:39
-
-
Save oranj/1515799 to your computer and use it in GitHub Desktop.
Thumbnailer w/ auto crop
This file contains 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 | |
error_reporting(E_ALL); | |
function getExtension($filename, &$base = NULL) { | |
$a = split('\.', $filename); | |
$ext = array_pop($a); | |
$base = join('.', $a); | |
return strtolower($ext); | |
} | |
function getDimensionsInside($bounding_width, $bounding_height, $source_filename) { | |
list($width, $height, $type, $attributes) = getimagesize($source_filename); | |
$ratio = $width / $height; | |
$bounding_ratio = $bounding_width / $bounding_height; | |
if ($ratio >= $bounding_ratio) { | |
return Array( | |
$bounding_width, // w | |
$bounding_width / $ratio, // h | |
); | |
} else { | |
return Array( | |
$bounding_height * $ratio, | |
$bounding_height, | |
); | |
} | |
} | |
function getDimensionsMaxPixels($num_pixels, $source_filename) { | |
list($width, $height, $type, $attributes) = getimagesize($source_filename); | |
if ($num_pixels > $width * $height) { | |
return Array($width, $height); | |
} | |
$ratio = $width / $height; | |
$res = Array( | |
round(sqrt($num_pixels * $ratio)), | |
round(sqrt($num_pixels) / sqrt($ratio)) | |
); | |
return $res; | |
} | |
function say() { | |
// return; | |
echo "<pre>"; | |
$args = func_get_args(); | |
echo join(', ', $args); | |
echo '</pre><br/>'; | |
exit(); | |
} | |
function resizeImage($target_width, $target_height, $h_focus, $v_focus, $quality, $source_filename, $target_filename) { | |
list($width, $height, $type, $attributes) = getimagesize($source_filename); | |
$h_percent = $target_height / $height; | |
$w_percent = $target_width / $width; | |
$w_to_h = $width / $height; | |
$bound_ratio = $target_width / $target_height; | |
$ext = getExtension($source_filename); | |
$data = file_get_contents($source_filename); | |
ini_set('memory_limit', '80M'); | |
$source = imagecreatefromstring($data); | |
$target = imagecreatetruecolor($target_width, $target_height); | |
if ($w_to_h > $bound_ratio) {//$target_height <= $target_width) { // Taller than wider, use height; | |
$pre_cropped_height = $target_height; | |
$scale = $height / $target_height; | |
$pre_cropped_width = $w_to_h * $pre_cropped_height; | |
} else { // Use width | |
$pre_cropped_width = $target_width; | |
$scale = $width / $target_width; | |
$pre_cropped_height = $pre_cropped_width / $w_to_h; | |
} | |
$x_offset = $scale * ($pre_cropped_width - $target_width ) * $h_focus; | |
$y_offset = $scale * ($pre_cropped_height - $target_height) * $v_focus; | |
# say ($scale, $x_offset, $y_offset, $pre_cropped_width, $pre_cropped_height, $width, $height, $target_width, $target_height); | |
imagecopyresampled($target, $source, 0, 0, $x_offset, $y_offset, $pre_cropped_width, $pre_cropped_height, $width, $height); | |
if (! imagejpeg($target, $target_filename, $quality)) { | |
die ("Error creating file $target_filename"); | |
} | |
return $target; | |
} | |
$filename = basename(__FILE__); | |
$scriptname = basename($_SERVER['SCRIPT_NAME']); | |
if ($filename == $scriptname) { | |
if (! isset($_GET['f'])) { | |
die('4'); | |
} | |
require_once(dirname(__FILE__).'/config.php'); | |
$filename = str_replace('..', '', urldecode($_GET['f'])); | |
$folder = $CONFIG['dirs']['img']; | |
$width = (isset($_GET['w'])?$_GET['w']:200); | |
$height = (isset($_GET['h'])?$_GET['h']:200); | |
if (isset($_GET['fit'])) { | |
list($width, $height) = getDimensionsInside($width, $height, $folder.$filename); | |
} | |
# print_r(Array($width, $height)); | |
$ext = getExtension($filename, $base); | |
$hash = md5($filename.$width.$height).'.'.$ext; | |
$destination = $_SERVER['DOCUMENT_ROOT'].'/cache/'; | |
if (! file_exists($folder.$filename)) { | |
die($folder.$filename.' Does not exist'); | |
} | |
if (file_exists($destination.$hash)) { | |
$img = imagecreatefromjpeg($destination.$hash); | |
} else { | |
$img = resizeImage($width, $height, 0.5, 0.5, 90, $folder.$filename, $destination.$hash); | |
} | |
header('Content-Type: image/jpeg'); | |
imagejpeg($img); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't judge the bad code in here.