Advanced search  

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Pages: [1]   Go Down

Author Topic: Histogram added.  (Read 39678 times)

0 Members and 1 Guest are viewing this topic.

Spaz

  • Contributor
  • Coppermine newbie
  • ***
  • Offline Offline
  • Posts: 10
Histogram added.
« on: June 09, 2005, 11:15:22 pm »

Hi everyone,

This is my first real post here..I've been using Coppermine for a while now..it just does such a better job than the others.

I've written some code, that I've added to my site, that I thought you may like.  It is linked from the displayimage.php page, and creates a histogram of the image being viewed.  The histogram has a total (white), plus the individual RGB (lines) values.  The histogram is saved to your normal image folder, and if the image is relooked at, the saved file is loaded, rather than trying to recreate it.

There isnt much error checking in it.  I know that it requires GD, but it doesnt check for it.  It also assumes you have a JPG image.

In displayimage.php there is one line added in function html_picinfo()

Code: [Select]
    $info[$lang_picinfo['Histogram']]="<img src='histo.php?image=".$CURRENT_PIC_DATA['filename']."&filepath=".$CURRENT_PIC_DATA['filepath']."'>";

    return theme_html_picinfo($info);
}

In the same directory as displayimage.php create a file called histo.php

Here is the code.  Feel free to use/abuse/edit/change the code to your hearts content.  Please leave the opening comments in, so I get credit at least :) 

Oh, and feel free to email me anton_spaz@yahoo.com any thoughts, comments, or changes.

Code: [Select]
<?php
//      Histogram creation
//      Created by Anton Sparrius (Spaz) 6/9/05  anton_spaz@yahoo.com
//      Free to use and change, provided you keep these lines :)
//
                        
$image $_REQUEST['image'];
                        
$filepath $_REQUEST['filepath'];
                        
$path "albums/".$filepath;

                        
$hist_file="hist_".substr($image,0,strlen($image)-4).".png";


                        if (
file_exists($path.$hist_file)) {
                          
$im=imagecreatefromPNG($path $hist_file);
                          
imagePNG($im);
                          
imagedestroy($im);
                        } else {
                        
$im=imagecreatefromjpeg($path.$image);
                        for(
$i=0;$i<imagesx($im);$i+=2)
                        {
                                for(
$j=0;$j<imagesy($im);$j++)
                                {
                                        
$rrggbb=imagecolorsforindex ($imimagecolorat($im,$i,$j));
                                        
$r[$rrggbb['red']]+=1;
                                        
$g[$rrggbb['green']]+=1;
                                        
$b[$rrggbb['blue']]+=1;
                                }
                        }
                        for (
$i=0;$i<256;$i++)
                        {
                                
$max[$i]=($r[$i]+$g[$i]+$b[$i])/3;
                        }
                        
$max_value=max($max)/150;
                        
$m[0]=max($r);
                        
$m[1]=max($b);
                        
$m[2]=max($g);
                        
$max_rgb=max($m)/150;

                        
$im_out imageCreate (280164);
                        
$background imageColorAllocate($im_out,70,70,70);
                        
$hist=ImageColorAllocate($im_out,171,205,239);
                        
$white=ImageColorAllocate($im_out,255,255,255);
                        
$red=ImageColorAllocate($im_out,255,0,0);
                        
$green=ImageColorAllocate($im_out,0,255,0);
                        
$blue=ImageColorAllocate($im_out,0,0,255);
                        
$ry=107;
                        
$gy=107;
                        
$by=107;

                        for(
$i=0;$i<256;$i++)
                        {
                                
imageLine($im_out$i+14157$i+14157-($max[$i]/$max_value),$white);
                                
imageLine($im_out$i+13$ry$i+14157-($r[$i]/$max_rgb), $red);
                                
imageLine($im_out$i+13$gy$i+14157-($g[$i]/$max_rgb), $green);
                                
imageLine($im_out$i+13$by$i+14157-($b[$i]/$max_rgb), $blue);
                                
$ry=157-($r[$i]/$max_rgb);
                                
$gy=157-($g[$i]/$max_rgb);
                                
$by=157-($b[$i]/$max_rgb);
                        }
                        
imageLine($im_out,13,158,270,158,$hist);
                        
imageLine($im_out,13,6,270,6,$hist);
                        
imageLine($im_out,13,6,13,158,$hist);
                        
imageLine($im_out,270,6,270,158,$hist);

                        
imagePNG($im_out,$path $hist_file);
                        
imageDestroy($im);
                        
imagedestroy($im_out);
                        
$im=imagecreatefromPNG($path $hist_file);
                        
imagePNG($im);
                        
imagedestroy($im);
                        }
