=================ImageTool.class.php=================
<?PHPclassimagetool{Private $imagePath;//Picture Path Private $outputDir;//output Folder Private $memoryImg;//Memory Image Public function__construct ($imagePath,$outputDir=NULL) { $this->imagepath =$imagePath; $this->outputdir =$outputDir; $this->memoryimg =NULL; } /** * Show images in memory * @param $image*/ Public functionShowImage () {if($this->memoryimg! =NULL) { $info=getimagesize($this-ImagePath); $type= Image_type_to_extension ($info[2],false); Header(' Content-type: '.$info[' MIME ']); $funs= "image{$type}"; $funs($this-memoryimg); Imagedestroy ($this-memoryimg); $this->memoryimg =NULL; } } /** Save the image as a file * @param $image*/ Private functionSaveImage ($image) { $info=getimagesize($this-ImagePath); $type= Image_type_to_extension ($info[2],false); $funs= "image{$type}"; if(Empty($this-OutputDir)) { $funs($image,MD5($this->imagepath). ‘.‘ .$type); } Else { $funs($image,$this->outputdir.MD5($this->imagepath). ‘.‘ .$type); } } /** * Compress picture * @param $width compression width * @param $height post Compression height * @param bool $output output file * @return Resou Rce*/ Public functionCompressimage ($width,$height,$output=false) { $image=NULL; $info=getimagesize($this-ImagePath); $type= Image_type_to_extension ($info[2],false); $fun= "imagecreatefrom{$type}"; $image=$fun($this-ImagePath); $thumbnail= Imagecreatetruecolor ($width,$height); Imagecopyresampled ($thumbnail,$image, 0, 0, 0, 0,$width,$height,$info[0],$info[1]); Imagedestroy ($image); if($output) { $this->saveimage ($thumbnail); } $this->memoryimg =$thumbnail; return $this; } /** * Add text tags to images * * @param $content text content * @param $size font size * @param $font font style * @param bool $ Output file * @return $this*/ Public functionAddtextmark ($content,$size,$font,$output=false) { $info=getimagesize($this-ImagePath); $type= Image_type_to_extension ($info[2],false); $fun= "imagecreatefrom{$type}"; $image=$fun($this-ImagePath); $color= Imagecolorallocatealpha ($image, 0, 0, 0, 80); $posX= Imagesx ($image) -strlen($content) *$size/2; $posY= Imagesy ($image) -$size/1.5; Imagettftext ($image,$size, 0,$posX,$posY,$color,$font,$content); if($output) { $this->saveimage ($image); } $this->memoryimg =$image; return $this; } /** * Add watermark to Image * * @param $watermark watermark Image Path * @param $alpha watermark Transparency (0-100) * @param bool $output output file * @return $this*/ Public functionAddwatermark ($watermark,$alpha,$output=false) { $image _info=getimagesize($this-ImagePath); $image _type= Image_type_to_extension ($image _info[2],false); $image _fun= "imagecreatefrom{$image _type}"; $image=$image _fun($this-ImagePath); $mark _info=getimagesize($watermark); $mark _type= Image_type_to_extension ($mark _info[2],false); $mark _fun= "imagecreatefrom{$mark _type}"; $mark=$mark _fun($watermark); $posX= Imagesx ($image)-Imagesx ($mark); $posY= Imagesy ($image)-Imagesy ($mark); Imagecopymerge ($image,$mark,$posX,$posY, 0, 0,$mark _info[0],$mark _info[1],$alpha); if($output) { $this->saveimage ($image); } $this->memoryimg =$image; return $this; }}
===================Imagetool using =====================
First import the Imagetool tool:
require_once ' ImageTool.class.php ';
Then instantiate the Imagetool object:
$imageTool New Imagetool (' img/oppman.jpeg ', ' out/'); // picture path, output folder
First, generate compressed pictures
$imageTool true); // compression width, compression height, whether to save $imageTool->showimage ();
Second, add text watermark
$imageTool true); // content, size, font, save $imageTool->showimage ();
Third, add image watermark
$imageTool true); // watermark Path, transparency, whether to save $imageTool->showimage ();
Only as temporary image output:
$imageTool->addtextmark (' Fast output ', ' Res/micro.ttf ')->showimage ();
PHP Image Processing Tool class (add watermark and generate thumbnails)