4 class Controller_Image extends Controller_Base
6 public function handle($uri)
8 if (!isset($_GET['path'])) {
9 return $this->error('400 Bad request', 'Path missing');
12 $path = $GLOBALS['imagestore']['basedir'] . $_GET['path'];
13 if (!file_exists($path)) {
14 return $this->error('404 Not Found', 'File not found');
17 if (!isset($_GET['w']) && !isset($_GET['h'])) {
18 header('Content-type: image/jpeg');
19 header('Content-length: ' . filesize($path));
20 return readfile($path);
24 $img = imagecreatefromjpeg($path);
26 $oldW = imagesx($img);
27 $oldH = imagesy($img);
29 if (isset($_GET['w']) && isset($_GET['h'])) {
30 //max. bounding box respecting aspect ratio
31 $newW = (int) $_GET['w'];
32 $newH = (int) $_GET['h'];
37 $newOldW = intval($oldW / $rW * $rH);
38 $oldX = $oldW / 2 - $newOldW / 2;
41 $newOldH = intval($oldH / $rH * $rW);
42 $oldY = $oldH / 2 - $newOldH / 2;
45 } else if (isset($_GET['w'])) {
46 $newW = (int) $_GET['w'];
47 $newH = intval($newW / $oldW * $oldH);
49 $newH = (int) $_GET['h'];
50 $newW = intval($newH / $oldH * $oldW);
53 $thumb = imagecreatetruecolor($newW, $newH);
56 $thumb, $img, 0, 0, $oldX, $oldY,
57 $newW, $newH, $oldW, $oldH
61 header('Content-type: image/jpeg');