magento调用PHPImageWorkshop生成水印图片

因为magento自带的生成水印类不好用,就改成用ImageWorkshop这个插件来生成。

这个插件太奇葩了,require_once的时候总是报错:

‘Warning: include(ImageWorkshop.php): failed to open stream: No such file or directory’

google很久,才发现要set_include_path.

1
2
3
$includePath = Mage::getBaseDir('lib');
set_include_path(get_include_path() . PS . $includePath);
require_once(Mage::getBaseDir('lib').DS.'PHPImageWorkshop'.DS.'ImageWorkshop.php');

 

具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
class Zou_Test_Helper_Data extends Mage_Core_Helper_Abstract
{
private static $_callbacks = array(
IMAGETYPE_GIF => array('output' => 'imagegif', 'create' => 'imagecreatefromgif'),
IMAGETYPE_JPEG => array('output' => 'imagejpeg', 'create' => 'imagecreatefromjpeg'),
IMAGETYPE_PNG => array('output' => 'imagepng', 'create' => 'imagecreatefrompng'),
IMAGETYPE_XBM => array('output' => 'imagexbm', 'create' => 'imagecreatefromxbm'),
IMAGETYPE_WBMP => array('output' => 'imagewbmp', 'create' => 'imagecreatefromxbm'),
);
private function _getCallback($callbackType, $fileType = null, $unsupportedText = 'Unsupported image format.')
{
if (null === $fileType) {
$fileType = $this->_fileType;
}
if (empty(self::$_callbacks[$fileType])) {
throw new Exception($unsupportedText);
}
if (empty(self::$_callbacks[$fileType][$callbackType])) {
throw new Exception('Callback not found.');
}
return self::$_callbacks[$fileType][$callbackType];
}

public function watermark($imagePath,$newPath=false){

if(!$newPath || empty($newPath)){
$newPath = $imagePath;
}
list($imgSrcWidth, $imgSrcHeight, $imgFileType, ) = getimagesize($imagePath);
$this->_fileType = $imgFileType;
// Init' layers
$includePath = Mage::getBaseDir('lib');
set_include_path(get_include_path() . PS . $includePath);
require_once(Mage::getBaseDir('lib').DS.'PHPImageWorkshop'.DS.'ImageWorkshop.php');
$font = Mage::getBaseDir('lib').DS.'PHPImageWorkshop'.DS.'Fonts'.DS.'OpenSans-Regular.ttf';
try {
$imageWorkshop = new PHPImageWorkshop\ImageWorkshop();
$norwayLayer = $imageWorkshop::initFromPath($imagePath);
$textLayer = $imageWorkshop::initTextLayer('Urheilumuseo', $font, 28, 'ffffff', $this->calculAngleBtwHypoAndLeftSide($norwayLayer->getWidth(), $norwayLayer->getHeight()));

// Some funky opacity
$textLayer->opacity(60);

// We add the $textLayer on the norway layer, in its middle
$norwayLayer->addLayer(1, $textLayer, 0, 0, 'MM');
$image = $norwayLayer->getResult();
$functionParameters = array();
$functionParameters[] = $image;
$functionParameters[] = $newPath;
call_user_func_array($this->_getCallback('output'), $functionParameters);

} catch (Exception $e) {
// echo $e->getMessage();die;
}
return;

}

// We want to add a text on the diagonal of the norway picture,
// We create a function to calculate the angle with pythagore & thales theorems
// this is not an obligation, you can choose another rotation angle...
function calculAngleBtwHypoAndLeftSide($bottomSideWidth, $leftSideWidth)
{
$hypothenuse = sqrt(pow($bottomSideWidth, 2) + pow($leftSideWidth, 2));
$bottomRightAngle = acos($bottomSideWidth / $hypothenuse) + 180 / pi();

return -round(90 - $bottomRightAngle);
}
}

 

坚持原创技术分享,您的支持将鼓励我继续创作!