If we intend to make an image gallery, and we would like to show the thumbails of the images, it wouldn't be a wise idea to show the original images with their height & width modified in the
tag, that would result in low image quality & higher download time. The solution to this problem would be creating thumbnails at the server end and display them instead.
This solution can be approached in two ways, you can create the thumbnails once and for all or create thumbnails everytime at runtime.
For this purpose, I wrote a class which will create the thumbnail.
The class:
PHP Code:
/*
* File : thumb.class.php
* Description : Class for creating thumbnails
*/
class Thumb
{
var $filename;
var $img;
var $height;
var $width;
var $ratio = 10;
var $oldim;
var $oldh;
var $oldw;
function filename($file) /* used for setting filename */
{
$this->filename = $file;
}
function setRatio($ratio) /* used for setting ratio */
{
$this->ratio = $ratio;
}
function init() /* creates the GD stream */
{
$this->img = imagecreatetruecolor($this->width,$this->height);
}
function calculateSize() /* to calculate the output size */
{
//$this->info = getimagesize($this->filename); /* returns height,width,type,attribute */
$this->oldim = imagecreatefromjpeg($this->filename);
$this->oldw = imagesx($this->oldim);
$this->oldh = imagesy($this->oldim);
/* code to re-calculate size maintaining the aspect ratio */
$this->width = ($this->oldw/100)*$this->ratio;
$this->height = ($this->oldh/100)*$this->ratio;
$this->init();
return true;
}
function finish()
{
$this->calculateSize();
imagecopyresampled($this->img,$this->oldim,0,0,0,0,$this->width,$this->height,$this->oldw,$this->oldh);
header("Content-Type: image/jpeg");
imagejpeg($this->img);
}
}
This solution can be approached in two ways, you can create the thumbnails once and for all or create thumbnails everytime at runtime.
For this purpose, I wrote a class which will create the thumbnail.
The class:
PHP Code:
/*
* File : thumb.class.php
* Description : Class for creating thumbnails
*/
class Thumb
{
var $filename;
var $img;
var $height;
var $width;
var $ratio = 10;
var $oldim;
var $oldh;
var $oldw;
function filename($file) /* used for setting filename */
{
$this->filename = $file;
}
function setRatio($ratio) /* used for setting ratio */
{
$this->ratio = $ratio;
}
function init() /* creates the GD stream */
{
$this->img = imagecreatetruecolor($this->width,$this->height);
}
function calculateSize() /* to calculate the output size */
{
//$this->info = getimagesize($this->filename); /* returns height,width,type,attribute */
$this->oldim = imagecreatefromjpeg($this->filename);
$this->oldw = imagesx($this->oldim);
$this->oldh = imagesy($this->oldim);
/* code to re-calculate size maintaining the aspect ratio */
$this->width = ($this->oldw/100)*$this->ratio;
$this->height = ($this->oldh/100)*$this->ratio;
$this->init();
return true;
}
function finish()
{
$this->calculateSize();
imagecopyresampled($this->img,$this->oldim,0,0,0,0,$this->width,$this->height,$this->oldw,$this->oldh);
header("Content-Type: image/jpeg");
imagejpeg($this->img);
}
}
Comments
Post a Comment