When you do a website , their may arrive situation where you like to display the thumbnail of the orginal images.Here iam explaining how you can display athumb with the height and width you wanted.
For that please include the ollowing code in a file named ThumbController.php in your /app/controller/ folder
<?php
class ThumbController extends AppController{
public $uses = array('SessionManager','Validator','Captcha');
public $components=array('Session');
function init(){
$this->SessionManager->Session=$this->Session;
}
function index(){
$datas = $query=$this->params->query;
ini_set('memory_limit','64M');
$cacheBaseDir = Configure::read('CacheDir'); //Cache Base Directory//
$mediaBaseDir = Configure::read('MediaDir'); //Media Base Directory//
$site_config['document_root'] = $_SERVER['DOCUMENT_ROOT'];
$site_config['path_thumbnail'] = $cacheBaseDir.'cache/';
$thumb_size = 128;
$thumb_size_x = 0;
$thumb_size_y = 0;
$quality = 80;
if (isset($datas["size"]) && $datas["size"]<>0) {
$thumb_size=intval($datas["size"]);
}
if(isset($datas['sizex'])){
$thumb_size_x =$datas['sizex'];
}
if(isset($datas['sizey'])){
$thumb_size_y =$datas['sizey'];
}
if(isset($datas['file'])){
$file = $datas['file'];
}
$subF='images';
if(isset($datas['t'])){
if($datas['t']=='b'){
$subF = 'banners';
}
if($datas['t']=='v'){
$subF = 'videos';
}
}
$filename = $mediaBaseDir.$datas['file'];
$baseCache = explode("/",$datas['file']);
$cacheName = $baseCache[1];
//$filename = $mediaBaseDir.'projects/project-769ac34a132887725692236433110021180.jpg';
$fileextension=substr($filename, strrpos ($filename, ".") + 1);
$cache_file=$cacheBaseDir.$cacheName;
if ((file_exists($cache_file)) && (@filemtime($cache_file)>@filemtime($filename)))
{
header('Content-type: image/'.$fileextension);
header("Expires: Mon, 26 Jul 2030 05:00:00 GMT");
header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size.$thumb_size_x.$thumb_size_y.$quality).'.'.$fileextension));
echo (join('', file( $cache_file )));
exit; # no need to create thumbnail - it already exists in the cache
}
# determine php and gd versions
$ver=intval(str_replace(".","",phpversion()));
if ($ver>=430)
{
$gd_version=@gd_info();
}
# define the right function for the right image types
if (!$image_type_arr = @getimagesize($filename))
{
}
$image_type=$image_type_arr[2];
switch ($image_type)
{
case 2: # JPG
if (!$image = @imagecreatefromjpeg ($filename))
{
# not a valid jpeg file
$image = imagecreatefrompng ($image_error);
$file_type="png";
if (file_exists($cache_file))
{
# remove the cached thumbnail
unlink($cache_file);
}
}
break;
case 3: # PNG
if (!$image = @imagecreatefrompng ($filename))
{
# not a valid png file
$image = imagecreatefrompng ($image_error);
$file_type="png";
if (file_exists($cache_file))
{
# remove the cached thumbnail
unlink($cache_file);
}
}
break;
case 1: # GIF
if (!$image = @imagecreatefromgif ($filename))
{
# not a valid gif file
$image = imagecreatefrompng ($image_error);
$file_type="png";
if (file_exists($cache_file))
{
# remove the cached thumbnail
unlink($cache_file);
}
}
break;
default:
$image = imagecreatefrompng($image_error);
break;
}
# define size of original image
$image_width = imagesx($image);
$image_height = imagesy($image);
# define size of the thumbnail
if (@$thumb_size_x>0)
{
# define images x AND y
$thumb_width = $thumb_size_x;
$factor = $image_width/$thumb_size_x;
$thumb_height = intval($image_height / $factor);
if ($thumb_height>$thumb_size_y)
{
$thumb_height = $thumb_size_y;
$factor = $image_height/$thumb_size_y;
$thumb_width = intval($image_width / $factor);
}
} else {
# define images x OR y
$thumb_width = $thumb_size;
$factor = $image_width/$thumb_size;
$thumb_height = intval($image_height / $factor);
if ($thumb_height>$thumb_size)
{
$thumb_height = $thumb_size;
$factor = $image_height/$thumb_size;
$thumb_width = intval($image_width / $factor);
}
}
# create the thumbnail
if ($image_width < 4000) //no point in resampling images larger than 4000 pixels wide - too much server processing overhead - a resize is more economical
{
if (substr_count(strtolower($gd_version['GD Version']), "2.")>0)
{
//GD 2.0
$thumbnail = ImageCreateTrueColor($thumb_width, $thumb_height);
imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
} else {
//GD 1.0
$thumbnail = imagecreate($thumb_width, $thumb_height);
imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
}
} else {
if (substr_count(strtolower($gd_version['GD Version']), "2.")>0)
{
# GD 2.0
$thumbnail = ImageCreateTrueColor($thumb_width, $thumb_height);
imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
} else {
# GD 1.0
$thumbnail = imagecreate($thumb_width, $thumb_height);
imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
}
}
switch ($image_type)
{
case 2: # JPG
header('Content-type: image/jpeg');
header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size.$thumb_size_x.$thumb_size_y.$quality).'.jpeg'));
@imagejpeg($thumbnail,$cache_file, $quality);
imagejpeg($thumbnail,'',$quality);
break;
case 3: # PNG
header('Content-type: image/png');
header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size.$thumb_size_x.$thumb_size_y.$quality).'.png'));
@imagepng($thumbnail,$cache_file);
imagepng($thumbnail);
break;
case 1: # GIF
if (function_exists('imagegif'))
{
header('Content-type: image/gif');
header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size.$thumb_size_x.$thumb_size_y.$quality).'.gif'));
@imagegif($thumbnail,$cache_file);
imagegif($thumbnail);
} else {
header('Content-type: image/jpeg');
header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size.$thumb_size_x.$thumb_size_y.$quality).'.jpg'));
@imagejpeg($thumbnail,$cache_file);
imagejpeg($thumbnail);
}
break;
}
//clear memory
imagedestroy ($image);
imagedestroy ($thumbnail);
die();
//Thumb//
}
}
?>
And for displaying image you have to call like this in the view folder
<img src="<?php echo $urlRoot; ?>thumb/?file=page/imagename" alt="no image" />
no run your code
For that please include the ollowing code in a file named ThumbController.php in your /app/controller/ folder
<?php
class ThumbController extends AppController{
public $uses = array('SessionManager','Validator','Captcha');
public $components=array('Session');
function init(){
$this->SessionManager->Session=$this->Session;
}
function index(){
$datas = $query=$this->params->query;
ini_set('memory_limit','64M');
$cacheBaseDir = Configure::read('CacheDir'); //Cache Base Directory//
$mediaBaseDir = Configure::read('MediaDir'); //Media Base Directory//
$site_config['document_root'] = $_SERVER['DOCUMENT_ROOT'];
$site_config['path_thumbnail'] = $cacheBaseDir.'cache/';
$thumb_size = 128;
$thumb_size_x = 0;
$thumb_size_y = 0;
$quality = 80;
if (isset($datas["size"]) && $datas["size"]<>0) {
$thumb_size=intval($datas["size"]);
}
if(isset($datas['sizex'])){
$thumb_size_x =$datas['sizex'];
}
if(isset($datas['sizey'])){
$thumb_size_y =$datas['sizey'];
}
if(isset($datas['file'])){
$file = $datas['file'];
}
$subF='images';
if(isset($datas['t'])){
if($datas['t']=='b'){
$subF = 'banners';
}
if($datas['t']=='v'){
$subF = 'videos';
}
}
$filename = $mediaBaseDir.$datas['file'];
$baseCache = explode("/",$datas['file']);
$cacheName = $baseCache[1];
//$filename = $mediaBaseDir.'projects/project-769ac34a132887725692236433110021180.jpg';
$fileextension=substr($filename, strrpos ($filename, ".") + 1);
$cache_file=$cacheBaseDir.$cacheName;
if ((file_exists($cache_file)) && (@filemtime($cache_file)>@filemtime($filename)))
{
header('Content-type: image/'.$fileextension);
header("Expires: Mon, 26 Jul 2030 05:00:00 GMT");
header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size.$thumb_size_x.$thumb_size_y.$quality).'.'.$fileextension));
echo (join('', file( $cache_file )));
exit; # no need to create thumbnail - it already exists in the cache
}
# determine php and gd versions
$ver=intval(str_replace(".","",phpversion()));
if ($ver>=430)
{
$gd_version=@gd_info();
}
# define the right function for the right image types
if (!$image_type_arr = @getimagesize($filename))
{
}
$image_type=$image_type_arr[2];
switch ($image_type)
{
case 2: # JPG
if (!$image = @imagecreatefromjpeg ($filename))
{
# not a valid jpeg file
$image = imagecreatefrompng ($image_error);
$file_type="png";
if (file_exists($cache_file))
{
# remove the cached thumbnail
unlink($cache_file);
}
}
break;
case 3: # PNG
if (!$image = @imagecreatefrompng ($filename))
{
# not a valid png file
$image = imagecreatefrompng ($image_error);
$file_type="png";
if (file_exists($cache_file))
{
# remove the cached thumbnail
unlink($cache_file);
}
}
break;
case 1: # GIF
if (!$image = @imagecreatefromgif ($filename))
{
# not a valid gif file
$image = imagecreatefrompng ($image_error);
$file_type="png";
if (file_exists($cache_file))
{
# remove the cached thumbnail
unlink($cache_file);
}
}
break;
default:
$image = imagecreatefrompng($image_error);
break;
}
# define size of original image
$image_width = imagesx($image);
$image_height = imagesy($image);
# define size of the thumbnail
if (@$thumb_size_x>0)
{
# define images x AND y
$thumb_width = $thumb_size_x;
$factor = $image_width/$thumb_size_x;
$thumb_height = intval($image_height / $factor);
if ($thumb_height>$thumb_size_y)
{
$thumb_height = $thumb_size_y;
$factor = $image_height/$thumb_size_y;
$thumb_width = intval($image_width / $factor);
}
} else {
# define images x OR y
$thumb_width = $thumb_size;
$factor = $image_width/$thumb_size;
$thumb_height = intval($image_height / $factor);
if ($thumb_height>$thumb_size)
{
$thumb_height = $thumb_size;
$factor = $image_height/$thumb_size;
$thumb_width = intval($image_width / $factor);
}
}
# create the thumbnail
if ($image_width < 4000) //no point in resampling images larger than 4000 pixels wide - too much server processing overhead - a resize is more economical
{
if (substr_count(strtolower($gd_version['GD Version']), "2.")>0)
{
//GD 2.0
$thumbnail = ImageCreateTrueColor($thumb_width, $thumb_height);
imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
} else {
//GD 1.0
$thumbnail = imagecreate($thumb_width, $thumb_height);
imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
}
} else {
if (substr_count(strtolower($gd_version['GD Version']), "2.")>0)
{
# GD 2.0
$thumbnail = ImageCreateTrueColor($thumb_width, $thumb_height);
imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
} else {
# GD 1.0
$thumbnail = imagecreate($thumb_width, $thumb_height);
imagecopyresized($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
}
}
switch ($image_type)
{
case 2: # JPG
header('Content-type: image/jpeg');
header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size.$thumb_size_x.$thumb_size_y.$quality).'.jpeg'));
@imagejpeg($thumbnail,$cache_file, $quality);
imagejpeg($thumbnail,'',$quality);
break;
case 3: # PNG
header('Content-type: image/png');
header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size.$thumb_size_x.$thumb_size_y.$quality).'.png'));
@imagepng($thumbnail,$cache_file);
imagepng($thumbnail);
break;
case 1: # GIF
if (function_exists('imagegif'))
{
header('Content-type: image/gif');
header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size.$thumb_size_x.$thumb_size_y.$quality).'.gif'));
@imagegif($thumbnail,$cache_file);
imagegif($thumbnail);
} else {
header('Content-type: image/jpeg');
header('Content-Disposition: inline; filename='.str_replace('/','',md5($filename.$thumb_size.$thumb_size_x.$thumb_size_y.$quality).'.jpg'));
@imagejpeg($thumbnail,$cache_file);
imagejpeg($thumbnail);
}
break;
}
//clear memory
imagedestroy ($image);
imagedestroy ($thumbnail);
die();
//Thumb//
}
}
?>
And for displaying image you have to call like this in the view folder
<img src="<?php echo $urlRoot; ?>thumb/?file=page/imagename" alt="no image" />
no run your code
No comments:
Post a Comment