static function readfile($basefile = NULL, $h = NULL, $w = NULL, $attachment = FALSE) { $mime = obje::mime($basefile); $file = obje::fsPath($basefile); $attachmentName = basename($file); if (!file_exists($file)) { return controller('_404'); } // print these two headers now since if we can't cache the image we'll return immediately header("Content-Description: File Transfer"); header("Content-type: {$mime}"); if (!is_null($h) || !is_null($w)) { switch ($mime) { case 'image/jpeg': $img = imagecreatefromjpeg($file); $outfunc = 'imagejpeg'; break; case 'image/png': $img = imagecreatefrompng($file); $outfunc = 'imagepng'; break; case 'image/gif': $img = imagecreatefromgif($file); $outfunc = 'imagegif'; break; default: // some other image format, just print it full sized return readfile($file); } $origwidth = imagesx($img); $origheight = imagesy($img); // preserve image aspect ratio while making the image fit within the h & w provided // both h & w given if (!is_null($h) && !is_null($w)) { if ($origwidth > $origheight) { $newwidth = $w; $newheight = floor($origheight / ($origwidth / $w)); } else { $newheight = $h; $newwidth = floor($origwidth / ($origheight / $h)); } } else { if (!is_null($h)) { $newheight = $h; $newwidth = floor($origwidth / ($origheight / $h)); } else { if (!is_null($w)) { $newwidth = $w; $newheight = floor($origheight / ($origwidth / $w)); } } } $cachePath = __DIR__ . "/../cache/{$newheight}x{$newwidth}/" . obje::relPath($basefile); if (file_exists($cachePath)) { $file = $cachePath; } else { $tmpimg = imagecreatetruecolor($newwidth, $newheight); imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $origwidth, $origheight); $dir = dirname($cachePath); @umask(022); if (!is_dir($dir)) { @mkdir($dir, 0775, TRUE); } if (is_dir($dir)) { @$outfunc($tmpimg, $cachePath); } if (file_exists($cachePath)) { $file = $cachePath; } else { return $outfunc($tmpimg); } } } header("Content-Length: " . filesize($file)); if ($attachment) { header("Content-disposition: attachment; filename={$attachmentName}"); } return readfile($file); }
function multimedia($id) { $_GET = array_merge(array('h' => NULL, 'w' => NULL, 'a' => NULL), $_GET); $file = join('/', func_get_args()); return obje::readfile($file, $_GET['h'], $_GET['w'], $_GET['a']); }