?>


« Last Edit: July 28, 2005, 06:13:53 am by GauGau »
Logged

Spaz

  • Contributor
  • Coppermine newbie
  • ***
  • Offline Offline
  • Posts: 10
Re: Histogram added.
« Reply #1 on: June 10, 2005, 03:12:07 am »

I size my images prior to uploading to CM, since I apply sharpening to the image size, rather than let ImageMagick do as it wishes, I didnt notice a bug.  If you try to go off full sized images eg 8mpix, the histogram will not build, as there is a limit that gets reached.  I will fix this tomorrow.  I will also add detection for JPG/GIF/PNG.  And, I believe I can add an easy check to avoid running if GD is not installed.

Stay tuned tomorrow....same bat channel..
Logged

kegobeer

  • Dev Team member
  • Coppermine addict
  • ****
  • Offline Offline
  • Gender: Male
  • Posts: 4637
  • Beer - it does a body good!
    • The Kazebeer Family Website
Re: Histogram added.
« Reply #2 on: June 10, 2005, 03:54:05 am »

You'll have to check for GD 2.0.28+, which includes GIF creation support.  Earlier versions don't support GIF creation.  Any GIF manipulation with an old version will result in failure.
Logged
Do not send me a private message unless I ask for one.  Make your post public so everyone can benefit.

There are no stupid questions
But there are a LOT of inquisitive idiots

snork13

  • Contributor
  • Coppermine frequent poster
  • ***
  • Offline Offline
  • Gender: Male
  • Posts: 260
  • Internet! Is that thing still around?
    • Gallery
Re: Histogram added.
« Reply #3 on: June 10, 2005, 04:47:31 am »

@Spaz


thanks for the cool mod, I attached a screen shot for others ;D

--snork13
Logged

Spaz

  • Contributor
  • Coppermine newbie
  • ***
  • Offline Offline
  • Posts: 10
Re: Histogram added.
« Reply #4 on: June 10, 2005, 01:46:59 pm »

You'll have to check for GD 2.0.28+, which includes GIF creation support.  Earlier versions don't support GIF creation.  Any GIF manipulation with an old version will result in failure.

Should I even bother with GIF support?  Since we are talking about photos being displayed, for web viewing, I would have to assume that JPG is used pretty much all the time.  TIFFs would be too large, GIF wouldnt have good enough quality.

Is there any information available about what file format people use to display their images?

As for standard GD detection, I've changed the line in displayimage.php to include a simple check.  If it doesnt find the ImageType function, than no GD is available, and it wont ever run histogram.php

Code: [Select]
   

 if (function_exists('ImageTypes')) {
      $info[$lang_picinfo['Histogram']]="<img src='histo.php?image=".$CURRENT_PIC_DATA['filename']."&filepath=".$CURRENT_PIC_DATA['filepath']."'>";
    }
    return theme_html_picinfo($info);
}

Logged

Spaz

  • Contributor
  • Coppermine newbie
  • ***
  • Offline Offline
  • Posts: 10
Histogram added - Updated
« Reply #5 on: June 10, 2005, 04:23:37 pm »

Here is the new code.  Changes include :

displayimage.php :

1) Copied code to get file name of the image displayed.  If the image is an intermediate image, a user-definable prefix (defaults to normal_ is added to the filename.  The intermediate image is used for the histogram creation, because it returns results that are the same as the full sized image, and the histogram is created quicker with smaller file sizes.

2) Since a histogram is really only relevant for photos, and since Coppermine is a photo gallery software, and since JPG is used to get a good quality image, with low file size, I've added code to only do the histogram from a JPG

3) The entire code is wrapped in a simple GD detection.  If GD is not installed, none of the code will be run.

Again, this is in the function html_picinfo() in displayimage.php
Code: [Select]
// Added 6/10/05 - Anton Sparrius
// Creation/Display of histogram

    if (function_exists('ImageTypes')) {  // Only do if GD exists

      if($CONFIG['thumb_use']=='ht' && $CURRENT_PIC_DATA['pheight'] > $CONFIG['picture_width'] ){ // The wierd comparision is because only picture_width is $
        $condition = true;
      }elseif($CONFIG['thumb_use']=='wd' && $CURRENT_PIC_DATA['pwidth'] > $CONFIG['picture_width']){
        $condition = true;
      }elseif($CONFIG['thumb_use']=='any' && max($CURRENT_PIC_DATA['pwidth'], $CURRENT_PIC_DATA['pheight']) > $CONFIG['picture_width']){
        $condition = true;
      }else{
       $condition = false;
      }

      if ($CONFIG['make_intermediate'] && $condition ) {
          $picture_url = get_pic_url($CURRENT_PIC_DATA, 'normal');
      } else {
          $picture_url = get_pic_url($CURRENT_PIC_DATA, 'fullsize');
      }

      if (strtoupper(substr($picture_url,strlen($picture_url)-3,3))=="JPG") {
        $info[$lang_picinfo['Histogram']]="<img src='histo.php?image=".$picture_url."'>";
      }
    }

// End Addition

    return theme_html_picinfo($info);
}

In the Coppermine root folder, create a file called histo.php with the following code.

Changes include adjustments for the new file name (normal_ etc) and cosmetic changes to the actual histogram.  A PNG is created in the same folder as the image.  It looks like the PNG is normally around 2k.

Code: [Select]
<?php
//      Histogram creation
//      Created by Anton Sparrius (Spaz) 6/9/05  anton_spaz@yahoo.com
//      Free to use and change, provided you keep these lines :)
//
                        
$tmp_image $_REQUEST['image'];
                        
$image_array=split('[/\]',$tmp_image);
                        
$image=$image_array[count($image_array)-1];
                        
$path=rtrim($tmp_image,$image);

                        
$hist_file="hist_".substr($image,0,strlen($image)-4).".png";

                        if (
file_exists($path.$hist_file)) {
                          
$im=imagecreatefromPNG($path $hist_file);
                          
imagePNG($im);
                          
imagedestroy($im);
                        } else {
                        
$im=imagecreatefromjpeg($path.$image);
                        for(
$i=0;$i<imagesx($im);$i+=2)
                        {
                                for(
$j=0;$j<imagesy($im);$j++)
                                {
                                        
$rrggbb=imagecolorsforindex ($imimagecolorat($im,$i,$j));
                                        
$r[$rrggbb['red']]+=1;
                                        
$g[$rrggbb['green']]+=1;
                                        
$b[$rrggbb['blue']]+=1;
                                }
                        }
                        for (
$i=0;$i<256;$i++)
                        {
                                
$max[$i]=($r[$i]+$g[$i]+$b[$i])/3;
                        }
                        
$max_value=max($max)/150;
                        
$m[0]=max($r);
                        
$m[1]=max($b);
                        
$m[2]=max($g);
                        
$max_rgb=max($m)/150;

                        
$im_out imageCreate (284164);
                        
$background imageColorAllocate($im_out,100,100,100);
                        
$box_fill   imageColorAllocate($im_out,70,70,70);
                        
$white=ImageColorAllocate($im_out,200,200,200);
                        
$black=ImageColorAllocate($im_out,20,20,20);
                        
$grey=ImageColorAllocate($im_out,160,160,160);
                        
$red=ImageColorAllocate($im_out,255,0,0);
                        
$green=ImageColorAllocate($im_out,0,200,0);
                        
$blue=ImageColorAllocate($im_out,0,0,255);
                        
$ry=107;
                        
$gy=107;
                        
$by=107;

                        
imageFilledRectangle($im_out,13,6,270,158,$box_fill);

                        for(
$i=0;$i<256;$i++)
                        {
                                
imageLine($im_out$i+14157$i+14157-($max[$i]/$max_value),$white);
                                
imageLine($im_out$i+13$ry$i+14157-($r[$i]/$max_rgb), $red);
                                
imageLine($im_out$i+13$gy$i+14157-($g[$i]/$max_rgb), $green);
                                
imageLine($im_out$i+13$by$i+14157-($b[$i]/$max_rgb), $blue);
                                
$ry=157-($r[$i]/$max_rgb);
                                
$gy=157-($g[$i]/$max_rgb);
                                
$by=157-($b[$i]/$max_rgb);
                        }
                        
imageSetThickness($im_out,3);
                        
imageLine($im_out,13,158,272,158,$grey);
                        
imageLine($im_out,13,6,272,6,$black);
                        
imageLine($im_out,13,6,13,158,$black);
                        
imageLine($im_out,272,6,272,158,$grey);

                        
imagePNG($im_out,$path $hist_file);
                        
imageDestroy($im);
                        
imagedestroy($im_out);
                        
$im=imagecreatefromPNG($path $hist_file);
                        
imagePNG($im);
                        
imagedestroy($im);
                        }
?>


Example of the histogram.  Note, if you have the image information turned off, you will not see the histogram.

(http://spaz.is-a-geek.com/pics/albums/userpics/10001/histo_example.jpg)
Logged

Spaz

  • Contributor
  • Coppermine newbie
  • ***
  • Offline Offline
  • Posts: 10
Re: Histogram added.
« Reply #6 on: June 10, 2005, 05:21:31 pm »

@Spaz


thanks for the cool mod, I attached a screen shot for others ;D

--snork13

Thanks :)  Question..did you have to change code somewhere to get the word "Histogram" to show up?  On mine, it doesnt list anything, just ":"
Logged

djarnis

  • Coppermine newbie
  • Offline Offline
  • Gender: Male
  • Posts: 2
    • Jole's Place
Re: Histogram added.
« Reply #7 on: July 01, 2005, 11:58:50 am »

Thanks :) Question..did you have to change code somewhere to get the word "Histogram" to show up? On mine, it doesnt list anything, just ":"
Just to make this mod complete, then you have to add a line in the language-file in order to get the word "Histogram" to show up.to
i.e. lang/english.php
Find "$lang_picinfo = array(" and add the following line to that array:
Code: [Select]
        'Histogram'=>'Histogram',
« Last Edit: July 02, 2005, 11:59:20 am by djarnis »
Logged
/dj

Spaz

  • Contributor
  • Coppermine newbie
  • ***
  • Offline Offline
  • Posts: 10
Re: Histogram added.
« Reply #8 on: July 27, 2005, 04:40:31 pm »

This patch works fine in 1.41 Beta as well.
Logged

flux

  • Contributor
  • Coppermine regular visitor
  • ***
  • Offline Offline
  • Posts: 80
    • CPG Remote Hack Demo
Re: Histogram added.
« Reply #9 on: January 03, 2006, 03:13:27 pm »

hi that mod looks interesting but i've got a really stupid question..

What are histograms good for ? can it be used to compare two pictures to see if they look alike ?

got redirected to this thread by Gaugau answering a question i posted there : http://forum.coppermine-gallery.net/index.php?topic=25818.0

I was wondering if there were any ways to build some kind of CRC value of a picture in order to "visually" compare two pictures even though their technical caracteristics are different (size, resolution, depth..)..

I've been trying to find some keywords on the subject and read all i could find using ImageMagick or GDlib but no luck so far..

If you're into image analysis maybe you can help me with that or point me in the right direction..
thx

ecto

  • Supporter
  • Coppermine frequent poster
  • ****
  • Offline Offline
  • Gender: Male
  • Posts: 144
    • My very incomplete gallery
Re: Histogram added.
« Reply #10 on: January 03, 2006, 05:20:56 pm »

What are histograms good for ? can it be used to compare two pictures to see if they look alike ?

Here's a really good introduction to histograms: http://www.luminous-landscape.com/tutorials/understanding-series/understanding-histograms.shtml
Logged

flux

  • Contributor
  • Coppermine regular visitor
  • ***
  • Offline Offline
  • Posts: 80
    • CPG Remote Hack Demo
Re: Histogram added.
« Reply #11 on: January 03, 2006, 11:50:55 pm »

very interesting indeed.
unfortunately that's not what i'm looking for..

thx

Tomi1977

  • Coppermine newbie
  • Offline Offline
  • Posts: 2
Re: Histogram added.
« Reply #12 on: April 25, 2006, 08:56:07 pm »

hallo!
i want to change the background for the histogramm.. i want to insert an image as background for it.. how can i do this..??
Logged

Spaz

  • Contributor
  • Coppermine newbie
  • ***
  • Offline Offline
  • Posts: 10
Re: Histogram added.
« Reply #13 on: April 26, 2006, 07:10:38 pm »

You cant.

The histograph is created live, by anaylising the image you feed it.  It's then generates a image file by drawing lines.

Sorry.

hallo!
i want to change the background for the histogramm.. i want to insert an image as background for it.. how can i do this..??
Logged

Tomi1977

  • Coppermine newbie
  • Offline Offline
  • Posts: 2
Re: Histogram added.
« Reply #14 on: April 26, 2006, 09:10:30 pm »

i`ve made it..  :P
so it`s possible to do it..
Logged

Joachim Müller

  • Dev Team member
  • Coppermine addict
  • ****
  • Offline Offline
  • Gender: Male
  • Posts: 47843
  • aka "GauGau"
    • gaugau.de
Re: Histogram added.
« Reply #15 on: April 27, 2006, 09:51:59 am »

then why don't you tell us how you did?
Logged

hlabout

  • Coppermine frequent poster
  • ***
  • Offline Offline
  • Gender: Male
  • Posts: 118
    • Harald Labout Fotografie
Re: Histogram added.
« Reply #16 on: August 23, 2007, 02:35:19 pm »

Thanks for this really cool mod.

Regards,

Harald

www.haraldlabout.nl/fotografie
Logged

mykee

  • Contributor
  • Coppermine frequent poster
  • ***
  • Country: hu
  • Offline Offline
  • Gender: Male
  • Posts: 140
    • One moment from Hungary
Re: Histogram added.
« Reply #17 on: November 30, 2007, 08:44:23 pm »

I use ImageMagick, no GD. Can change this mod to ImageMagick users?
Logged
My gallery and demo for AnythingSlider, Photocols and CollagePlus plugin: http://foto.acegem.hu/index.php

Joachim Müller

  • Dev Team member
  • Coppermine addict
  • ****
  • Offline Offline
  • Gender: Male
  • Posts: 47843
  • aka "GauGau"
    • gaugau.de
Re: Histogram added.
« Reply #18 on: December 03, 2007, 08:40:06 am »

If you code it: yes.
Logged

Heroe

  • Coppermine frequent poster
  • ***
  • Offline Offline
  • Gender: Male
  • Posts: 124
    • abroadbg.com
Re: Histogram added.
« Reply #19 on: February 03, 2009, 08:51:40 pm »

I have question about this mod,i have at on my gallery but every time when i do batch upload i have the histo_png selected,how to made the histo_png files to be not selected,because if i have 50 pictures i will have 50 histo_png and i must unmark them one by one.
Logged
Pages: [1]   Go Up
 

Page created in 0.043 seconds with 20 queries.