4images Forum & Community

4images Modifications / Modifikationen => Mods & Plugins (Releases & Support) => Topic started by: dp on January 19, 2010, 03:59:32 PM

Title: [MOD] similar images
Post by: dp on January 19, 2010, 03:59:32 PM
This Mod displays thumbs of images similar to the choosen one on the details page. Similarity is recognized by text-comparison of image keywords (or image description, if one likes better). From the whole set of similar picture a randomized subset is selected to be displayed. You can configure this number of displayed thumbs.

Note
This Mod uses metadata of the images to find similar pics. It just takes e.g. the keywords of the image displayed on the details page and performes a text-comparison via php function "similar_text()". That means, if your metadata does not properly describe your images in a consist and structural way, the result will not be satisfyingly.

You must fit some variables of this mod to your gallerys image data. The best way to to this is still to vary variables value and look at the results.
For example:
similar_text() calculates similarity of two strings by setting a defined variable at 0 to 100, where 0 means no and 100 complete similarity.
Therefor in this Mod "$percent_limit = 80; " is the value that devides similar images from non-similar. Descending the value results more similar images and vice versa.

In this Mod I use only a part of 50 letters of the compared metadata. This is because in my database the first keywords contains the most relevant information. So if one image has 20 keywords and a real similar image only 5, text-comparison will result a low similarity. If I use only the first keywords for comparison, comparison will fit much better.
Vary the number of letters in the substr()-function or remove this function to use the whole metadata.

Acknowledgment
This Mod is based on Mod Random pictures by V@tno
http://www.4homepages.de/forum/index.php?topic=4259.msg17513#msg17513 (http://www.4homepages.de/forum/index.php?topic=4259.msg17513#msg17513)

Files affected
[change] details.php
[change] lang/english/main.php
[change] lang/yourlanguage/main.php
[change] templates/default/details.html

open details.php, locate
//-----------------------------------------------------
//--- Show Image --------------------------------------
//-----------------------------------------------------

insert before
//------------------------------------
//------- MOD Similar Images
//------------------------------------
$image_keywords  = substr($image_row['image_keywords'], 0, 50); //keywords of actual image
$image_description  = $image_row['image_description'];          //description of actual image
$image_id_self  = $image_row['image_id'];                       //id of actual image
$i_ids = 0;
$i_ids_max = 4;                                                 //max number of thumbs displayed
$image_ids = "";
$percent_limit = 80;                                            //limit of similarity; vary to fit to your database!
$percent_minimum = 40;                                          //minimum of similarity; vary to fit to your database!

//-- select all images and identify similar images
//-- write a string $image_ids with the set of id's, comma seperated
//-- if there are no similar images, descend $percent_limit and try once more
$sql = "SELECT image_id, image_name, image_description, image_keywords, image_active
        FROM ".IMAGES_TABLE."
        WHERE image_active = 1";
$result_allimages = $site_db->query($sql);

while (($i_ids == 0) && ($percent_limit > $percent_minimum)) {
        while ($image_row_allimages = $site_db->fetch_array($result_allimages)){
              similar_text ( $image_keywords, substr($image_row_allimages['image_keywords'], 0, 50), $percent );
//              similar_text ( $image_description, substr($image_row_allimages['image_description'], 0, 50), $percent );
              if (($percent > $percent_limit) && ($image_row_allimages['image_id'] != $image_id_self)) {
                 $i_ids = $i_ids + 1;
                 $image_ids .= $image_row_allimages['image_id'] . ", ";
              }
        }
        $percent_limit = $percent_limit - 5;
}

//-- remove the last comma in $image_ids
if (strlen($image_ids) > 0) {
  $image_ids = substr($image_ids, 0, strlen($image_ids)-2);
}
//-- set $i_ids to its maximum, for correct mysql statement below
if ($i_ids > 4) $i_ids = $i_ids_max;

//-- build table with thumbs of similar images (only if there are some, of course)
if ($i_ids == 0) {
  $similar_images = "<table width=\"".$config['image_table_width']."\" border=\"0\" cellpadding=\"".$config['image_table_cellpadding']."\" cellspacing=\"".$config['image_table_cellspacing']."\"><tr class=\"imagerow1\"><td>";
  $similar_images .= "</td></tr></table>";
}
else  {

//---- select similar images, there id's are in the set $image_ids now
//---- randomized and limited
$sql = "SELECT *
        FROM ".IMAGES_TABLE."
        WHERE image_id IN (" . $image_ids . ")
        ORDER BY RAND()
        LIMIT ".$i_ids;
$result_similarimages = $site_db->query($sql);
$num_rows_similarimages = $site_db->get_numrows($result_similarimages);

//---- build table and table-cells
if (!$num_rows_similarimages)  {
  $similar_images = "<table width=\"".$config['image_table_width']."\" border=\"0\" cellpadding=\"".$config['image_table_cellpadding']."\" cellspacing=\"".$config['image_table_cellspacing']."\"><tr class=\"imagerow1\"><td>";
  $similar_images .= "</td></tr></table>";
}
else  {
  $similar_images = "<table width=\"".$config['image_table_width']."\" border=\"0\" cellpadding=\"".$config['image_table_cellpadding']."\" cellspacing=\"".$config['image_table_cellspacing']."\">";
  $count = 0;
  $bgcounter = 0;
  while ($image_row_similarimages = $site_db->fetch_array($result_similarimages)){
    if ($count == 0) {
      $row_bg_number = ($bgcounter++ % 2 == 0) ? 1 : 2;
      $similar_images .= "<tr class=\"imagerow".$row_bg_number."\">\n";
    }
    $similar_images .= "<td width=\"".$imgtable_width."\" valign=\"top\">\n";

    show_image($image_row_similarimages);
    $similar_images .= $site_template->parse_template("thumbnail_bit");
    $similar_images .= "\n</td>\n";
    $count++;
    if ($count == $config['image_cells']) {
      $similar_images .= "</tr>\n";
      $count = 0;
    }
  }

  if ($count > 0)  {
    $leftover = ($config['image_cells'] - $count);
    if ($leftover >= 1) {
      for ($f = 0; $f < $leftover; $f++) {
        $similar_images .= "<td width=\"".$imgtable_width."\">\n&nbsp;\n</td>\n";
      }
      $similar_images .= "</tr>\n";
    }
  }
  $similar_images .= "</table>\n";
}
}

//-- register template-keys
$site_template->register_vars(array(
    "similar_images"        => $similar_images,
    "lang_similar_images"   => $lang['lang_similar_images']
    ));
unset($similar_images);
//------- End similar images---------
//------------------------------------

open lang/english/main.php
insert at the bottom (before ?>)
//-----------------------------------------------------
//--- MOD similar pictures dp
//-----------------------------------------------------
$lang['lang_similar_images'] = "similar images";

open lang/yourlanguage/main.php
insert at the bottom (before ?>)
//-----------------------------------------------------
//--- MOD similar pictures dp
//-----------------------------------------------------
$lang['lang_similar_images'] = "translation of "similar images"";

open templates/default/details.html
place {lang_similar_images} and {similar_images} were ever you want to display the table of thumbs with similar images

Title: Re: [MOD] similar images
Post by: Rembrandt on January 19, 2010, 05:42:57 PM
Hi!

Thank you for sharing this mod.

mfg Andi



Title: Re: [MOD] similar images
Post by: Sun Zaza on February 18, 2010, 03:07:50 AM
Finally a related images for 4images. I did not test yet, but I am very happy with it.
Thank you very much dp :D
Cruxy
Title: Re: [MOD] similar images
Post by: coloresonline on February 19, 2010, 12:21:46 AM
Thanks for sharing it. It works perfectly,.  adjusting the values to real related pictures.
Thanks.
Title: Re: [MOD] similar images
Post by: GaYan on February 20, 2010, 05:46:52 AM
Thanks man..its a great mod :) Works Coooooooooooooooooooooooooooooooooooooooooooooooooooooooool  :mrgreen:
Title: Re: [MOD] similar images
Post by: PSDProtocol on May 05, 2010, 01:10:44 PM
Works great.  Although the generated images are all saying they are from (Guest) and not the submitter.  How do I fix this or remove that info panel to show just the thumb.
Title: Re: [MOD] similar images
Post by: CanonInk on May 05, 2010, 02:36:32 PM
Many thanks !!!
Title: Re: [MOD] similar images
Post by: dp on May 05, 2010, 04:42:24 PM
Works great.  Although the generated images are all saying they are from (Guest) and not the submitter.  How do I fix this or remove that info panel to show just the thumb.

The Mod uses the template-file "thumbnail_bit.html" to display the thumbs and their metadata. In this file there is a templatekey {user_name_link} which is registered by the function "show_image()" in include/functions.php. Sorry, but I got no idea, why this template_key has the wrong value.

You can simply take out the templatekey {user_name_link} from thumbnail_bit.html, but this will affect all thumbs in your gallery! A simple solution would be, to copy your thumbnail_bit.html to e.g. thumbnail_bit_similarimages.html and parse this template.

After copying thumbnail_bit.html to thumbnail_bit_similarimages.html find in details.php
Code: [Select]
    $similar_images .= $site_template->parse_template("thumbnail_bit");and change it to
Code: [Select]
    $similar_images .= $site_template->parse_template("thumbnail_bit_similarimages");
Now you got two different templates to display your thumbs and you are free to design them seperately.
Hope this helps!
Title: Re: [MOD] similar images
Post by: exceLLency on May 07, 2011, 12:04:54 AM
Thanks for sharing it. It works perfectly
Title: Re: [MOD] similar images
Post by: juewei on May 09, 2011, 09:11:03 AM
habe mir das jetzt mal angesehen und auch eingebaut. Leider ohne Erflolg.
Da wir keine keywords  (Schlüsselwörter) zulassen.
Ist es auch möglich anhand der Bildberschreibung?
Title: Re: [MOD] similar images
Post by: dp on May 09, 2011, 11:28:40 AM
habe mir das jetzt mal angesehen und auch eingebaut. Leider ohne Erflolg.
Da wir keine keywords  (Schlüsselwörter) zulassen.
Ist es auch möglich anhand der Bildberschreibung?

Nichts einfacher als das! Ist nämlich im Mod schon vorbereitend angelegt ...

ersetze
              similar_text ( $image_keywords, substr($image_row_allimages['image_keywords'], 0, 50), $percent );
//              similar_text ( $image_description, substr($image_row_allimages['image_description'], 0, 50), $percent );


durch
//              similar_text ( $image_keywords, substr($image_row_allimages['image_keywords'], 0, 50), $percent );
              similar_text ( $image_description, substr($image_row_allimages['image_description'], 0, 50), $percent );

Title: Re: [MOD] similar images
Post by: juewei on May 09, 2011, 11:36:33 AM
hast du dich da vertan?
du hast geschrieben, dass ich das eine mit dem anderen ersetzen soll, nur ist ja beides das Gleiche, was du da geschrieben hast, oder?  :wink:

sorry war noch nicht ganz wach!!!!

jezt geht es, Danke
Title: Re: [MOD] similar images
Post by: bergblume on May 13, 2011, 10:55:03 AM
coole sache... kleine frage bzgl. erweiterung.. ich habe meine bilder geo-getagged... kann ich mir auch die nächstgelegenen Bilder (laut GPS koordinaten) in einem definierten Umkreis anzeigen lassen? (oder zumindest es so definieren, dass automatisch die fünf nächstgelegenen angezeigt werden?
Title: Re: [MOD] similar images
Post by: dp on May 13, 2011, 03:53:20 PM
coole sache... kleine frage bzgl. erweiterung.. ich habe meine bilder geo-getagged... kann ich mir auch die nächstgelegenen Bilder (laut GPS koordinaten) in einem definierten Umkreis anzeigen lassen? (oder zumindest es so definieren, dass automatisch die fünf nächstgelegenen angezeigt werden?

Das würde ich so machen:
- Abfrage der Felder mit den Koordinaten und ggf. Aufteilung in Länge und Breite
- Schleife, in der diese Koordinaten schrittweise vergrößert/verkleinert werden, und Vergleich mit den Koordniaten der übrigen Datensätze
- Schleife solange durchlaufen bis mindestens 5 weitere Datensätze identifiziert wurden
- Ausgabe der Bilder

Im wesentlichen kann der Aufbau meines Mods dabei so belassen werden.

Nun frag mich aber bitte nicht, ob ich das mal eben programmieren kann  :wink: - dazu habe ich momentan absolut keine Zeit. Wenn du es aber selbst umsetzen solltest, poste doch dein Ergebnis hier im Forum, ggf. auch als eigenes Mod.

UPDATE
Geht natürlich viel einfacher, vorausgesetzt, die Koordinaten für Länge und Breite befinden sich in getrennten Feldern. Dann braucht man die Tabelle nur nacheinander nach Längen- und Breiten-Koordinate sortieren und die jeweils nächsten Werte in beiden Richtungen identifizieren.
Title: Re: [MOD] similar images
Post by: khansahib on June 07, 2011, 12:38:09 PM
Thanks for such a nice MOD.  8)

one request, can we control the number of rows and columns, not from ACP bcoz that changes the whole gallery i want to change number of rows and column for the similar images only..
In my case i've 4 rows with no columns in categories page, but in details page i want to show the similar images in 2 rows and 3 columns.
thanks.
Title: Re: [MOD] similar images
Post by: dp on June 08, 2011, 08:51:00 AM
Thanks for such a nice MOD.  8)

one request, can we control the number of rows and columns, not from ACP bcoz that changes the whole gallery i want to change number of rows and column for the similar images only..
In my case i've 4 rows with no columns in categories page, but in details page i want to show the similar images in 2 rows and 3 columns.
thanks.

To achive this you have to replace the $config['...']-variables with simple values and change the total numbers of displayed images.

So in details.php and only in the MOD's code find:
<table width=\"".$config['image_table_width']."\" border=\"0\" cellpadding=\"".$config['image_table_cellpadding']."\" cellspacing=\"".$config['image_table_cellspacing']."\">
and replace e.g. with:
<table width='50%' border='0' cellpadding='2' cellspacing='4'>
This has to be done at 3 different places. Note: the values given are just examples, you are hard-coding html, adapt it to your needs.


Find:
if ($count == $config['image_cells']) {
Replace with:
if ($count == 2) {

Find:
$leftover = ($config['image_cells'] - $count);
Replace with:
$leftover = (2 - $count);
2 is the number of rows.


At
if ($i_ids > 4) $i_ids = 4;
set the maximum of displayed images. 2 rows and 3 colums will have 2x3=6 images. For your example change it to:
if ($i_ids > 6) $i_ids = 6;

Good luck!
Title: Re: [MOD] similar images
Post by: khansahib on June 08, 2011, 01:24:21 PM
its working...
Thanks Alot... :D
Title: Re: [MOD] similar images
Post by: jotabonfim on June 09, 2011, 09:52:36 PM
god idea

tks :) :) :) :) :) :)


Thanks for such a nice MOD.  8)

one request, can we control the number of rows and columns, not from ACP bcoz that changes the whole gallery i want to change number of rows and column for the similar images only..
In my case i've 4 rows with no columns in categories page, but in details page i want to show the similar images in 2 rows and 3 columns.
thanks.

To achive this you have to replace the $config['...']-variables with simple values and change the total numbers of displayed images.

So in details.php and only in the MOD's code find:
<table width=\"".$config['image_table_width']."\" border=\"0\" cellpadding=\"".$config['image_table_cellpadding']."\" cellspacing=\"".$config['image_table_cellspacing']."\">
and replace e.g. with:
<table width='50%' border='0' cellpadding='2' cellspacing='4'>
This has to be done at 3 different places. Note: the values given are just examples, you are hard-coding html, adapt it to your needs.


Find:
if ($count == $config['image_cells']) {
Replace with:
if ($count == 2) {

Find:
$leftover = ($config['image_cells'] - $count);
Replace with:
$leftover = (2 - $count);
2 is the number of rows.


At
if ($i_ids > 4) $i_ids = 4;
set the maximum of displayed images. 2 rows and 3 colums will have 2x3=6 images. For your example change it to:
if ($i_ids > 6) $i_ids = 6;

Good luck!
Title: Re: [MOD] similar images
Post by: sunl on December 07, 2011, 10:30:07 PM
Good MOD

But when use more keywords images didn't show, even i lower the values. So i try to add category instead keywords but could not figure it out. Anyone know how to do this?
Title: Re: [MOD] similar images
Post by: dp on December 08, 2011, 09:13:43 AM
So i try to add category instead keywords but could not figure it out. Anyone know how to do this?

This dosn't make sense, I think. Based on categories all images of the same category are similar. Self-evident, isn't it? To do so, you simply can use the categories template tags.

Mod similar images does not complex analysis of image description or keywords, it just uses php-own string comparison. So the results strongly depends on the quality of your metadata. If you don't use controlled vocabulary and consistent structure or systematic, results will be unsatisfying. For my experience only less galery operators pay attention to metadata, but metadata are the key to any information processing. To get proper results with this mod, set up your image description and keywords with controlled vocabulary and consistent structure first. This can be very time-expensive, but there's no other way.
Title: Re: [MOD] similar images
Post by: sunl on December 08, 2011, 12:47:52 PM
Yes, It must show all images of category eg: Nature.

Actually i already use keywords in gallery and with those keywords it not working. So i thought it will be good if i use category instead keywords to show similar images from particular category. So i try to replace keyword tag with category tag but its not worked i think i am missing where  actually to put category tag.

If i use it, as it is, then i have to edit all images and its very hard work.

To show images from particular category i think category tag is good option.

Can you tell how to replace  keyword tag with category tag?
Title: Re: [MOD] similar images
Post by: dp on December 08, 2011, 01:08:51 PM
Can you tell how to replace  keyword tag with category tag?

I'm sorry, but this mod performes a special database query on description or keywords. To show all images of a single category, you don't need any mod, just use the normal 4images template tags in your template.
Title: Re: [MOD] similar images
Post by: uploadeur on February 28, 2012, 02:39:46 AM
I need an easier trick;

i need a trick(at left sidebar) which shows 5 or 10 random images from current category.

any ideas?
Title: Re: [MOD] similar images
Post by: dp on February 28, 2012, 07:34:23 AM
I need an easier trick;

i need a trick(at left sidebar) which shows 5 or 10 random images from current category.

any ideas?

You can use V@tno's Mod Random pictures and fit it to your needs: http://www.4homepages.de/forum/index.php?topic=4259.msg17513#msg17513
 (http://www.4homepages.de/forum/index.php?topic=4259.msg17513#msg17513)
Title: Re: [MOD] similar images
Post by: uploadeur on February 28, 2012, 02:03:02 PM
I need an easier trick;

i need a trick(at left sidebar) which shows 5 or 10 random images from current category.

any ideas?

You can use V@tno's Mod Random pictures and fit it to your needs: http://www.4homepages.de/forum/index.php?topic=4259.msg17513#msg17513
 (http://www.4homepages.de/forum/index.php?topic=4259.msg17513#msg17513)

i think i need a easier trick to do this.
Title: Re: [MOD] similar images
Post by: dp on February 28, 2012, 02:09:20 PM
i think i need a easier trick to do this.

Well, you defenitly need to perform a database query on the current category and display the result in the propriate way. The only thing that's easier as v@tno's mod is, to look for a programmer and pay for its work.
Title: Re: [MOD] similar images
Post by: uploadeur on February 28, 2012, 03:35:52 PM
i think i need a easier trick to do this.

Well, you defenitly need to perform a database query on the current category and display the result in the propriate way. The only thing that's easier as v@tno's mod is, to look for a programmer and pay for its work.

i just tried v@tno's code but didnt work :(
Title: Re: [MOD] similar images
Post by: dp on February 28, 2012, 03:58:07 PM
i just tried v@tno's code but didnt work :(

All I can say is, v@tno's mods a pretty good and reliable and I'm using this particular one at my own galery - without problems. Working at 4images core files and modifying them to my needs have been my first experiences in coding php and I had to learn a lot, mostly by trial and error. If you are not familiar with php, this will be the hard way.

Post your problems at the given thread, v@tno for sure will give you kind assistance.
Title: Re: [MOD] similar images
Post by: uploadeur on February 29, 2012, 01:22:13 AM
and your similar images, didnt work for me, i have no english folder in lang folder, maybe problem is this?
Title: Re: [MOD] similar images
Post by: dp on February 29, 2012, 07:57:01 AM
and your similar images, didnt work for me, i have no english folder in lang folder, maybe problem is this?

main.php in folder lang/english just contains the variable for the heading "similar images". If it's not declared and set, only this output should miss, image thumbs should still be there.

Can you describe more detailed your problem?
Title: Re: [MOD] similar images
Post by: relu on November 15, 2012, 05:49:30 PM
my server is crashing after adding this mood. somehow stop responding with 100% server load. I notticed that also images that visible only to admin is showing in similat images.
Title: Re: [MOD] similar images
Post by: dp on November 16, 2012, 09:01:54 AM
my server is crashing after adding this mood. somehow stop responding with 100% server load. I notticed that also images that visible only to admin is showing in similat images.

Hi relu,

do your images have proper keywords or descriptions? The mod as it is uses keywords. If there are none, the main loop will not terminate.

I'm using this mod with a database of nearly 8000 images and extensive metadata without problems. Maybe your gallery is much bigger? I can't guarantee the reliability of this mod for any number of images ...
Title: Re: [MOD] similar images
Post by: relu on November 17, 2012, 12:11:30 PM
My page is heavy mooded and i have around 80.000 images on my page. i figure out twice times when i installed the mood that my server hung up after short time and just after i remove this mod was back. Overall images are with description and keywords right.
Title: Re: [MOD] similar images
Post by: kar76 on July 21, 2013, 02:26:06 PM
Hi

Nice Mode but i am facing one problem with it.
I try to add {cat_name} tag but it not show cattegory name but on detail page this tag works every where
How to fix it?
Title: Re: [MOD] similar images
Post by: kar76 on July 26, 2013, 01:22:21 AM
Does anyone has no idea how to include {cat_name} tag to this mode :?:

In this mod {image_name} tag works but when use {cat_name} it show blank space
i try to figure it out but fail
Title: Re: [MOD] similar images
Post by: Rembrandt on July 26, 2013, 05:51:56 AM
Hi!
Does anyone has no idea how to include {cat_name} tag to this mode :?:
..

search in the mod:

$sql = "SELECT *
        FROM ".IMAGES_TABLE."
        WHERE image_id IN (" . $image_ids . ")


and replace:

$sql = "SELECT i.*, c.cat_name
        FROM (".IMAGES_TABLE." i,  ".CATEGORIES_TABLE." c)
        WHERE image_id IN (" . $image_ids . ")AND c.cat_id = i.cat_id

search:

show_image($image_row_similarimages);

insert below:

    $site_template->register_vars(array(
      "cat_name" => format_text($image_row_similarimages['cat_name'], 2)
    ));


mfg Andi
Title: Re: [MOD] similar images
Post by: Starblade on October 23, 2014, 01:12:19 PM
Hallo,

habe es eben mal eingebaut und leider läuft es nicht. Anstelle der Bilder, wird nur der Text " similar images " angezeigt.

Ist doch richtig das sich die Suche nach den Keywords und oder der Bildbeschreibung richtet, oder ?
Title: Re: [MOD] similar images
Post by: Rembrandt on October 23, 2014, 03:27:05 PM
Hast du das "{similar_images}" in die HTML geschrieben dann sollte es funktionieren, seinerzeit hatte ich den Mod getestet und er funktionierte, ansonsten hätte ich ihn nicht verschoben.