PHP GD Create Thumbnail
This example shows How to Create Thumbnail in PHP GD.
This example shows How to Create Thumbnail in PHP GD.
<?php
$i =
new imagethumbnail();
$i->open("index.jpg");
$i->setX(100);
header(
"Content-type:
image/jpeg;");
$i->imagejpeg();
class imagethumbnail
{
var $filename;
var $x;
var $y;
var $image;
var $thumbnail;
function imagethumbnail()
{
}
function open($filename)
{
$this->filename
= $filename;
$imageinfo =
array();
$imageinfo =
getimagesize($this->filename,$imageinfo);
$this->old_x
= $imageinfo[0];
$this->old_y
= $imageinfo[1];
switch ($imageinfo[2])
{
case "1":
$this->image =
imagecreatefromgif($this->filename);
break;
case "2":
$this->image =
imagecreatefromjpeg($this->filename);
break;
case "3":
$this->image =
imagecreatefrompng($this->filename);
break;
}
}
function setX($x="")
{
if (isset($x))
{ $this->x = $x;
}
return $this->x;
}
0
function setY($y="")
{
if (isset($y))
{ $this->y = $y;
}
return $this->y;
1
}
function generate()
{
if ($this->x
> 0 and $this->y
> 0) {
2
$new_x =
$this->x;
$new_y =
$this->y;
}
elseif ($this->x
> 0 and $this->x
!= "") {
3
$new_x =
$this->x;
$new_y =
($this->x/$this->old_x)*$this->old_y;
}
else {
4
$new_x =
($this->y/$this->old_y)*$this->old_x;
$new_y =
$this->y;
}
5
$this->thumbnail
= imagecreatetruecolor($new_x,$new_y);
$white =
imagecolorallocate($this->thumbnail,255,255,255);
imagefill(
$this->thumbnail,0,0,$white);
6
imagecopyresampled (
$this->thumbnail,
$this->image, 0,
0, 0,
0, $new_x,
$new_y, $this->old_x,
$this->old_y);
}
function imagegif($filename="")
{
7
if (!isset($this->thumbnail))
{ $this->generate();
}
imagetruecolortopalette(
$this->thumbnail,0,256);
if ($filename=="")
{
8
imagegif(
$this->thumbnail);
}
else {
imagegif(
$this->thumbnail,$filename);
9
}
}
function imagejpeg($filename="",$quality=80)
{
0
if (!isset($this->thumbnail))
{ $this->generate();
}
imagejpeg(
$this->thumbnail,$filename,$quality);
}
1
function imagepng($filename="")
{
if (!isset($this->thumbnail))
{ $this->generate();
}
if ($filename=="")
{
2
imagepng(
$this->thumbnail);
}
else {
imagepng(
$this->thumbnail,$filename);
3
}
}
}
4
?>
After running the program you will get the following output