Php image processing class code sharing

  • 2020-05-10 17:54:52
  • OfStack

At present, only 3 functions have been realized: 1: image zooming, 2: image clipping, 3: image watermarking
In an instantiation, different functionality is achieved by passing a different value to the second parameter
 
<?php 
include "image.class.php"; 
$image=new image("2.png", 1, "300", "500", "5.png"); // Use the image zoom function  
$image=new image("2.png", 2, "0,0", "50,50", "5.png"); // Use the image clipping feature  
$image=new image("2.png", 3, "1.png", "0", "5.png"); // Use image watermarking  
$image->outimage(); 
?> 

PHP code
 
<?php 
/* Known problems: 1. In the image zooming function, use imagecreatetruecolor The function creates a canvas and USES a transparent processing algorithm, however PNG The format of the image cannot be transparent. with imagecreate The create canvas function solves this problem, but zooms out with too few colors  
* 
* 
*type Value:  
*  ( 1 ) : represents the use of the image zooming function. At this point, $value1 Represents the width of the image after scaling, $value2 Represents the height of the image after scaling  
*  ( 2 ) : Is to use the image clipping function. At this point, $value1 The coordinate that represents the starting point of clipping. For example, starting from the origin is" 0,0 "The front is x Shaft is followed by y Axis, in the middle , Space, $value2 Is the width and height of the clipping, which is also" 20 . 20 "Is used in the form of  
*  ( 3 ) : Represents the use of the image watermarking function. At this point, $value1 The file name of the watermark image, $value2 Represents the position of the watermark in the image, yes 10 You can choose between them ,1 Top left, 2 That's the center left, 3 Stands for left and right, 4 Center left, 5 On behalf of the middle, 6 Center right, 7 I'm going to do, 8 Is the bottom, 9 That's the bottom right, 0 It's a random position  
* 
*/ 
class image{ 
private $types; // Function number used, 1 Zoom function for image  2 Clipping function for image  3, Add image watermarking to the image  
private $imgtype;// Image format  
private $image; // Image resources  
private $width;// Image width  
private $height;// Picture height  
private $value1;// According to the report type The different values, $value1 Each represents a different value  
private $value2;// According to the report type The different values, $value2 Each represents a different value  
private $endaddress;// The address after the output + The file name  
function __construct($imageaddress, $types, $value1="", $value2="", $endaddress){ 
$this->types=$types; 
$this->image=$this->imagesources($imageaddress); 
$this->width=$this->imagesizex(); 
$this->height=$this->imagesizey(); 
$this->value1=$value1; 
$this->value2=$value2; 
$this->endaddress=$endaddress; 
} 
function outimage(){ // According to the incoming type Different values output different functions  
switch($this->types){ 
case 1: 
$this->scaling(); 
break; 
case 2: 
$this->clipping(); 
break; 
case 3: 
$this->imagewater(); 
break; 
default: 
return false; 
} 
} 
private function imagewater(){ // Add image watermarking function  
// Function to get the length and width of the watermark file  
$imagearrs=$this->getimagearr($this->value1); 
// The call function calculates where the watermark is loaded  
$positionarr=$this->position($this->value2, $imagearrs[0], $imagearrs[1]); 
// watermarking  
imagecopy($this->image, $this->imagesources($this->value1), $positionarr[0], $positionarr[1], 0, 0, $imagearrs[0], $imagearrs[1]); 
// Call the output method save  
$this->output($this->image); 
} 
private function clipping(){ // Image clipping function  
// Assign the passed values to the variables separately  
list($src_x, $src_y)=explode(",", $this->value1); 
list($dst_w, $dst_h)=explode(",", $this->value2); 
if($this->width < $src_x+$dst_w || $this->height < $src_y+$dst_h){ // The decision is to limit the ability to cut out of the picture  
return false; 
} 
// Create a new canvas resource  
$newimg=imagecreatetruecolor($dst_w, $dst_h); 
// By cutting  
imagecopyresampled($newimg, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $dst_w, $dst_h); 
// Call the output method save  
$this->output($newimg); 
} 
private function scaling(){ // Image zooming function  
// Gets the width and height of the scaling ratio  
$this-> proimagesize(); 
// Scale according to the parameters , And call the output function to save the processed file  
$this->output($this->imagescaling()); 
} 
private function imagesources($imgad){ // Gets the image type and opens the image resource  
$imagearray=$this->getimagearr($imgad); 
switch($imagearray[2]){ 
case 1://gif 
$this->imgtype=1; 
$img=imagecreatefromgif($imgad); 
break; 
case 2://jpeg 
$this->imgtype=2; 
$img=imagecreatefromjpeg($imgad); 
break; 
case 3://png 
$this->imgtype=3; 
$img=imagecreatefrompng($imgad); 
break; 
default: 
return false; 
} 
return $img; 
} 
private function imagesizex(){ // Get image width  
return imagesx($this->image); 
} 
private function imagesizey(){ // Get image height  
return imagesy($this->image); 
} 
private function proimagesize(){ // Calculates the height and width of a scaled image  
if($this->value1 && ($this->width < $this->height)) { // Proportional scaling algorithm  
$this->value1=round(($this->value2/ $this->height)*$this->width); 
}else{ 
$this->value2=round(($this->value1/ $this->width) * $this->height); 
} 
} 
private function imagescaling(){// Image zooming function, return the processed image resources  
$newimg=imagecreatetruecolor($this->value1, $this->value2); 
$tran=imagecolortransparent($this->image);// Processing transparency algorithm  
if($tran >= 0 && $tran < imagecolorstotal($this->image)){ 
$tranarr=imagecolorsforindex($this->image, $tran); 
$newcolor=imagecolorallocate($newimg, $tranarr['red'], $tranarr['green'], $tranarr['blue']); 
imagefill($newimg, 0, 0, $newcolor); 
imagecolortransparent($newimg, $newcolor); 
} 
imagecopyresampled($newimg, $this->image, 0, 0, 0, 0, $this->value1, $this->value2, $this->width, $this->height); 
return $newimg; 
} 
private function output($image){// The output image  
switch($this->imgtype){ 
case 1: 
imagegif($image, $this->endaddress); 
break; 
case 2: 
imagejpeg($image, $this->endaddress); 
break; 
case 3: 
imagepng($image, $this->endaddress); 
break; 
default: 
return false; 
} 
} 
private function getimagearr($imagesou){// Returns an array of image properties  
return getimagesize($imagesou); 
} 
private function position($num, $width, $height){// Returns based on the number passed in 1 Coordinates of position ,$width and $height Represents the width and height of the inserted image respectively  
switch($num){ 
case 1: 
$positionarr[0]=0; 
$positionarr[1]=0; 
break; 
case 2: 
$positionarr[0]=($this->width-$width)/2; 
$positionarr[1]=0; 
break; 
case 3: 
$positionarr[0]=$this->width-$width; 
$positionarr[1]=0; 
break; 
case 4: 
$positionarr[0]=0; 
$positionarr[1]=($this->height-$height)/2; 
break; 
case 5: 
$positionarr[0]=($this->width-$width)/2; 
$positionarr[1]=($this->height-$height)/2; 
break; 
case 6: 
$positionarr[0]=$this->width-$width; 
$positionarr[1]=($this->height-$height)/2; 
break; 
case 7: 
$positionarr[0]=0; 
$positionarr[1]=$this->height-$height; 
break; 
case 8: 
$positionarr[0]=($this->width-$width)/2; 
$positionarr[1]=$this->height-$height; 
break; 
case 9: 
$positionarr[0]=$this->width-$width; 
$positionarr[1]=$this->height-$height; 
break; 
case 0: 
$positionarr[0]=rand(0, $this->width-$width); 
$positionarr[1]=rand(0, $this->height-$height); 
break; 
} 
return $positionarr; 
} 
function __destruct(){ 
imagedestroy($this->image); 
} 
} 
?> 

Related articles: