Welcome to Geeklog, Anonymous Thursday, November 28 2024 @ 09:32 am EST

Geeklog Forums

Adding alt text to pictures


Status: offline

jmucchiello

Forum User
Full Member
Registered: 08/29/05
Posts: 985
In a thread in general help someone asked for alt text for stories. Here is the code. After you replace these two functions in /path.to.geeklog/system/lib_story.php, you can now enter image helper tags as [imageX:Alt text], [imageX_left:Alt text], and [image_right:Alt_text]

I have not thoroughly tested the unscaled version but they seem to work as expected.

Text Formatted Code

function STORY_replace_images($sid, $intro, $body)
{
    global $_CONF, $_TABLES, $LANG24;

    $stdImageLoc = true;
    if (!strstr($_CONF['path_images'], $_CONF['path_html'])) {
        $stdImageLoc = false;
    }
    $result = DB_query("SELECT ai_filename FROM {$_TABLES['article_images']} WHERE ai_sid = '$sid' ORDER BY ai_img_num");
    $nrows = DB_numRows($result);
   
    $aSearch = Array();
    $aReplace = Array();
   
    for ($i = 1; $i <= $nrows; $i++) {
        $A = DB_fetchArray($result);

        $sizeattributes = COM_getImgSizeAttributes ($_CONF['path_images'] . 'articles/' . $A['ai_filename']);

        $lLinkPrefix = '';
        $lLinkSuffix = '';
        if ($_CONF['keep_unscaled_image'] == 1) {
            $lFilename_large = substr_replace ($A['ai_filename'], '_original.',
                    strrpos ($A['ai_filename'], '.'), 1);
            $lFilename_large_complete = $_CONF['path_images'] . 'articles/'
                                      . $lFilename_large;
            if ($stdImageLoc) {
                $imgpath = substr ($_CONF['path_images'],
                                   strlen ($_CONF['path_html']));
                $lFilename_large_URL = $_CONF['site_url'] . '/' . $imgpath
                                     . 'articles/' . $lFilename_large;
            } else {
                $lFilename_large_URL = $_CONF['site_url']
                    . '/getimage.php?mode=showâ„‘=' . $lFilename_large;
            }
            if (file_exists ($lFilename_large_complete)) {
                $lLinkPrefix = '<a href="' . $lFilename_large_URL
                             . '" title="' . $LANG24[57] . '">';
                $lLinkSuffix = '</a>';
            }
        }

        if ($stdImageLoc) {
            $imgpath = substr ($_CONF['path_images'],
                               strlen ($_CONF['path_html']));
            $imgSrc = $_CONF['site_url'] . '/' . $imgpath . 'articles/'
                    . $A['ai_filename'];
        } else {
            $imgSrc = $_CONF['site_url']
                . '/getimage.php?mode=articlesâ„‘=' . $A['ai_filename'];
        }
 
        $imgSrc = preg_quote($imgSrc);
        $sizeattributes = preg_quote($sizeattributes);
        $lLinkPrefix = preg_quote($lLinkPrefix);
        $lLinkSuffix = preg_quote($lLinkSuffix);
       
        $aSearch[] = '%'.$lLinkPrefix.'<img ' . $sizeattributes . 'src="' . $imgSrc . '" alt="">' . $lLinkSuffix . '%';
        $aReplace[] = '[image'.$i.']';
        $aSearch[] = '%'.$lLinkPrefix.'<img ' . $sizeattributes . 'src="' . $imgSrc . '" alt="(.+)">' . $lLinkSuffix . '%';
        $aReplace[] = '[image'.$i.':1]';
        $aSearch[] = '%'.$lLinkPrefix.'<img ' . $sizeattributes . 'align="(left|right)" src="' . $imgSrc . '" alt="(.*)">' . $lLinkSuffix . '%';
        $aReplace[] = '[image'.$i.'_1:2]';

        if (($_CONF['allow_user_scaling'] == 1) and ($_CONF['keep_unscaled_image'] == 1)) {
            if (file_exists ($lFilename_large_complete)) {
                $sizeattributes = preg_quote(COM_getImgSizeAttributes($lFilename_large_complete));
                $imgSrc = preg_quote($lFilename_large_URL);
            }
            $aSearch[] = '%'.$lLinkPrefix.'<img ' . $sizeattributes . 'src="' . $imgSrc . '" alt="">' . $lLinkSuffix . '%';
            $aReplace[] = '[unscaled'.$i.']';
            $aSearch[] = '%'.$lLinkPrefix.'<img ' . $sizeattributes . 'src="' . $imgSrc . '" alt="(.+)">' . $lLinkSuffix . '%';
            $aReplace[] = '[unscaled'.$i.':1]';
            $aSearch[] = '%'.$lLinkPrefix.'<img ' . $sizeattributes . 'align="(left|right)" src="' . $imgSrc . '" alt="(.*)">' . $lLinkSuffix . '%';
            $aReplace[] = '[unscaled'.$i.'_1:2]';
        }
    }
    list($intro, $body) = preg_replace($aSearch, $aReplace, Array($intro, $body));

    return array($intro, $body);
}

