Member
Member
kyrd   2022-05-06, 18:21
#1
Hi, I'm using `printImageDesc()` to display the Image description just under the thumbnails in the gallery view, but I'd like to limit the output to a certain fixed limit number of characters and add a link to the image item. Just like some News items. I know this shouldn't be difficult and I've tried something unsuccesfully but I'm not finding an example (that I can understand! ;-) in the docs ...
That's my code now:

<?php printBareImageTitle(); ?>
<?php echo printImageDesc(); ?>
Administrator
Administrator
acrylian   2022-05-06, 18:46
#2
Use this to get the plain desc:
https://docs.zenphoto.org/1.5.x/function-getImageDesc.html
That contains HTML formatting. There is also: https://docs.zenphoto.org/1.5.x/function-getBareImageDesc.html

Or the object model if there is a current image:
`$_zp_current_image->getDesc()`;

Rhen use https://docs.zenphoto.org/1.5.x/function-truncate_string.html to shortend the text. Use this with the "bare" version.

To keep formatting of HTML better use this as that try to fix invalid html (to work properly your server PHP needs the the tidy class installed):
https://docs.zenphoto.org/1.5.x/function-shortenContent.html

> add a link to the image item

The iamge item would be the single image page.
https://docs.zenphoto.org/1.5.x/function-getImageURL.html
You should see that on your theme's album.php

Or the object model if there is a current image:
`$_zp_current_image->getLink()`;

The object model version would be

If you mean the original full image itself:https://docs.zenphoto.org/1.5.x/function-getFullImageURL.html

Or the object model if there is a current image:
`$_zp_current_image->getFullImageURL()`;


Member
Member
kyrd   2022-05-06, 18:54
#3
Thankyou for your help, I'll try to make it work in my site :-)
Member
Member
kyrd   2022-05-09, 10:39
#4
I post here what I did, just in case someone else is interested

<?php echo shortenContent($_zp_current_image->getDesc(),222, ' <b>(...)</b> '); ?>

after unsuccesfully trying to insert a dynamic link to the ImageURL by wrapping the above `<b>(...)</b>` I decided to add instead the following lines

<a class="" href="<?php echo html_encode(getImageURL()); ?>" title="<?php echo html_encode(getBareImageTitle());?>">
[ <b>⇒</b> ]</a>

(Obviously the styles and classes are a working draft)

However I made this choice mainly because I've noticed that *shortenContent()* very cleverly closes any opened tag eventually interrupted (i.e. `<a href ... >`, `<b>`, `<i>` ...).

Here is a working example: http://www.indirizzofantasma.net/bnv/str-art/

A further step would be to print the link *only* when the *shortenContent()* is used, but I don't know how to write the *if* statement...

Administrator
Administrator
acrylian   2022-05-09, 10:52
#5
> However I made this choice mainly because I've noticed that shortenContent() very cleverly closes any opened tag eventually interrupted

Yes, "fixing" broken HTML is the main purpose of this. For plain string truncation you can just use native PHP functions.

> A further step would be to print the link only when the shortenContent() is used, but I don't know how to write the if statement...

Rather basic native PHP actually. just check the length of the description to be larger than your expected length using `strlen`. Of course that will also count HTML elements.



Member
Member
kyrd   2022-05-09, 11:16
#6
mmm, yeah, so it would mean that to make it work properly one should scrap away the unwanted HTML before counting... surely someone who knows *howto* PHP can make it easily.
In my case this is exactly a Murphy's law rule: _Every solution brings new problems_
;-)
Administrator
Administrator
acrylian   2022-05-09, 11:48
#7
Stripping html is rather easy using either our `getBare()` function or even the nativ PHP function `strip_tags()`.
  
Powered By MyBB, © 2002-2026 MyBB Group.
Made with by Curves UI.