Home Backend Development PHP Tutorial PHP image processing class (with examples)

PHP image processing class (with examples)

Jul 25, 2016 am 08:55 AM

Share an image processing class implemented in PHP, which can set text watermarks and image watermarks. Friends in need can refer to it.

This section shares an image processing class that simply implements text watermarks and image watermarks. It is a small example for learning PHP image operations.

Code:

<?php 
class ImageModifier { 
/** 
 *  实现在图片上保存文本信息 
 * 
 *  default is array() 
 * 
 *  @access private 
 */ 
var $aTextData = array(); 

/** 
 *  保存文本信息到图片上 
 * 
 *  default is array() 
 * 
 *  @access private 
 */ 
var $aImageData = array(); 

/** 
 *  imagick的资源标识符
 * 
 *  default is FALSE 
 * 
 *  @access private 
 */ 
var $image = ""; 

/** 
 *  错误消息级别 
 * 
 *  default is 0 
 * 
 *  @varinteger 
 */ 
var $sError = 0; 


   /** 
 *  构造函数
 *   
 *  @param  string  $tplImage   template image 
 *  @access private 
 */ 
public function __construct($tplImage) { 

// Check Imagick class exist or not if not show error. 
if (!class_exists("Imagick", false)) { 
exit("Unable to load class: Imagick\n. Imagick Image Library Missing."); 
} 
// create a object of Imagick template image 
$this->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); 
} 
} 
?>
Copy after login

2, calling example:

<?php 
//调用类文件
require_once "ImageModifier.class.php"; 

//示例
$oImageMagick = new ImageModifier('template.jpg'); // Image Template on which you have to manupulate 
$oImageMagick->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); 
?>
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles