Resize images in articles on the fly

Ceci est une ARCHIVE, peut-être périmée. Vérifiez bien les compatibilités !

This filter can resize all the images added by an author in the text of an article on the fly. You can specify max size for width, height or both.

Overview

Many layouts need images of a certain size specification in order to display them correctly. However, not all authors or administrators know that image size can be adjusted manually (you would be surprised !). So, in order to show all pages correctly, now you can automatically resize all images embedded in the text of an article using a filter.

How to use the filter

The filter is a modified version of reduire_image.
If the GD library is present on your server, the first time the filter is called, a new resized image will be created. This image will be used in the following filter calls. If the GD library was not present, the resizing will be made simply putting width and height attributes on the <img> tag in the HTML code.

The filter has two arguments :
-  the maximum pizel size of the image
-  the axis along which to resize (x,y,both)

The syntax is :
reduire_all_images{max_size,direction}

As an example, to limit all images to 500 pixels in width you would write :

<BOUCLE_art(ARTICLES){id_article}>
[(#TEXTE|reduire_all_images{500,x})]
</BOUCLE_art>

With this filter, a 450x600 image would not be resized, but a 600x600 would be.

original image
The image is larger that the column and even the caption is not fully visible.
resized image
The image was resized with reduire_all_images{500,x}.
Now it is fully visible together with its caption.

As with all filters, it is only active in the public site. So you won’t be able to preview your article with the resized images in the private space.

Cache

The resized images are stored in the « resized » folder situated in the subfolder of IMG whose name is like the file extension.
example : IMG/jpg/resized or IMG/gif/resized

They are calculated only the first time the filter is called.

They are stored with a name like this :
size-axe-imagename.extension

So an image called image1.jpg resized within 500 pixels on the x axe will be saved in IMG/jpg folder as 500-x-image1.jpg.

Automatic maintenance

An automatic maintenance action takes place after a certain time. This duration is indicated in the variable $maint_delay at the beginning of the file and can be changed. It’s default value is one week.

$maint_delay = 3600*24*7;  
//7 days of 24h of 3600 second 

Maintenance operation consists in managing and erasing the unused files (a « garbage collector »).

Thus, if you modify the templates and the sizes of the images, or you change the original images, the old resized images becomes useless. Maintenance will detect those not used anymore and will automatically erase them from resized folder.

Note : If ever a resized image is deleted but is still in use (because it has not been shown for a long time for example), then it will be recreated if requested, therefore there will not be any problem.

How to install

To install the filter, you must download the attached archive, extract the file mes_fonctions.php3 and add its content to your mes_fonctions.php3 file in your root directory. If you don’t yet have a file called mes_fonctions.php3, just add this one to your root directory.

This is an experimental filter so feel free to contact me if you are not able to make it work in your host environment.

téléchargement
Copiez-coller le code du fichier mes_fonctions dans votre propre fichier mes fonctions

Discussion

2 discussions

  • Hello,
    Thanks for this. I will use it for my web site. For now I face a problem with my site Kabulpress.org/my and I need your advice.

    When I’m trying to open and manage www.kabulpress.org/my , the following error have to face :
    Warning : imagejpeg() : Unable to open ’../local/cache-vignettes/L23xH20/arton803-942b2.jpg’ for writing in /home/kabulpre/public_html/my/ecrire/inc/filtres_images.php on line 482

    All small images are unavailable.

    I look forward to hear from you as soon as possible.
    Regards

    Répondre à ce message

  • 1

    When I tryed to use the filter the following error message appeared :

    Fatal error : Allowed memory size of 8388608 bytes exhausted (tried to allocate 4096 bytes) in /opt/lampp/htdocs/learnpython/mes_fonctions.php on line 124

    When I changed the value of the the memory-limit parameter in the php.ini from memory-limit = 8M to
    memory-limit = 16M , the filter worked fine.

    Répondre à ce message

Ajouter un commentaire

Avant de faire part d’un problème sur un plugin X, merci de lire ce qui suit :

  • Désactiver tous les plugins que vous ne voulez pas tester afin de vous assurer que le bug vient bien du plugin X. Cela vous évitera d’écrire sur le forum d’une contribution qui n’est finalement pas en cause.
  • Cherchez et notez les numéros de version de tout ce qui est en place au moment du test :
    • version de SPIP, en bas de la partie privée
    • version du plugin testé et des éventuels plugins nécessités
    • version de PHP (exec=info en partie privée)
    • version de MySQL / SQLite
  • Si votre problème concerne la partie publique de votre site, donnez une URL où le bug est visible, pour que les gens puissent voir par eux-mêmes.
  • En cas de page blanche, merci d’activer l’affichage des erreurs, et d’indiquer ensuite l’erreur qui apparaît.

Merci d’avance pour les personnes qui vous aideront !

Par ailleurs, n’oubliez pas que les contributeurs et contributrices ont une vie en dehors de SPIP.

Qui êtes-vous ?
[Se connecter]

Pour afficher votre trombine avec votre message, enregistrez-la d’abord sur gravatar.com (gratuit et indolore) et n’oubliez pas d’indiquer votre adresse e-mail ici.

Ajoutez votre commentaire ici

Ce champ accepte les raccourcis SPIP {{gras}} {italique} -*liste [texte->url] <quote> <code> et le code HTML <q> <del> <ins>. Pour créer des paragraphes, laissez simplement des lignes vides.

Ajouter un document

Suivre les commentaires : RSS 2.0 | Atom