function STORY_insert_images($sid, $intro, $body, $usage='html')
{
    global $_CONF, $_TABLES, $LANG24;

    $result = DB_query("SELECT ai_filename FROM {$_TABLES['article_images']} WHERE ai_sid = '$sid' ORDER BY ai_img_num");
    $nrows = DB_numRows($result);
    $errors = array();
    $stdImageLoc = true;
    if (!strstr($_CONF['path_images'], $_CONF['path_html'])) {
        $stdImageLoc = false;
    }
   
    $aSearch = Array();
    $aReplace = Array();
   
    for ($i = 1; $i <= $nrows; $i++) {
        $A = DB_fetchArray($result);

        $lLinkPrefix = '';
        $lLinkSuffix = '';
        if ($_CONF['keep_unscaled_image'] == 1) {
            $lFilename_large = substr_replace ($A['ai_filename'], '_original.',
                    strrpos ($A['ai_filename'], '.'), 1);
            $lFilename_large_complete = $_CONF['path_images'] . 'articles/'
                                      . $lFilename_large;
            if ($stdImageLoc) {
                $imgpath = substr ($_CONF['path_images'],
                                   strlen ($_CONF['path_html']));
                $lFilename_large_URL = $_CONF['site_url'] . '/' . $imgpath
                                     . 'articles/' . $lFilename_large;
            } else {
                $lFilename_large_URL = $_CONF['site_url']
                    . '/getimage.php?mode=showâ„‘=' . $lFilename_large;
            }
            if (file_exists ($lFilename_large_complete)) {
                $lLinkPrefix = '<a href="' . $lFilename_large_URL
                             . '" title="' . $LANG24[57] . '">';
                $lLinkSuffix = '</a>';
            }
        }

        $sizeattributes = COM_getImgSizeAttributes ($_CONF['path_images'] . 'articles/' . $A['ai_filename']);

        $match = '%[(image|unscaled)'.$i.'(_(left|right))?(Sad[^]]*))?]%';

        if (!preg_match($match, $intro) and !preg_match($match, $body)) {
            // There is an image that wasn't used, create an error
            $errors[] = $LANG24[48] . " #$i, {$A['ai_filename']}, " . $LANG24[53] . "<br>$match";
        } else {
            if ($usage=='email') {  // image will be attached, no path necessary
                $imgSrc = $A['ai_filename'];
            } elseif ($stdImageLoc) {
                $imgpath = substr ($_CONF['path_images'],
                                   strlen ($_CONF['path_html']));
                $imgSrc = $_CONF['site_url'] . '/' . $imgpath . 'articles/'
                        . $A['ai_filename'];
            } else {
                $imgSrc = $_CONF['site_url'] . '/getimage.php?mode=articlesâ„‘=' . $A['ai_filename'];
            }
           
            $aSearch[] = '%[image'.$i.'(Sad[^]]*))?]%';
            $aReplace[] = $lLinkPrefix . '<img ' . $sizeattributes . 'src="' . $imgSrc . '" alt="2">' . $lLinkSuffix;
            $aSearch[] = '%[image'.$i.'_(left|right)(Sad[^]]*))?]%';
            $aReplace[] = $lLinkPrefix . '<img ' . $sizeattributes . 'align="1" src="' . $imgSrc . '" alt="3">' . $lLinkSuffix;
            if (($_CONF['allow_user_scaling'] == 1) and ($_CONF['keep_unscaled_image'] == 1)) {
                if (file_exists ($lFilename_large_complete)) {
                    $imgSrc = $lFilename_large_URL;
                    $sizeattributes = COM_getImgSizeAttributes ($lFilename_large_complete);
                }
                $aSearch[] = '%[unscaled'.$i.'(Sad[^]]*))?]%';
                $aReplace[] = $lLinkPrefix . '<img ' . $sizeattributes . 'src="' . $imgSrc . '" alt="2">' . $lLinkSuffix;
                $aSearch[] = '%[unscaled'.$i.'_(left|right)(Sad[^]]*))?]%';
                $aReplace[] = $lLinkPrefix . '<img ' . $sizeattributes . 'align="1" src="' . $imgSrc . '" alt="3">' . $lLinkSuffix;
            }
           
        }
   }

    if (count($aSearch) > 0) {
        list($intro, $body) = preg_replace($aSearch, $aReplace, Array($intro, $body));
    }

    return array($errors, $intro, $body);
}

 

If there mistakes, there are in my copy/paste as this code is working on my installation of 1.4.0sr5_1.

I considered expanding the choices from left and right to include other valid align tags but decided against it. Ambitious folk can replace (left|right) with (left|right|top|middle|bottom|absbottom) etc if they so please in the couple places it occurs in the code above.

Joe
 Quote

Status: offline

jmucchiello

Forum User
Full Member
Registered: 08/29/05
Posts: 985
I guess smiley replacement code doesn't respect the CODE tag. Smile

Any chance this can make it into 1.4.2? Now that I have alt text in my database, I don't want to have to merge the code every release.
 Quote

All times are EST. The time is now 09:32 am.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content