php 圖片處理類(附執行個體)

來源:互聯網
上載者:User
分享一個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); ?>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.