Code: Select all
<?php
class ImagesController extends AppController {
var $name = 'Images';
var $uses = array('Image','Home','Rv');
/**
* Outputs an image to the browser if one can be found, otherwise it issues a 404
*/
function view($id = null) {
if ($image = $this->Image->read(null, $id)) {
$this->__display($image['Image']['path'], $image['Image']['mime_type'], $image['Image']['size']);
} else {
header("HTTP/1.0 404 Not Found");
$this->__display(WWW_ROOT . 'img' . DS . 'not-available.png', 'image/png');
}
exit;
}
/**
* Output a thumbnail of a big image
* @todo Issue a 404, but output a No Image image
*/
function thumb($id) {
if ($image = $this->Image->read(null, $id)) {
$parts = pathinfo($image['Image']['path']);
$imagename = WWW_ROOT . 'img' . DS . 'uploads' . DS . 'homes' . DS . 'thumbs' . DS . $parts['basename'];
// if there is not a thumbnail for this image, create one
if(!file_exists($imagename)) {
$img = imagecreatefromjpeg($image['Image']['path']);
$height = 157;
$width = 210;
$rs_image = imagecreatetruecolor($width, $height);
imagecopyresampled($rs_image, $img, 0, 0, 0, 0, $width, $height, 400, 300);
imagejpeg($rs_image, $imagename, 100);
}
Configure::write('debug', 0);
header("HTTP/1.0 200 OK");
header(sprintf("Content-type: %s", $image['Image']['mime_type']));
header(sprintf("Content-length: %d", $image['Image']['size']));
readfile($imagename);
exit;
}
header("HTTP/1.0 404 Not Found");
$this->__display(WWW_ROOT . 'img' . DS . 'not-available-thumb.png', 'image/png');
exit;
}
/**
* Used for displaying an image by its path starting from uploads/
*/
function tmp($path, $type = "text/jpeg", $size = null) {
$this->__display(UPLOAD_DIR . $path, $type, $size);
}
function __display($path, $type = null, $size = null) {
Configure::write('debug', 0);
if (!$type) $type = "text/jpeg"; // best guess
if (!$size) $size = filesize($path);
//header("HTTP/1.0 200 OK"); // removed because I want to be able to issue a 404 and still show an image (a 404 one)
header(sprintf("Content-type: %s", $type));
header(sprintf("Content-length: %d", $size));
readfile($path);
exit;
}
}