Add centered asset drawing support and refine image transformations
- Introduce `drawAssetCentered` method in `dynamicimages_image_t` for drawing assets visually centered on specified coordinates. - Reset virtual canvas/page after image rotation in both `dynamicimages_asset_t` and `dynamicimages_image_t` to ensure consistent positioning. - Update `machine_1` logic to use `drawAssetCentered` for improved thumb drawing and dynamic pivot alignment. - Adjust thumb rotation calculations and positioning logic for better visual rendering.
This commit is contained in:
BIN
Binary file not shown.
|
Before Width: | Height: | Size: 509 KiB After Width: | Height: | Size: 1.2 MiB |
@@ -25,9 +25,12 @@ class machine_1 extends dynamicimages_image
|
||||
new dynamicimages_asset(self::getAssetPath(self::IMAGE_PANEL_BACKGROUND)),
|
||||
new dynamicimages_asset(self::getAssetPath(self::IMAGE_WASH_PROGRAMS_THUMB)),
|
||||
new dynamicimages_asset(self::getAssetPath(self::IMAGE_WASH_PROGRAMS)),
|
||||
], self::asset_path . str_replace('\\', '/', __CLASS__) . '/');
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function setup(): void
|
||||
{
|
||||
$this->initImageCanvas(6000, 4500);
|
||||
@@ -35,20 +38,34 @@ class machine_1 extends dynamicimages_image
|
||||
$this->drawAsset($this->getAsset(self::IMAGE_PANEL_BACKGROUND), 0, 0);
|
||||
$this->drawAsset($this->getAsset(self::IMAGE_WASH_PROGRAMS), 0, 0);
|
||||
$thumbHeight = 1550;
|
||||
$thumbX = 570;
|
||||
$this->drawAsset($this->getAsset(self::IMAGE_WASH_PROGRAMS_THUMB)->resize($this->calculateThumbWidth($thumbHeight), $thumbHeight)->rotate($this->getRotationThumbPosition()), $thumbX, 1290);
|
||||
$thumbX = 620;
|
||||
$thumbY = 1290;
|
||||
// Base resized thumb to compute the visual pivot (center coordinates)
|
||||
$baseThumb = $this->getAsset(self::IMAGE_WASH_PROGRAMS_THUMB)->resize($this->calculateThumbWidth($thumbHeight), $thumbHeight);
|
||||
$centerX = $thumbX + (int)floor($baseThumb->getWidth() / 2);
|
||||
$centerY = $thumbY + (int)floor($baseThumb->getHeight() / 2);
|
||||
|
||||
$tmp = new dynamicimages_asset($this->getAssetPath(self::IMAGE_WASH_PROGRAMS_THUMB));
|
||||
$tmp->resize($this->calculateThumbWidth($thumbHeight), $thumbHeight)
|
||||
->rotate($this->getRotationThumbPosition());
|
||||
$this->drawAssetCentered($tmp, $centerX, $centerY);
|
||||
// Free memory used by temporary asset image, if any
|
||||
if (method_exists($tmp, 'clearMemoryImage')) {
|
||||
$tmp->clearMemoryImage();
|
||||
}
|
||||
$this->drawAsset($this->getAsset(self::IMAGE_WASH_PROGRAMS), 0, 0);
|
||||
//$this->drawAsset($this->getAsset(self::IMAGE_POWER_BUTTON), 0, 0);
|
||||
}
|
||||
|
||||
public function getRotationThumbPosition(): float
|
||||
{
|
||||
// Each position represents a 30 degree increment (12 positions in total for 360 degrees)
|
||||
return ($this->thumb_position % 12) * 30.0;
|
||||
$degrees = -90;
|
||||
return ($this->thumb_position) * 30 + $degrees;
|
||||
}
|
||||
|
||||
public function calculateThumbWidth(int $height): int
|
||||
{
|
||||
return $height;
|
||||
$originalWidth = $this->getAsset(self::IMAGE_WASH_PROGRAMS_THUMB)->getWidth();
|
||||
$originalHeight = $this->getAsset(self::IMAGE_WASH_PROGRAMS_THUMB)->getHeight();
|
||||
return (int)round(($originalWidth / $originalHeight) * $height);
|
||||
|
||||
@@ -101,6 +101,8 @@ trait dynamicimages_asset_t
|
||||
$this->ensureMemoryImageLoaded();
|
||||
$bg = new \ImagickPixel($background === 'transparent' ? 'transparent' : $background);
|
||||
$this->memoryImage->rotateImage($bg, $degrees);
|
||||
// Reset virtual canvas/page so top-left becomes (0,0) and rotation is treated around center
|
||||
$this->memoryImage->setImagePage(0, 0, 0, 0);
|
||||
// Update cached size info if the class maintains it
|
||||
if (method_exists($this, 'getWidth') && method_exists($this, 'getHeight')) {
|
||||
if (property_exists($this, 'width')) {
|
||||
|
||||
@@ -160,6 +160,41 @@ trait dynamicimages_image_t
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw an asset centered around the given coordinates.
|
||||
* This is useful for rotated layers whose bounding box changes per angle,
|
||||
* but should visually share the same pivot point.
|
||||
*/
|
||||
public function drawAssetCentered(dynamicimages_asset $asset, int $centerX, int $centerY, float $opacity = 1.0): dynamicimages_image_i
|
||||
{
|
||||
$this->assertCanvasInitialized();
|
||||
if ($opacity < 0.0 || $opacity > 1.0) {
|
||||
throw new \InvalidArgumentException('Opacity must be between 0.0 and 1.0');
|
||||
}
|
||||
$memoryImage = method_exists($asset, 'getMemoryImage') ? $asset->getMemoryImage() : null;
|
||||
if ($memoryImage instanceof \Imagick) {
|
||||
$layer = $memoryImage; // already a clone from asset
|
||||
} else {
|
||||
if (!is_readable($asset->getPath())) {
|
||||
throw new \RuntimeException('Asset is not readable: ' . $asset->getPath());
|
||||
}
|
||||
$layer = new \Imagick();
|
||||
$layer->readImage($asset->getPath());
|
||||
}
|
||||
$layer->setImageAlphaChannel(\Imagick::ALPHACHANNEL_SET);
|
||||
if ($opacity < 1.0) {
|
||||
$layer->evaluateImage(\Imagick::EVALUATE_MULTIPLY, $opacity, \Imagick::CHANNEL_ALPHA);
|
||||
}
|
||||
$w = (int)$layer->getImageWidth();
|
||||
$h = (int)$layer->getImageHeight();
|
||||
$x = (int)floor($centerX - $w / 2);
|
||||
$y = (int)floor($centerY - $h / 2);
|
||||
$this->image->compositeImage($layer, \Imagick::COMPOSITE_DEFAULT, $x, $y);
|
||||
$layer->clear();
|
||||
$layer->destroy();
|
||||
return $this;
|
||||
}
|
||||
|
||||
// ===== Transformations on the temporary image object =====
|
||||
public function resize(int $width, int $height): dynamicimages_image_i
|
||||
{
|
||||
@@ -192,6 +227,8 @@ trait dynamicimages_image_t
|
||||
$this->assertCanvasInitialized();
|
||||
$bg = new \ImagickPixel($background === 'transparent' ? 'transparent' : $background);
|
||||
$this->image->rotateImage($bg, $degrees);
|
||||
// Reset virtual page so top-left is (0,0) after rotation; rotation occurs around center
|
||||
$this->image->setImagePage(0, 0, 0, 0);
|
||||
// Update stored dimensions
|
||||
$this->canvasWidth = (int)$this->image->getImageWidth();
|
||||
$this->canvasHeight = (int)$this->image->getImageHeight();
|
||||
|
||||
Reference in New Issue
Block a user