Want placeholder image for broken img links

Started by scrapper, March 22, 2018, 09:51:56 AM

Previous topic - Next topic

Illori

i dont think alt text is part of the img bbc anyway. you could modify the bbc code to add it if you really want. i believe it is in subs.php.

@rjen

It is built in already. I would like to respect the text users put in, just to set a default if they don't
Running SMF 2.1 with latest TinyPortal at www.fjr-club.nl

@rjen

I got most of it working.

Solution is to force an Alt text for all images.
I noticed that chrome still display a broken image icon IF the image has an Alt text. Firefox does not though, but it will show the Alt text in the place of the broken image, which is at least a clear indication an image is missing...

It requires a few tweaks:

1. Add an additional txt to the Modifications.english-utf8.php (and whatever other language files you use)

// AW added standard alt text images
$txt['Alttext'] = '  image  ';



2. In subs.php
Find

array(
'tag' => 'img',
'type' => 'unparsed_content',
'parameters' => array(
'alt' => array('optional' => true),
'width' => array('optional' => true, 'value' => ' width="$1"', 'match' => '(\d+)'),
'height' => array('optional' => true, 'value' => ' height="$1"', 'match' => '(\d+)'),
),
'content' => '<img src="$1" alt="{alt}"{width}{height} class="bbc_img resized" />',
'validate' => function (&$tag, &$data, $disabled)
{
global $image_proxy_enabled, $image_proxy_secret, $boardurl;

$data = strtr($data, array('<br>' => ''));
if (strpos($data, 'http://') !== 0 && strpos($data, 'https://') !== 0)
$data = 'http://' . $data;

if (substr($data, 0, 8) != 'https://' && $image_proxy_enabled)
$data = $boardurl . '/proxy.php?request=' . urlencode($data) . '&hash=' . md5($data . $image_proxy_secret);
},
'disabled_content' => '($1)',
),
array(
'tag' => 'img',
'type' => 'unparsed_content',
'content' => '<img src="$1" alt="" class="bbc_img" />',
'validate' => function (&$tag, &$data, $disabled)
{
global $image_proxy_enabled, $image_proxy_secret, $boardurl;

$data = strtr($data, array('<br>' => ''));
if (strpos($data, 'http://') !== 0 && strpos($data, 'https://') !== 0)
$data = 'http://' . $data;

if (substr($data, 0, 8) != 'https://' && $image_proxy_enabled)
$data = $boardurl . '/proxy.php?request=' . urlencode($data) . '&hash=' . md5($data . $image_proxy_secret);
},
'disabled_content' => '($1)',
),


replace by
array(
'tag' => 'img',
'type' => 'unparsed_content',
'parameters' => array(
'alt' => array('optional' => true, 'value' => $txt['Alttext']),
'width' => array('optional' => true, 'value' => ' width="$1"', 'match' => '(\d+)'),
'height' => array('optional' => true, 'value' => ' height="$1"', 'match' => '(\d+)'),
),
'content' => '<img src="$1" alt="{alt}"{width}{height} class="bbc_img resized" />',
'validate' => function (&$tag, &$data, $disabled)
{
global $image_proxy_enabled, $image_proxy_secret, $boardurl;

$data = strtr($data, array('<br>' => ''));
if (strpos($data, 'http://') !== 0 && strpos($data, 'https://') !== 0)
$data = 'http://' . $data;

if (substr($data, 0, 8) != 'https://' && $image_proxy_enabled)
$data = $boardurl . '/proxy.php?request=' . urlencode($data) . '&hash=' . md5($data . $image_proxy_secret);
},
'disabled_content' => '($1)',
),
array(
'tag' => 'img',
'type' => 'unparsed_content',
'content' => '<img src="$1" alt="  image  " class="bbc_img" />',
'validate' => function (&$tag, &$data, $disabled)
{
global $image_proxy_enabled, $image_proxy_secret, $boardurl;

$data = strtr($data, array('<br>' => ''));
if (strpos($data, 'http://') !== 0 && strpos($data, 'https://') !== 0)
$data = 'http://' . $data;

if (substr($data, 0, 8) != 'https://' && $image_proxy_enabled)
$data = $boardurl . '/proxy.php?request=' . urlencode($data) . '&hash=' . md5($data . $image_proxy_secret);
},
'disabled_content' => '($1)',
),


3. In order to make the Alt text  clearly stand out from the regular post text.

Open index.css and add:


.post .inner img.bbc_img {
  font-style: italic;
  font-size: 0.8em;
}


I managed to get the first part of the subs.php fix to use the txt string for the Alt txt. In the second part I did not gget it working yet, so for now I hard-code the text '  image  ' .

Any suggestions to make that line use the txt string would be appreciated...
Running SMF 2.1 with latest TinyPortal at www.fjr-club.nl

Kindred

Giga, under normal circumstances, the server never downloads or processes the image... the page just displays the image directly from the url entered...   however, In order to tell if the image is broken, the server would have to download the image.

See what I am saying?
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Pipke

i have this in one of my mods, sofar i looked this is the only way to check if the image really exists.

//Uncomment below lines if you want to check for attachment avatars that are deleted from server, note that this will make the pages load longer!
//if( strpos($avatar, 'type=avatar') !== false)
//{
// if (!@getimagesize($avatar))
// $avatar = $settings['images_url']. '/abm_avatar.gif';
//}
"If something is wrong, fix it if you can. But train yourself not to worry: Worry never fixes anything."

Click here to view my mods for SMF

Hey 👋 Did i helped... you like what i do. You can now buy me a coffee! ☕

GigaWatt

Quote from: Kindred on March 24, 2018, 06:50:58 PM
Giga, under normal circumstances, the server never downloads or processes the image... the page just displays the image directly from the url entered...   however, In order to tell if the image is broken, the server would have to download the image.

See what I am saying?

Aha, I get it now ;).

You're saying that, basically, there is no way the script can tell if an image URL was broken or not. Specific commands would have to be issued to the server in order to achieve this, which isn't wise since it really increases server load.

I thought that the script had to reload the image twice, once for the analysis ("is the image there" check) and again for the actual image loading.

Well, you learn something new every day ;).




I just read what Pipke posted.

Quote from: Pipke on March 24, 2018, 06:57:53 PM
i have this in one of my mods, sofar i looked this is the only way to check if the image really exists.

//Uncomment below lines if you want to check for attachment avatars that are deleted from server, note that this will make the pages load longer!
//if( strpos($avatar, 'type=avatar') !== false)
//{
// if (!@getimagesize($avatar))
// $avatar = $settings['images_url']. '/abm_avatar.gif';
//}

Is the getimagesize the command you had in mind when you were talking about "server abuse" (:D)? Does the command really puts that much extra stress on the server, even if the image doesn't reside on the server (external URL)?
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

Advertisement: