A class to simplify the use of gdlib in PHP.
LGPL license
<?php /*
EXAMPLE:
$path='/somepath/to/an/image.jpg';
$newpath = '/some/other/path/for/the/image.jpg';
$image = new image();
//Get image from $path;
$image->byStr($path);
//Resize the image to $width, $height
$image->resize($width,$height);
//Save the image to $newpath
$image->jpeg($newpath);
*/
class image {
var $im;
var $fonts;
//Creates a new class
function __construct($str='')
{
$this->im = null;
if($str!='')
{
$this->byStr($str);
}
$this->fonts = array();
}
//Loads a font
function loadFont($font,$name='')
{
$this->fonts['name']=imageloadfont($font);
}
//Writes a char to the image on position
function char($char,$x,$y,$font=1,$color='')
{
if($color=='')
{
$color = imagecolorallocate ($this->im, 255, 0,0);
}
imagechar($this->im,$font,$x,$y,$char,$color);
}
//get the type of an image: supports only png, jpg and gif.
//$path is the location of the image.
function getType($path)
{
$a = getimagesize($path);
switch($a[2])
{
case 3:
return 'png';
break;
case 2:
return 'jpg';
break;
case 1:
return 'gif';
}
return false;
}
//set the image from a resource.
function byResource($resource)
{
$this->im = $resource;
}
//Sets the image from a path.
function byStr($str)
{
if($str)
{
$a = getimagesize($str);
switch($a[2])
{
case 3:
$this->im =imagecreatefrompng ($str);
break;
case 2:
$this->im =imagecreatefromjpeg ($str);
break;
default:
$this->im =imagecreatefromgif ($str);
}
}
}
function getWidth()
{
return imagesx($this->im);
}
function getHeight()
{
return imagesy($this->im);
}
//Tag the image in the bottom right corner with another image
function tag($path,$x=-1,$y=-1)
{
$im = new rgImage();
$im->byStr($path);
$x = $this->getWidth()-$im->getWidth();
$y = $this->getHeight()-$im->getHeight();
imagecopyresized ($this->im, $im->im, $x, $y, 0, 0, $this->getWidth(), $this->getHeight(), $this->getWidth(), $this->getHeight());
}
//Output or save the image
function gif($path=null)
{
if($path)
{
$h = fopen($path,'w');
fclose($h);
return imagegif($this->im,$path);
chmod($path,0777);
}
else
{
return imagegif($this->im);
}
}
//Output or save the image
function png($path=null)
{
if($path)
{
$h = fopen($path,'w');
fclose($h);
return imagepng($this->im,$path);
chmod($path,0777);
}
else
{
return imagepng($this->im);
}
}
//Output or save the image
function jpeg($path=null)
{
if($path)
{
$h = fopen($path,'w');
fclose($h);
$var = imagejpeg($this->im,$path,100);
chmod($path,0777);
return $var;
}
else
{
return imagejpeg($this->im);
}
}
function jpg($path=null)
{
return $this->jpeg($path);
}
//Return a copy of the image
function copy()
{
$im = imagecreatetruecolor($this->getWidth(),$this->getHeight());
imagecopy ( $im, $this->im, 0, 0, 0, 0, $this->getWidth(), $this->getHeight());
$im1 = new rgImage();
$im1->byResource($im);
return $im1;
}
//Resize the image
function resize($width,$height=null)
{
$proportion = $width / $this->getWidth();
if($height==null)
{
$height = round(($proportion*$this->getHeight()));
}
global $_USER;
$im = imagecreatetruecolor($width,$height); // imagecopyresized ( $im, $this->im, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
imagecopyresampled ($im, $this->im, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->im=$im;
}
} ?>
Sign up to add your own comment here!
|
|