您好,欢迎访问一九零五行业门户网

php 图片处理类(附实例)

分享一个php实现的图片处理类,可以设置文字水印与图片水印等,有需要的朋友参考下。本节分享一个图片处理类,简单实现了文字水印与图片水印,是学习php图片操作的小例子。
代码:
image = new imagick($tplimage); } /** * 设置文本属性 * * @param string $stext text to print on the image (i.e. buy 1 get 1 free ) * @param integer $x text to print from x codinates * @param integer $y text to print from y codinates * @param integer $font text size for printing * @param string $color text color for print * @param integer $text_anglerotate text from 0-360 * @param string $font_style installed font name and path (i.e /usr/share/fonts/liberation/liberationsans-italic.ttf) * @creating an array of text properties */ public function settext($stext, $x = 0, $y = 0, $font = 12, $color = 'black', $text_angle = 0, $font_style = './liberationsans-italic.ttf') { $this->atextdata[] = array(text=>$stext, font_color=>$color, font_size=>$font,x=>$x,y=>$y, font_style=>$font_style, text_angle=>$text_angle); } /** * 设置图片属性 * * @param string $simage text to print on the image (i.e. /home/httpd/images/brand.jpg ) * @param integer $x text to print from x codinates * @param integer $y text to print from y codinates * @param integer $text_anglerotate text from 0-180 * @creating an array of image properties */ public function setimage($simage, $x = 0, $y = 0, $angle=0) { $this->aimagedata[] = array(image=>$simage, x=>$x, y=>$y, angle=>$angle); } /** * 从文字和图片属性生成最终图像 * * @param string $simage output image name * @return boolean returns true on success and false upon failure */ public function generateimage($simage) { foreach ($this->aimagedata as $aimagevalue) { if (!trim($aimagevalue[image])) { $serror = 1; break; } $oimg = new imagick($aimagevalue[image]); $oimg->rotateimage(transparent, $aimagevalue[angle]); $this->image->compositeimage($oimg, $oimg->getimagecompose(), $aimagevalue[x], $aimagevalue[y]); unset($oimg); } foreach ($this->atextdata as $atextvalue){ if (!trim($atextvalue['text'])) { $serror = 2; break; } $odraw = new imagickdraw(); $odraw->setfont($atextvalue['font_style']); $odraw->setfontsize($atextvalue['font_size']); $odraw->setfillcolor($atextvalue['font_color']); $this->image->annotateimage($odraw, $atextvalue['x'], $atextvalue['y'], $atextvalue['text_angle'], $atextvalue['text']); unset($odraw); } if ($serror == 1) { exit(unable to generate image. check \setimage\ properties); }elseif ($serror == 2) { exit(unable to generate image. check \settext\ properties); } $this->image->setimageformat(jpg); return $this->image->writeimage($simage); } } ?>
2,调用示例:
settext(this is one, 350, 20, 22, red); $oimagemagick->settext(this is two, 50, 50, 25, blue,50); $oimagemagick->setimage(brand.jpg, 160, 90, 0); $oimagemagick->setimage(tata.jpg, 160, 20); $newimagename = mynewimage.jpg; $oimagemagick->generateimage($newimagename); ?>
其它类似信息

推荐信息