Something went wrong. Try again.
Snapshot of https://codeberg.org/notplants/AltTextGpt; full Git branches and tags, copied 2026-09-06.
Something went wrong. Try again.
AltTextGpt AltTextGpt.module.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387<?php namespace ProcessWire;
/** * AltTextGpt Module for ProcessWire 3.x * * A module for using ChatGpt to generate alt text for any images in your ProcessWire site which are * currently missing alt text. * * @property string $apiKey Open AI API Key * */
use GuzzleHttp\Client;
class AltTextGpt extends Process implements ConfigurableModule {
/** * Construct * * Here we set defaults for any configuration settings * */ public function __construct() { parent::__construct(); // remember to call the parent } /** * This is an optional initialization function called before any execute functions. * * If you don't need to do any initialization common to every execution of this module, * you can simply remove this init() method. * */ public function init() { parent::init(); // always remember to call the parent init }
/** * This method is executed when a page with your Process assigned is accessed. * * This can be seen as your main or index function. You'll probably want to replace * everything in this function. * * Return value is typically direct HTML markup. But it can also be an associative * array of variables to pass to a view file named either 'AltTextGpt.view.php' * or 'views/execute.php' (demonstrated here). * * @return string|array * */ public function ___execute() { // main page return $this->___executeForm(); }
/** * Handles the ./form/ URL * * This is the page from which alt text can be generated. * * @return string * */ public function ___executeForm() { $modules = $this->wire()->modules; $input = $this->wire()->input; $this->headline($this->_('AltTextGPT')); // the <h1> headline $this->browserTitle($this->_('AltTextGPT')); // The <title> tag
/** @var InputfieldForm $form */ $form = $modules->get('InputfieldForm'); $form->description = $this->_('Enter the prompt, model and configurations for alt text generation.');
/** @var InputfieldText $field */ $field = $modules->get('InputfieldText'); $field->attr('name', 'api_key'); $field->label = $this->_('Open AI API Key (can be found from your account on the Open AI website)'); $field->icon = 'file-text-o'; $field->value = $this->ApiKey; $field->required = true; $form->add($field);
/** @var InputfieldText $field */ $field = $modules->get('InputfieldText'); $field->attr('name', 'prompt'); $field->label = $this->_('Text to use as prompt for generating alt text with ChatGPT (e.g. "generate alt text for the following image in one sentence")'); $field->icon = 'file-text-o'; $field->value = 'Generate alt text for the following image in one sentence'; $field->required = true; $form->add($field);
/** @var InputfieldText $field */ $field = $modules->get('InputfieldText'); $field->attr('name', 'model'); $field->label = $this->_('Identifier of model to use (e.g. gpt-4o; a full list of available models and their identifiers can be found on the Open AI website)'); $field->icon = 'file-text-o'; $field->value = 'gpt-4o'; $field->required = true; $form->add($field);
/** @var InputfieldText $field */ $field = $modules->get('InputfieldInteger'); $field->attr('name', 'max_images'); $field->label = $this->_('Maximum number of images to generate alt text for in one batch (e.g. 10; 0 can be entered for no limit)'); $field->icon = 'file-text-o'; $field->required = true; $field->defaultValue = 10; $form->add($field);
// Create a new InputfieldCheckbox $inputfield = wire('modules')->get('InputfieldCheckbox'); $inputfield->icon = 'wrench'; $inputfield->name = 'test_mode'; $inputfield->label = 'Test Mode'; $inputfield->description = 'When enabled, alt text will only be generated for images which currently have "test" as their image description.'; $form->add($inputfield);
/** @var InputfieldSubmit $submit */ $submit = $modules->get('InputfieldSubmit'); $submit->attr('name', 'submit_now'); $submit->val($this->_('Generate Alt Text')); $submit->icon = 'file-text-o'; $submit->addActionValue('exit', $this->_('Submit and exit'), 'frown-o'); // after-submit actions $submit->addActionValue('pages', $this->_('Submit and go to page list'), 'meh-o'); $form->add($submit);
// check if form has been submitted if($input->post($submit->name)) $this->processForm($form);
$pages = $this->wire()->pages->find("template!=admin"); $numPages = $pages->count(); $numImages = 0; $numImagesWithoutAltText = 0; foreach ($pages as $page) { $imgs = $page->images; if ($imgs) { foreach ($imgs as $img) { $numImages += 1; if (! $img->description) {
$numImagesWithoutAltText += 1; } } } }
$pretext = sprintf( "<table class='alt-text-info'> <tbody> <tr><td>Number of pages in site:</td><td>%d</td></tr> <tr><td>Number of images in site:</td><td>%d</td></tr> <tr><td>Number of images without alt text:</td><td>%d</td></tr> </tbody> </table> <br></br> <div class='gpt-explanation'> <p> This ProcessWire module, AltTextGPT, is an interface for generating alt text for all of the images in your website, using the ChatGPT Open AI API. </p> <p> Using the API requires an account with the Open AI API and costs money, although its pay-what-you-use and the charges are minimal. For example, alt text was generated for 200 images, using 94 cents of Open AI Credits. You can create an account with Open AI, from <a href='https://openai.com/'>this link</a>, and then once you have an API key, you can enter it below, or configure it as a permanent setting for this module via Modules->Configure->AltTextGpt. </p> <p> After configuring the API key as described above, you can then use the form below to generate alt text for images in the site. The module will attempt to generate alt txt for every image that currently has no alt text, one at a time. Generating alt text takes a few seconds for each image, so this is not an instantaneous process. For this reason, if you have many images, we suggest generating alt text for the images in batches. You can also set a batch size below, generating alt text for 10 or 20 images at a time, and then repeating the process, until you have generated alt text for all of the images in the site. After each run, the table above should show that there are fewer images without alt text in the site, until eventually the table indicates that there are 0 images in the site without alt text. </p> <p> Note, for alt text to show up for images uploaded in the body of a CKEditor field, this configuration must be set for that field as described in <a href='https://processwire.com/talk/topic/25641-how-do-i-fill-body-field-image-alt/?do=findComment&comment=214548'>this comment</a>. </p> </div> <br></br> ", $numPages, $numImages, $numImagesWithoutAltText); $text = $pretext . $form->render(); return $text; }
// Function to generate alt text using OpenAI's API protected function generateAltText($apiKey, $model, $prompt, $imageUrl) { // Initialize Guzzle client $client = new Client([ 'base_uri' => 'https://api.openai.com/v1/', 'headers' => [ 'Authorization' => 'Bearer ' . $apiKey, 'Content-Type' => 'application/json' ] ]);
// Prepare the payload $fullPrompt = $prompt . $imageUrl;
$payload = [ 'model' => $model, 'messages' => [ [ "role" => "user", "content"=> [ [ "type"=> "text", "text"=> $prompt ], [ "type" => "image_url", "image_url" => [ "url"=> $imageUrl ] ] ], 'max_tokens' => 50 ]]];
try { // Send the POST request to OpenAI's API $response = $client->request('POST', 'chat/completions', [ 'json' => $payload ]);
// Get the response body $responseBody = json_decode($response->getBody(), true);
// Extract and return the generated alt text if (isset($responseBody['choices'][0]['message']['content'])) { return trim($responseBody['choices'][0]['message']['content']); } else {// return 'Failed to generate alt text.'; return $responseBody; } } catch (RequestException $e) { // Handle request exception echo 'Request failed: ' . $e->getMessage(); if ($e->hasResponse()) { echo "\nResponse: " . $e->getResponse()->getBody(); } return 'Error in generating alt text.'; } }
/** * Process an Inputfields form and respond to requested submit action * * @param InputfieldForm $form * */ protected function processForm(InputfieldForm $form) { $input = $this->wire()->input; $session = $this->wire()->session; $config = $this->wire()->config; // process the form $form->processInput($input->post); // return now if form had errors if(count($form->getErrors())) return;
// Generate alt text for the image $apiKey = $form->getChildByName('api_key')->val(); $prompt = $form->getChildByName('prompt')->val(); $prompt = $form->getChildByName('prompt')->val(); $model = $form->getChildByName('model')->val(); $max_images = $form->getChildByName('max_images')->val(); $test_mode = $form->getChildByName('test_mode')->val(); $this->message("Using API Key: " . $apiKey); $this->message("Using Open AI model: " . $model); $this->message("Using prompt: " . $prompt); $this->message("Max number of images: " . $max_images); if ($test_mode) { $this->message("Test mode is enabled."); }
// try to add to alt text to all images $pages = $this->wire()->pages->find("template!=admin"); $numPages = $pages->count(); $numImages = 0; $numImagesWithoutAltText = 0; $maxCount = $max_images; foreach ($pages as $page) { if ($numImagesWithoutAltText > $maxCount) { break; } $this->message("Searching page for images: " . $page->url); $imgs = $page->images; if ($imgs) { foreach ($imgs as $img) { $numImages += 1; if ((!$test_mode && !$img->description) || ($test_mode && $img->description == "test")) { $numImagesWithoutAltText += 1; $imageUrl = "https://infrastructures.us" . $img->url; $this->message("image url: " . $imageUrl); try { $altText = $this->generateAltText($apiKey, $model, $prompt, $imageUrl); $st = var_export($altText, true); $this->message("Generated new alt text: " . $st); $img->description = $altText; } catch (\Exception $e) { $this->message("Failed to generate alt text for url: " . $imageUrl); } if ($numImagesWithoutAltText > $maxCount) { break; } } else { $this->message("Found previously set alt text: " . $img->description); } } } $page->save(); } /** @var InputfieldSubmit $submit */ $submit = $form->getChildByName('submit_now');
// user selected: submit and exit if($submit->submitValue === 'exit') $session->redirect('../');
// user selected: submit and go to page list if($submit->submitValue === 'pages') $session->redirect($config->urls->admin); }
/** * Called only when your module is installed * * If you don't need anything here, you can simply remove this method. * */ public function ___install() { parent::___install(); // Process modules must call parent method }
/** * Called only when your module is uninstalled * * This should return the site to the same state it was in before the module was installed. * * If you don't need anything here, you can simply remove this method. * */ public function ___uninstall() { parent::___uninstall(); // Process modules must call parent method }
/** * Get module configuration inputs * * As an alternative, configuration can also be specified in an external file * with a PHP array. See an example in the /extras/AltTextGpt.config.php file. * * @param InputfieldWrapper $inputfields * */ public function getModuleConfigInputfields(array $data) { $inputfields = new InputfieldWrapper(); $modules = $this->wire()->modules; /** @var InputfieldText $f */ $f = $modules->get('InputfieldText'); $f->attr('name', 'ApiKey'); $f->label = $this->_('Open AI API Key'); $f->description = $this->_('You can get this Key from the Open AI website after making an account'); $f->attr('value', isset($data['ApiKey']) ? $data['ApiKey'] : $this->apiKey); $inputfields->add($f);
return $inputfields;
} }