Jake Vanderwerf
2026-08-30 a58a2e7ca4700d3e23341fde767060a4c2c704a3
inc/managers/ImageGenerator.php
@@ -1,7 +1,6 @@
<?php
namespace JVBase\managers;
use JVBase\managers\UploadManager;
use WP_Error;
use Imagick;
use ImagickPixel;
@@ -21,6 +20,7 @@
 *
 * Generates artist feature cards as webp images with artist information
 * Uses Imagick with GD fallback and leverages FileUploadManager storage
 * TODO: Integrate better with UploadManager.php
 */
class ImageGenerator
{
@@ -97,9 +97,11 @@
    /**
     * Generate the artist card
     */
    /**
     * @return array
     */
   /**
    * @return array
    * @throws Exception
    * @throws Exception
    */
    public function generate():array
    {
        // Generate appropriate filename
@@ -132,12 +134,14 @@
        ];
    }
    /**
     * Create WordPress attachment from generated image
     * @param string $file_path
     *
     * @return int|WP_Error
     */
   /**
    * Create WordPress attachment from generated image
    * @param string $file_path
    *
    * @return int|WP_Error
    * @throws Exception
    * @throws Exception
    */
    protected function createAttachment(string $file_path):int|WP_Error
    {
        // Get upload directory based on user/content structure
@@ -146,7 +150,7 @@
        // Get directory structure from FileUploadManager if available
        if ($this->fileManager) {
            $user_dir = $this->fileManager->getUploadDirectory();
            $user_dir = $this->fileManager->generateDirectory('image', $this->data);
            $relative_dir = str_replace($base_dir, '', dirname($file_path));
            $destination_dir = $base_dir . '/' . $relative_dir;
        } else {
@@ -183,13 +187,10 @@
        $artist_name = $this->data['display_name'];
        $city = $this->data['city'];
        switch ($this->type) {
            case 'shop':
                $title = 'Featured Shop Image';
                break;
            default:
                $title = 'Featured Artist Image';
        }
      $title = match ($this->type) {
          'shop' => 'Featured Shop Image',
          default => 'Featured Artist Image',
       };
        $attachment = [
            'post_mime_type' => 'image/webp',
@@ -212,14 +213,10 @@
        require_once(ABSPATH . 'wp-admin/includes/media.php');
        switch ($this->type) {
            case 'shop':
                $type = 'Tattoo Studio';
                break;
            default:
                $type = $this->data['type'];
                break;
        }
      $type = match ($this->type) {
          'shop' => 'Tattoo Studio',
          default => $this->data['type'],
       };
        // Set alt text
        $alt_text = sprintf(
            'Featured image for %s - %sin %s',
@@ -239,20 +236,16 @@
    protected function generateFilename():void
    {
        if ($this->fileManager) {
            switch ($this->type) {
                case 'shop':
                    $title = 'tattoo-shop';
                    break;
                default:
                    $title = 'artist';
                    break;
            }
         $title = match ($this->type) {
              'shop' => 'tattoo-shop',
              default => 'artist',
           };
            // Use FileUploadManager to generate a filename
            $user_data = get_userdata($this->data['user_id']);
            $this->outputFilename = $this->fileManager->generateFilename(
                $title.'.webp',
                $user_data
                $this->data
            );
        } else {
            // Fallback filename generation
@@ -571,13 +564,15 @@
        }
    }
    /**
     * Create gradient background with Imagick
     * @param Imagick $canvas
     * @param string $color Center color
     * @return void
     */
    protected function createGradientBackgroundImagick(Imagick &$canvas, string $color): void
   /**
    * Create gradient background with Imagick
    * @param Imagick $canvas
    * @param string $color Center color
    * @return void
    * @throws ImagickException
    * @throws ImagickException
    */
    protected function createGradientBackgroundImagick(Imagick $canvas, string $color): void
    {
        $width = $canvas->getImageWidth();
        $height = $canvas->getImageHeight();
@@ -958,7 +953,7 @@
     * @param string $color Center color
     * @return void
     */
    protected function createGradientBackgroundGD(GdImage &$canvas, string $color):void
    protected function createGradientBackgroundGD(GdImage $canvas, string $color):void
    {
        $width   = imagesx($canvas);
        $height  = imagesy($canvas);
@@ -1016,22 +1011,13 @@
        $imageInfo = getimagesize($imagePath);
        // Create image from source based on file type
        switch ($imageInfo[2]) {
            case IMAGETYPE_JPEG:
                $sourceImage = imagecreatefromjpeg($imagePath);
                break;
            case IMAGETYPE_PNG:
                $sourceImage = imagecreatefrompng($imagePath);
                break;
            case IMAGETYPE_GIF:
                $sourceImage = imagecreatefromgif($imagePath);
                break;
            case IMAGETYPE_WEBP:
                $sourceImage = imagecreatefromwebp($imagePath);
                break;
            default:
                throw new Exception("Unsupported image type");
        }
      $sourceImage = match ($imageInfo[2]) {
          IMAGETYPE_JPEG => imagecreatefromjpeg($imagePath),
          IMAGETYPE_PNG => imagecreatefrompng($imagePath),
          IMAGETYPE_GIF => imagecreatefromgif($imagePath),
          IMAGETYPE_WEBP => imagecreatefromwebp($imagePath),
          default => throw new Exception("Unsupported image type"),
       };
        // Calculate dimensions while maintaining aspect ratio
        $sourceWidth = imagesx($sourceImage);