I have a performance problem in Cake, generating images. The problem is when the controller function is called Cake makes a connection to the DB and the images load in “sync”, waiting to close the last connection (image per image load with a .5 sec wait).
Here is my controller:
class ThumbnailController extends AppController {
var $uses = null;
var $components = array();
var $autoRender = false;
###########################################################
# PUBLIC
###########################################################
public function thumb($folder = null, $w=0, $h=0, $filename=null){
$this->thumbnail("default.jpg", $folder, $w, $h, $filename);
}
public function thumbMama($folder = null, $w=0, $h=0, $filename=null){
$this->thumbnail("iconopersonadh.png", $folder, $w, $h, $filename);
}
###########################################################
# PRIVATE
###########################################################
private function thumbnail($default_image, $folder = null, $w=0, $h=0, $filename=null){
session_write_close();
$img = WWW_ROOT.DS.$folder.DS.$filename;
if(!is_file($img)){
$img = WWW_ROOT.DS."img".DS.$default_image;
}
$img_info = pathinfo($img);
$filename = $img_info["filename"];
$extension = strtolower($img_info["extension"]);
$tn_filename = "tn-".$filename."-".$w."x".$h.".".$extension;
switch($extension){
case "gif":
header('Content-type: image/gif');
break;
case "png":
header('Content-type: image/png');
break;
default:
header('Content-type: image/jpeg');
break;
}
# COMPROBAMOS SI EXISTE UNA IMAGEN CACHEADA PARA NO GENERAR EL RECORTE AL VUELO
if(is_file(WWW_ROOT.DS."files".DS."tn".DS.$tn_filename)){
readfile(WWW_ROOT.DS."files".DS."tn".DS.$tn_filename);
}else{
$magic = new Imagick($img);
$magic->cropThumbnailImage($w, $h);
$magic->writeImage(WWW_ROOT.DS."files".DS."tn".DS.$tn_filename);
echo $magic;
//$this->log($img_info["basename"]." ---> ".$tn_filename, 'thumbnails-'.date("Ym"));
}
}
I need to force CakePHP to not load any DB or make a connection, to force the function to make only the code with no “lag”.
If you put (or already have) this action in a seperate Controller, just put the following in it’s Model to avoid any database operations:
Further details about this are in the Cookbook.