This article mainly introduces the PHP implementation can add watermark and generate thumbnail image processing tool class, involving PHP for image display, preservation, compression, watermark and other related operations skills, need to friends can refer to the next
The example of this article describes the PHP implementation can add watermarks and generate Thumbnails image processing tool class. Share to everyone for your reference, as follows:
ImageTool.class.php
<?phpclass imagetool{private $imagePath;//Picture path private $outputDir;//output folder private $MEMORYIMG;//Memory Image public Functio n __construct ($imagePath, $outputDir = null) {$this->imagepath = $imagePath; $this->outputdir = $outputDir; $this->memoryimg = null; }/** * Displays images in memory * @param $image */Public Function showImage () {if ($this->memoryimg! = null) {$inf o = 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 picture as a file * @param $image */Private Function SaveImage ($image) {$info = getimagesize ($this->IMAGEP ATH); $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 Compressed width * @param $height Compressed height * @param bool $output output file * @return Resource */Public Function compressimage ($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 output FILE * @return $this */Public Function Addtextmark ($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 Picture * * @param $watermark watermark Picture Path * @param $alpha watermark Transparency (0-100) * @param bool $output output file * @ret Urn $this */public Function Addwatermark ($watermark, $alpha, $output = False) {$image _info = getimagesize ($this-& Gt;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 use
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->compressimage (+, true);//compression width, compression height, whether to save $imagetool->showimage ();
Second, add text watermark
$imageTool->addtextmark (' One Punch Superman ', ' Res/micro.ttf ', true);//content, size, font, whether save $imagetool->showimage ();
Third, add image watermark
$imageTool->addwatermark (' res/logo.jpeg ', +, true);//watermark path, transparency, whether to save $imagetool->showimage ();
Only as temporary image output:
$imageTool->addtextmark (' Fast output ', ' Res/micro.ttf ')->showimage ();
PS: Here again for you to recommend a few more useful image processing tools for everyone to refer to the use:
Online picture crop/build tool:
Http://tools.jb51.net/aideddesign/imgcut
Online Image conversion BASE64 tool:
Http://tools.jb51.net/transcoding/img2base64
ico icon Online generation tool:
Http://tools.jb51.net/aideddesign/ico_img
Online Email icon Making tool:
Http://tools.jb51.net/email/emaillogo
Online image format conversion (jpg/bmp/gif/png) Tool:
Http://tools.jb51.net/aideddesign/picext
Articles you may be interested in:
PHP implements the method of printing binary tree in zigzag order
How to get the binary tree image by PHP
How PHP Gets the countdown k node in the list