Answer the question
In order to leave comments, you need to log in
How to make a translucent watermark using the CImageHandler library in yii1?
Good afternoon,
When loading an image, I put a translucent png watermark on it, but for some reason - the entire transparent substrate becomes a white rectangle,
// в поведении загрузки изоражения
public function createWM($image) {
Yii::app()->ih
->load($image)
->watermark($_SERVER['DOCUMENT_ROOT'] .
'/images/wm.png',
10,
20,
CImageHandler::CORNER_RIGHT_TOP)
->save($image);
}
public function watermark($watermarkFile, $offsetX, $offsetY, $corner = self::CORNER_RIGHT_BOTTOM, $zoom = false)
{
$this->checkLoaded();
if ($wImg = $this->loadImage($watermarkFile))
{
$posX = 0;
$posY = 0;
$watermarkWidth = $wImg['width'];
$watermarkHeight = $wImg['height'];
if($zoom !== false)
{
$dimension = round(max($this->width, $this->height) * $zoom);
$watermarkHeight = $dimension;
$watermarkWidth = round($watermarkHeight / $wImg['height'] * $wImg['width']);
if($watermarkWidth > $dimension)
{
$watermarkWidth = $dimension;
$watermarkHeight = round($watermarkWidth / $wImg['width'] * $wImg['height']);
}
}
switch ($corner)
{
case self::CORNER_LEFT_TOP:
$posX = $offsetX;
$posY = $offsetY;
break;
case self::CORNER_RIGHT_TOP:
$posX = $this->width - $watermarkWidth - $offsetX;
$posY = $offsetY;
break;
case self::CORNER_LEFT_BOTTOM:
$posX = $offsetX;
$posY = $this->height - $watermarkHeight - $offsetY;
break;
case self::CORNER_RIGHT_BOTTOM:
$posX = $this->width - $watermarkWidth - $offsetX;
$posY = $this->height - $watermarkHeight - $offsetY;
break;
case self::CORNER_CENTER:
$posX = floor(($this->width - $watermarkWidth) / 2);
$posY = floor(($this->height - $watermarkHeight) / 2);
break;
default:
throw new Exception('Invalid $corner value');
}
imagecopyresampled(
$this->image,
$wImg['image'],
$posX,
$posY,
0,
0,
$watermarkWidth,
$watermarkHeight,
$wImg['width'],
$wImg['height']
);
imagedestroy($wImg['image']);
return $this;
}
else
{
return false;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question