Tag : tu-pian-he-cheng
Tag : tu-pian-he-cheng
一个php合成图片的工具类
合成顺序从背景->图层依次设置
图片传入参数:路径.url.php图片资源
<?php //样例 $FormationImage = new FormationImage(); $image = imagecreatefrompng('url'); $FormationImage->setBackgroundImage($image); $FormationImage->setImage('图片路径',100,20,100,40,true); $FormationImage->setText('文字',30,50,12,'#000000'); $FormationImage->onDraw(); class FormationImage{ private $backgroundImage; private $resource; private $imageType; private $imagePath; const IMAGE = 1; const TEXT = 2; const PNG = 1; const JPG = 2; const GIF = 3; const FONTDEFAULTSTYLE = './msyh.ttf'; const FONTDEFAULTCOLOR = '#000000'; const FONTDEFAULTSIZE = 12; function __construct(){ $this->backgroundImage = null; $this->resource = array(); $this->imageType = self::PNG; $this->imagePath = null; } public function setBackgroundImage($backgroundImage, $isFile=FALSE,$fileType=self::PNG){ if ($backgroundImage) { if($isFile){ $this->backgroundImage = $this->getImageGd($backgroundImage,$fileType); }else{ $this->backgroundImage = $backgroundImage; } } return $this->backgroundImage; } private function getImageGd($imagePath,$fileType=self::PNG){ $image = null; switch ($fileType) { case self::PNG: $image = imagecreatefrompng($imagePath); break; case self::JPG: $image = imagecreatefromjpeg($imagePath); break; case self::GIF: $image = imagecreatefromgif($imagePath); break; default: $image = null; break; } return $image; } private function getImage($imageGd){ $image = null; if ($this->imagePath) { switch ($fileType) { case self::PNG: imagepng($imageGd,$this->imagePath); break; case self::JPG: imagejpeg($imageGd,$this->imagePath); break; case self::GIF: imagegif($imageGd,$this->imagePath); break; default: break; } }else{ switch ($this->imageType) { case self::PNG: header('Content-Type: image/png'); imagepng($imageGd); break; case self::JPG: header('Content-Type: image/jpeg'); imagejpeg($imageGd); break; case self::GIF: header('Content-Type: image/gif'); imagegif($imageGd); break; default: $image = null; break; } imagedestroy($imageGd); return $image; } } private function getColor($color){ if (strlen($color)!=7) $color = self::FONTDEFAULTCOLOR; $R = hexdec(substr($color,1,2)); $G = hexdec(substr($color,3,2)); $B = hexdec(substr($color,5)); $color = imagecolorallocate($this->backgroundImage, $R, $G, $B); return $color; } public function setText($text,$x,$y,$fontSize=self::FONTDEFAULTSIZE,$fontColor=self::FONTDEFAULTCOLOR,$fontStyle=self::FONTDEFAULTSTYLE){ if ($text) { $this->resource[] = array('type'=>self::TEXT, 'text' => $text, 'x'=>$x, 'y'=>$y, 'size'=>$fontSize, 'color'=>$this->getColor($fontColor), 'style'=>$fontStyle); } return $text; } public function setImage($image,$x,$y, $width, $height, $isFile=FALSE,$fileType=self::PNG){ if ($image) { if ($isFile) { $image = $this->getImageGd($image, $fileType=self::PNG); } $this->resource[] = array('type'=>self::IMAGE, 'image' =>$image ,'x'=>$x, 'y'=>$y, 'width'=>$width, 'height'=>$height); } return $image; } public function setImageType($type){ $this->imageType = $type; } public function setImagePath($imagePath){ $this->imagePath = $imagePath; } public function onDraw(){ imagealphablending($this->backgroundImage, true); if($this->resource){ foreach ($this->resource as $key => $value) { if($value['type'] == self::TEXT){ imagettftext($this->backgroundImage, $value['size'], 0, $value['x'], $value['y'], $value['color'], $value['style'], $value['text']); }elseif($value['type'] == self::IMAGE){ imagecopy($this->backgroundImage, $value['image'], $value['x'], $value['y'], 0, 0, $value['width'],$value['height']); imagedestroy($value['image']); } } } $this->getImage($this->backgroundImage); return true; } } ?> |