Files
2026-07-01 16:44:17 +03:00

110 lines
2.8 KiB
PHP
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php namespace ZN\Image;
/**
* ZN PHP Web Framework
*
* "Simplicity is the ultimate sophistication." ~ Da Vinci
*
* @package ZN
* @license MIT [http://opensource.org/licenses/MIT]
* @author Ozan UYKUN [ozan@znframework.com]
*/
class WatermarkImageAligner
{
/**
* It forms the alignment values according to conformation.
*
* @param string $type - options
* [
* topleft |topcenter |topright
* middleleft|center |middleright
* bottomleft|bottomcenter|bottomright
* ]
* @param string $sourceWidth
* @param string $sourceHeight
* @param string $targetWidth
* @param string $targetHeight
* @param int $margin
*
* @return array - [x, y]
*/
public static function align($type, $swidth, $sheight, $twidth, $theight, $margin)
{
switch( strtolower($type) )
{
case 'center':
{
$x = self::alignCenter($twidth , $swidth);
$y = self::alignCenter($theight, $sheight);
}
break;
case 'topleft':
{
$x = $margin;
$y = $margin;
}
break;
case 'topcenter':
{
$x = self::alignCenter($twidth , $swidth);
$y = $margin;
}
break;
case 'topright':
{
$x = self::alignEdge($twidth, $swidth, -$margin);
$y = $margin;
}
break;
case 'middleleft':
{
$x = $margin;
$y = self::alignCenter($theight, $sheight);
}
break;
case 'middleright':
{
$x = self::alignEdge($twidth, $swidth, -$margin);
$y = self::alignCenter($theight, $sheight);
}
break;
case 'bottomleft':
{
$x = $margin;
$y = self::alignEdge($theight, $sheight, -$margin);
}
break;
case 'bottomcenter':
{
$x = self::alignCenter($twidth, $swidth);
$y = self::alignEdge($theight, $sheight, -$margin);
}
break;
case 'bottomright':
{
$x = self::alignEdge($twidth , $swidth , -$margin);
$y = self::alignEdge($theight, $sheight, -$margin);
}
break;
}
return [$x, $y];
}
/**
* Protected align left
*/
protected static function alignEdge($val1, $val2, $margin)
{
return $val1 - $val2 + $margin;
}
/**
* Protected align center
*/
protected static function alignCenter($val1, $val2)
{
return ($val1 / 2) - ($val2 / 2);
}
}