Home >Backend Development >PHP Tutorial >PHP image manipulation class (1/3)_PHP tutorial
php image manipulation class This program can obtain image size, format and other information, and can also perform thumbnail processing and add watermarks to images.
php tutorial image manipulation class
This program can obtain image size, format and other information, and can also perform thumbnail processing on images and add watermarks to images.
1 2 3class image{
public $filename;
private $info;
private $im;
//Construction
public function __construct($filename){
$this->filename = $filename;
$this->info = @getimagesize($filename);
If($this->info[2]>3){echo "Only supports gif, jpeg, png formats.";exit;}
}
//Load
public function imgload(){
switch($this->info[2]){
case 1:
$this->im=@imagecreatefromgif($this->filename);
Break;
Case 2:
$this->im=@imagecreatefromjpeg($this->filename);
Break;
case 3:
$this->im=@imagecreatefrompng($this->filename);
Break;
}
}// Save
public function imagesave($img,$savename,$inf){
switch($inf){
case 1:
Imagegif($img,$savename);;
Break;
Case 2:
Imagejpeg($img,$savename);;
Break;
case 3:
Imagepng($img,$savename);;
Break;
}
//Filter
public function filter($arg=1,$savename=''){
$this->imgload();
if($savename=='')$savename='f_'.$this->filename;
$ok=false;
Switch($arg){
case 1:
Imagefilter($this->im,img_filter_negate);//Invert color
$ok=true;
Break;
case 2:
Imagefilter($this->im,img_filter_grayscale); //Black and white
$ok=true;
Break;
case 3:
Imagefilter($this->im,img_filter_emboss);//Emboss
$ok=true;
Break;
case 4:
Imagefilter($this->im,img_filter_gaussian_blur); //Gaussian blur
$ok=true;
Break;
case 5:
Imagefilter($this->im,img_filter_brightness,50); //Brightness 50
$ok=true;
Break;
case 6:
Imagefilter($this->im,img_filter_contrast,-50); //Contrast-50
$ok=true;
break;
}
if($ok){
$this->imagesave($this->im,$savename,$this->info[2]); //Write file
Imagedestroy($this->im);
Return 1;
}else{
Imagedestroy($this->im);
Return 0;}
}