News:

CPG Release 1.6.28
added submissions from {406man}
cleaned up a few PHP (8.4) deprecations
fixed PHP deprecation in calendar
removed security vulnerability
(please upgrade when possible)

Main Menu

Watermark Image Protection

Started by DJAxion, September 25, 2003, 10:22:41 PM

Previous topic - Next topic

0 Members and 5 Guests are viewing this topic.

DJAxion

As promised: the codes.

First create a file called 'logo.php' with this content:
<?
// SCHIPT BY DJ AXION
// e-mail: DJ@the-expansion.com
// Enjoy this script!

// GET SOURCE PICTURE FROM FORM INPUT

include("imageinfo.php");
 
$myimage = New ImageInformation($picturename);

// These Version Jiggleing is pain in the ass

if (function_exists("imagegif")) {
  $gi["GIF Read Support"] = 1;
}elseif (function_exists("imagejpeg")) {
  $gi["JPG Support"] = 1;
}elseif (function_exists("imagepng")) {
  $gi["PNG Support"] = 1;
}

// Try to dicide
if ($type = $myimage->format) {
   switch ($type) {
      case 'JPEG' :
         if ($gi["JPG Support"]){
            $b = imagecreatefromjpeg($picturename);
         } else {
            $image = $picturename;
         }
      break;
      case 'GIF' :
         if ($gi["GIF Read Support"]) {
            $b = imagecreatefromgif($picturename);
         } else {
            $image = $picturename;
         }
      break;
      case 'PNG' :
         if ($gi["PNG Support"]){
            $b = imagecreatefrompng($picturename);
         } else {
            $image = $picturename;
         }
      break;
   }
}

$bx = $myimage->details["width"];// source width
$by = $myimage->details["height"]; // source height

if(!empty($b)) {
   $lm = $b;

   if ($bx > 200)  {  // this ensures no watermark is added to small images (e.g. thumbnails)
   // ADD WATERMARK
      $pos = "bottomright"; //where is the watermark displayed...

      if ($pos == "topleft") {
         $src_x = 0;
         $src_y = 0;
      } else if ($pos == "topright") {
         $src_x = $bx - 193;
         $src_y = 0;
      } else if ($pos == "bottomleft") {
         $src_x = 0;
         $src_y = $by - 187;
      } else if ($pos == "bottomright") {
         $src_x = $bx - 90;
         $src_y = $by - 90;
      }
   // THIS IS THE CODE FOR THE WATERMARK
      ImageAlphaBlending($lm, true) or die ("Could not alpha blend"); // Enable when on GD 2+
      $logoImage = ImageCreateFromPNG('logo.png'); // logo.png is a watermark it add's...
      $logoW = ImageSX($logoImage);
      $logoH = ImageSY($logoImage);
      ImageCopy($lm,$logoImage,$src_x,$src_y,0,0,$logoW,$logoH);
   }
}
if ($myimage->format && !empty($b)) {
   Header("Content-type: image/$type");
   switch ($type) {
      case 'JPEG' :
         Imagejpeg($lm,'',60); //80 means JPEG quality
      break;
      case 'GIF' :
         ImageGif($lm);
      break;
      case 'PNG' :
         ImagePnG($lm);
      break;
   }
imageDestroy($lm);

} else {
   // No usable GD image Function ? Ok preventing brocken Images and give back the untouched Image
   Header("Content-type: image/$type");
   $fp = fopen($image,"r");
   $fa = fread($fp,filesize ($image));
   print($fa);
   fclose($fp);
}    
?>


Then a file 'imageinfo.php':
<?php

/*
        ImageInformation PHP Class
         
        This PHP class takes a local file as its input and will determine
        if the file is a BMP, GIF, JPEG or PNG.  It will also extract 
        other generic information about the image, including resolution, 
        height and width &#40;without using the builtin GetImageSize&#40;&#41; function&#41;
        as well as other image type specific information.
        
        ImageInformation class is a part port of the perl module Image-Info.
        
        I have not ported all of it as I really only required resolution, 
        jpeg_type and some other bits from gifs, jpegs and pngs.  But I have
        done some other bits as well.  If anybody implements this better or
        cleans it up send me a copy please.
        
        Download here ImageInformation.tar.gz.
        
        mike@nux.co.uk
        http&#58;//mike.nux.co.uk/
        
        ---------------------------------------------------------------
        
        usage&#58;
                include&#40;$includepath."get_imgsize.inc"&#41;;
                $myimage = New ImageInformation&#40;$file&#41;;

                $width = $myimage->details["width"&#93;;
                $height = $myimage->details["height"&#93;;

        show full picture information
                print_r&#40;$myimage->details&#41;;
                
        show errors&#58;
                echo $myimage->errors
        
        show file-Format&#58;
                echo $myimage->format
        
        ---------------------------------------------------------------
        
        External Files &#40;BMP.inc, GIF.inc, JPEG.inc, PNG.inc included into this
        Class by Christian Rütgers <chr@1komma6.com> &#40;www.1komma6.com&#41;
        
        Changed some attribut-strings from details[&#93;

*/
Class ImageInformation &#123;
        
var $errors;
        var 
$source;
        var 
$details;
        var 
$format;
        function 
ImageInformation&#40;$myfile&#41;&#123;
                
$this->source fopen &#40;$myfile, "r"&#41;;
                
$format $this->GetFileFormat&#40;&#41;;
                
if&#40;$format != "unknown"&#41;&#123;
                        
$this->format $format;
                        switch&
#40;$this->format&#41; &#123;
                                
case 'GIF'&#58;
                                        
$this->details $this->ProcessFileGIF&#40;$this->source&#41;;
                                        
break;
                                case 
'JPEG'&#58;
                                        
$this->details $this->ProcessFileJPEG&#40;$this->source&#41;;
                                        
break;
                                case 
'BMP'&#58;
                                        
$this->details $this->ProcessFileBMP&#40;$this->source&#41;;
                                        
break;
                                case 
'PNG'&#58;
                                        
$this->details $this->ProcessFilePNG&#40;$this->source&#41;;
                                        
break;
                        &
#125;
                
&#125;else&#123;
                        
$this->details = array&#40;&#41;;
                        
$this->format "unknown";
                &
#125;
                
fclose &#40;$this->source&#41;;
        
&#125;

        
function GetFileFormat&#40;&#41;&#123;
                
$head fread&#40;$this->source, 32&#41;;        
            
if&#40;preg_match&#40;"/^\xFF\xD8/",$head&#41;&#41;&#123;
                        
return "JPEG";
            &
#125;elseif&#40;preg_match&#40;"/^GIF8[79&#93;a/",$head&#41;&#41;&#123;
                         
return "GIF";
            &
#125;elseif&#40;preg_match&#40;"/^BM/",$head&#41;&#41;&#123;                           
                        
return "BMP";
            &
#125;elseif&#40;preg_match&#40;"/^\x89PNG\x0d\x0a\x1a\x0a/",$head&#41;&#41;&#123;                           
                        
return "PNG";
            &
#125;else&#123;
                        
return "unknown";
            &
#125;
            #return "ASCII" if /^[\011\012\015\040-\176&#93;*$/ && $_ !~ m%&#40;^<\?xml&#41;|&#40;^#define\s+&#41;|&#40;^/\* XPM \*/&#41;|&#40;static\s+char\s+\*\w+\[\&#93;\s*=\s*&#123;\s*"\d+&#41;%;
            #return "PPM" if /^P[1-6&#93;/;;
            #return "SVG" if /^<\?xml/;
                #return "XBM" if /^#define\s+/;
                #return "XPM" if /&#40;^\/\* XPM \*\/&#41;|&#40;static\s+char\s+\*\w+\[\&#93;\s*=\s*&#123;\s*"\d+&#41;/;
        
&#125;
        
        
        
function ShowDetails&#40;&#41;&#123;
                
$keys array_keys&#40;$this->details&#41;;
                
$out '';
                foreach &
#40;$keys as $key&#41; &#123;
                        
$out .= $key."&#58; ".$this->details[$key&#93;."<BR>";
                
                
&#125;
                
return $out;        
        &
#125;

        
function ProcessFileGIF &#40;$source&#41;&#123;
                #Global $ImageInformation;
                
rewind&#40;$source&#41;;
                
$head fread &#40;$source, 13&#41;;
                
if&#40;!eregi &#40;"^GIF&#40;8[79&#93;a&#41;", $head&#41;&#41;&#123;
                        
die &#40;"Bad GIF signature"&#41;;
                
&#125;
                
preg_match&#40;"/^GIF&#40;8[79&#93;a&#41;/",$head,$matches&#41;;
                
$version $matches[0&#93;;
                
$head str_replace&#40;$matches[0&#93;,"",$head&#41;;
                
$version str_replace&#40;"GIF","",$version&#41;;
                
$ImageInformation["GIF_Version"&#93; = $version;
                
                
$vars unpack&#40;"vsw/vsh/Cpacked/Cbg/Caspect",$head&#41;;
                
                #showdata&#40;$vars&#41;;
                
                
$ImageInformation["width"&#93; = $vars["sw"&#93;;
                
$ImageInformation["height"&#93; = $vars["sh"&#93;;
                
                
            
$color_table_size = &#40;1 << &#40;&#40;$vars["packed"&#93; & 7&#41; + 1&#41;&#41;;
            
$ImageInformation["ColorTableSize"&#93; = $color_table_size;
        
                //not sure below stuff is return the correct values
                
if&#40;&#40;$vars["packed"&#93; & 8&#41; > 0&#41;&#123;
                        
$sorted_colors = &#40;$vars["packed"&#93; & 8&#41;;
                
&#125;else&#123;
                        
$sorted_colors 0;
                &
#125;
                
$ImageInformation["SortedColors"&#93; = $sorted_colors;
                
        
                
                
if &#40;$version == "89a"&#41;&#123;
        
                        
$ImageInformation["ColorResolution"&#93; = &#40;&#40;&#40;$vars["packed"&#93; & 112&#41; >> 4&#41; + 1&#41;;
        
                    
$global_color_table = &#40;$vars["packed"&#93; & 128&#41;;
                    
if&#40;$global_color_table&#41;&#123;
                            
$ImageInformation["GlobalColorTableFlag"&#93; = 1;
                            
$ImageInformation["BackgroundColor"&#93; = $vars["bg"&#93;;
                    
&#125;else&#123;
                            
$ImageInformation["GlobalColorTableFlag"&#93; = 0;
                    
&#125;
        
                        
if&#40;$vars["aspect"&#93;&#41;&#123;
                                
$aspect = &#40;$vars["aspect"&#93; + 15&#41; / 64;                
                                
$ImageInformation["PixelAspectRatio"&#93; = $aspect;
                                
$ImageInformation["resolution"&#93; = "1/".$aspect;
                        
&#125;else&#123;
                                
$ImageInformation["resolution"&#93; = "1/1";
                        
&#125;
            
&#125;
                
$ImageInformation["file_media_type"&#93; = "image/gif";
                
$ImageInformation["file_ext"&#93; = "gif";  
        
                
return $ImageInformation;
        
        &
#125;

        
function ProcessFileBMP&#40;$source&#41;&#123;        
        
                
$ImageInformation = array&#40;&#41;;
                
rewind&#40;$source&#41;;
        
                
$buf fread&#40;$source, 54&#41;;
                
$header unpack&#40;"Szero/Lone/Stwo/Sthree/Lfour/Lfive/Vwidth/Vheight/Ssamplesperpixel/Scolor_type/Lfile_ext/Leleven/Vresa/Vresb/Lcolortablesize/Lcolorsimportant", $buf&#41;;
                
extract&#40;unpack&#40;"Szero/Lone/Stwo/Sthree/Lfour/Lfive/Vwidth/Vheight/Ssamplesperpixel/Scolor_type/Lfile_ext/Leleven/Vresa/Vresb/Lcolortablesize/Lcolorsimportant", $buf&#41;&#41;;
                
                
$total += strlen&#40;$buf&#41;;
                
$ImageInformation["file_media_type"&#93; = "image/bmp";
                
if&#40;&#40;$file_ext == 1&#41; || &#40;$file_ext == 2&#41;&#41;&#123;
                        
$ImageInformation["file_ext"&#93; = "rle";
                
&#125;else&#123;
                        
$ImageInformation["file_ext"&#93; = "bmp";
                
&#125;
        
                
$ImageInformation["width"&#93; = abs&#40;$width&#41;;
                
$ImageInformation["height"&#93; = abs&#40;$height&#41;;
        
                
if&#40;$color_type & &#40;$color_type < 24&#41;&#41;&#123;
                        
$ImageInformation["color_type"&#93; = "Indexed-RGB";
                
&#125;else&#123;
                        
$ImageInformation["color_type"&#93; = "RGB";
                
&#125;
                
$ImageInformation["SamplesPerPixel"&#93; = $samplesperpixel;
                
$ImageInformation["BitsPerSample"&#93; = $color_type;
                
$ImageInformation["resolution"&#93; = "$resa/$resb";
                
$ImageInformation["BMP_ColorsImportant"&#93; = $colorsimportant;
                
$ImageInformation["ColorTableSize"&#93; = $colortablesize;
        
                
if&#40;$height > 1&#41; &#123;
                        
$ImageInformation["BMP_Origin"&#93; = 1; //dunno if this is correct
                
&#125;else&#123;
                        
$ImageInformation["BMP_Origin"&#93; = 0; //dunno if this is correct
                
&#125;
        
                
$compression $file_ext;
                
$compressions = array&#40;'none','RLE8','RLE4','BITFIELDS','JPEG','PNG'&#41;;
                
if&#40;isset&#40;$compressions[$compression&#93;&#41;&#41;&#123;
                        
$compression $compressions[$compression&#93;;
                
&#125;
                
$ImageInformation["Compression"&#93; = $compression; 
                
return $ImageInformation;
        &
#125;

        
function ProcessFilePNG &#40;$source&#41;&#123;
                
$ImageInformation = array&#40;&#41;;
                
rewind&#40;$source&#41;;
                
$signature fread&#40;$source, 8&#41;;
                
if&#40;$signature != "\x89PNG\x0d\x0a\x1a\x0a"&#41;&#123;
                        
die&#40;"Bad PNG signature"&#41;;
                
&#125;
                
$ImageInformation["file_media_type"&#93; = "image/png";
                
$ImageInformation["file_ext"&#93; = "png";
        
                
while&#40;1&#41;&#123;
        
                        
extract&#40;unpack&#40;"Nlen/a4type", fread&#40;$source, 8&#41;&#41;&#41;;
                        
                        /*
                        if&#40;$chunks&#41;&#123;
                                $last = prev&#40;$chunks&#41;;
                                end&#40;$chunks&#41;;
                                //extract count digits from $last
                                preg_match&#40;"/\s&#40;\d+&#41;$/",$last,$matches&#41;;
                                if&#40;$matches[0&#93;&#41;&#123;
                                        $last = str_replace&#40;$matches[0&#93;,"",$last&#41;;
                                        $count = $matches[0&#93;;
                                &#125;else&#123;
                                        $count = 1;
                                &#125;
                                if&#40;$last == $type&#41;&#123;
                                        $count++;
                                        $chunks[$count-1&#93; = "$type $count";
                                &#125;else&#123;
                                        array_push&#40;$chunks,$type&#41;;
                                &#125;
                        &#125;else&#123;
                                $chunks = array&#40;&#41;;
                                array_push&#40;$chunks,$type&#41;;
                                
                        &#125;
                        */
                        
if &#40;$type == "IEND"&#41;&#123;
                                
break;
                        &
#125;
        
                        
$data fread&#40;$source, $len + 4&#41;;
                #        $datavars = unpack&#40;"Nmycrc", substr&#40;$data, -4, 4&#41;&#41;;
        
                        
if &#40;&#40;$type == "IHDR"&#41; && &#40;$len == 13&#41;&#41; &#123;
                                
                                
extract&#40;unpack&#40;"Nwidth/Nheight/Cdepth/Cctype/Ccompression/Cfilter/Cinterlace", $data&#41;&#41;;
                                
                                
$ImageInformation["width"&#93; = $width;
                                
$ImageInformation["height"&#93; = $height;
                                
$ImageInformation["SampleFormat"&#93; = "U$depth";
                            
                                
$ctypes = array&#40;0=>"Gray",2=>"RGB",3=>"Indexed-RGB",4=>"GrayA",6=>"RGBA"&#41;;
                                
if&#40;isset&#40;$ctypes[$ctype&#93;&#41;&#41;&#123;
                                        
$ImageInformation["color_type"&#93; = $ctypes[$ctype&#93;;
                                
&#125;else&#123;
                                        
$ImageInformation["color_type"&#93; = "PNG-$ctype";
                                
&#125;
        
                                
if&#40;$compression == 0&#41;&#123;
                                        
$compression "Deflate";
                                &
#125;
                                
$ImageInformation["Compression"&#93; = $compression;
                                                        
                                
if&#40;$filter == 0&#41;&#123;
                                        
$filter "Adaptive";
                                &
#125;
                                
$ImageInformation["PNG_Filter"&#93; = $filter;
                                
                                
if&#40;$interlace == 1&#41;&#123;
                                        
$interlace "Adam7";
                                &
#125;
                                
$ImageInformation["Interlace"&#93; = $interlace;
                                        
                        
&#125;elseif&#40;$type == "PLTE"&#41;&#123;  
                                
$paltable = array&#40;&#41;;
                                
$no 0;
                                while&
#40;$no < strlen&#40;$data&#41;&#41;&#123;
                                        
extract&#40;unpack&#40;"Cchar1/Cchar2/Cchar3", substr&#40;$data, 0, 3&#41;&#41;&#41;;
                                        
array_push&#40;$paltable,sprintf&#40;"#%02x%02x%02x",$char1,$char2,$char3&#41;&#41;;
                                        
$no++;        
                                &
#125;
                            
$ImageInformation["ColorPalette"&#93; = $paltable;  //dunno if this is correct
                        
&#125;elseif&#40;&#40;$type == "gAMA"&#41; && &#40;$len == 4&#41;&#41;&#123;
                                
extract&#40;unpack&#40;"Ngamma", $data&#41;&#41;;
                                
$ImageInformation["Gamma"&#93; = $gamma/100000;
                        
&#125;elseif&#40;&#40;$type == "pHYs"&#41; && &#40;$len == 9&#41;&#41;&#123;
                                
extract&#40;unpack&#40;"Nres_x/Nres_y/Cunit",$data&#41;&#41;;
                                
if &#40;&#40;0 && $unit&#41; == 1&#41; &#123;
                                        # convert to dpi
                                        
$unit "dpi";
                                        
$res_x $res_x 0.0254;
                                        
$res_y $res_y 0.0254;
                                &
#125;
                                
if&#40;$res_x == $res_y&#41;&#123;
                                        
$res $res_x;
                                &
#125;else&#123;
                                        
$res "$res_x/$res_y";
                                &
#125;
                                
if &#40;$unit&#41; &#123;
                                        
if &#40;$unit == 1&#41; &#123;
                                                
$res .= " dpm";
                                        &
#125;else&#123;
                                                
$res .= " png-unit-$unit";
                                        &
#125;
                                
&#125;
                                
$ImageInformation["resolution"&#93; = $res;
                        #&#125;elseif&#40;$type == "tEXt"&#41;&#123;
                                        #my&#40;$key, $val&#41; = split&#40;/\0/, $data, 2&#41;;
                                    ## XXX should make sure $key is not in conflict with any
                                    ## other key we might generate
                                    #$info->push_info&#40;0, $key, $val&#41;;
        
                        
&#125;elseif&#40;&#40;$type == "tIME"&#41; && &#40;$len == 7&#41;&#41;&#123;
                                
$mt unpack&#40;"nyear/Cmonth/Cday/Chour/Cminute/Csecond", $data&#41;;
                                
$ImageInformation["LastModificationTime"&#93; = sprintf&#40;"%04d-%02d-%02d %02d&#58;%02d&#58;%02d",$mt["year"&#93;,$mt["month"&#93;,$mt["day"&#93;,$mt["hour"&#93;,$mt["minute"&#93;,$mt["second"&#93;&#41;;
                        //&#125;elseif&#40;$type == "sBIT"&#41;&#123;
                                //$sb = unpack&#40;"C*", $data&#41;;
                                //$ImageInformation["SignificantBits"&#93; = "";
                        
&#125;elseif&#40;$type == "IDAT"&#41;&#123;
                                //ignore image data
                        
&#125;else&#123;
                                
$ImageInformation["Chunk-$type"&#93; = $data; //unknown chunk type
                        
&#125;
                
&#125;
                
if&#40;!isset&#40;$ImageInformation["resolution"&#93;&#41;&#41;&#123;
                        
$ImageInformation["resolution"&#93; = "1/1";
                
&#125;
                //$ImageInformation["PNG_Chunks"&#93; = $chunks;
                
                
return $ImageInformation;
                
        &
#125;

        
function process_app0_jfxx&#40;$data&#41;&#123;
                
$code ord&#40;substr&#40;$data, 0, 1&#41;&#41;;
                
if&#40;$code == 16&#41;&#123;
                        
$type "JPEG thumbnail";
                &
#125;elseif&#40;$code == 17&#41;&#123;
                        
$type "Bitmap thumbnail";
                &
#125;elseif&#40;$code == 19&#41;&#123;
                        
$type "RGB thumbnail";
                &
#125;else&#123;
                        
$type "Unknown extention code $code";
                &
#125;
                
$ImageInformation["JFXX_ImageType"&#93; = $type;
        
        //    if &#40;$code == 0x10&#41; &#123;
        //        eval &#123;
        //            require IO&#58;&#58;String;
        //            my $thumb_fh = IO&#58;&#58;String->new&#40;$data&#41;;
        //            _process_file&#40;$info, $thumb_fh, 1&#41;;
        //        &#125;;
        //        $info->push_info&#40;1, "error" => $@&#41; if $@;
        //    &#125;
        
&#125;
        
        
        
        
function process_app14_adobe&#40;$data&#41;&#123;
            
Global $ImageInformation;    
                
extract&#40;unpack&#40;"nversion/nflaga/nflagb/Ctransform",$data&#41;&#41;;
            
$ImageInformation["AdobeTransformVersion"&#93; =  $version;
            
$ImageInformation["AdobeTransformFlags"&#93; = array&#40;$flags0,$flags1&#41;;
            
$ImageInformation["AdobeTransform"&#93; = $transform;
        
&#125;
        
        
        
        
function process_app0_jfif&#40;$data&#41;&#123;
            
Global $ImageInformation;
            if&
#40;strlen&#40;$data&#41; < 9&#41;&#123;
                        
$ImageInformation["Debug"&#93; = "Short JFIF chunk";
                        
return;
            &
#125;
                
extract&#40;unpack&#40;"Cver_hi/Cver_lo/Cunit/nx_density/ny_density/Cx_thumb/Cy_thumb", substr&#40;$data, 0, 9&#41;&#41;&#41;;
            
$ImageInformation["JFIF_Version"&#93; = sprintf&#40;"%d.%02d", $ver_hi, $ver_lo&#41;;
                
if&#40;&#40;$x_density != $y_density&#41; | !$unit&#41;&#123;
                        
$res $x_density."/".$y_density;
                &
#125;else&#123;
                        
$res $x_density;
                &
#125;
            
if&#40;$unit&#41;&#123;
                        
$units = array&#40;"pixels","dpi","dpcm"&#41;;
                        
if&#40;isset&#40;$units[$unit&#93;&#41;&#41;&#123;
                                
$res .= " ".$units[$unit&#93;;
                        
&#125;else&#123;
                                
$res .= " jfif-unit-".$unit;
                        &
#125;
            
&#125;
            
$ImageInformation["resolution"&#93; = $res;
            
if&#40;$x_thumb | $y_thumb&#41;&#123;
                    
$ImageInformation["width"&#93; = $x_thumb;
                    
$ImageInformation["height"&#93; = $y_thumb;
                    
$ImageInformation["ByteCount"&#93; = strlen&#40;$data&#41;;
                 
&#125;
        
&#125;
        
        
        
        
        
function process_app&#40;$mark, $data&#41;&#123;
            
Global $ImageInformation;
            
$app $mark 224;
            
$id substr&#40;$data, 0, 5&#41;;
            
            
            #$ImageInformation["Debug"&#93; = $data;
            
            
$id "$app-$id";
            if&
#40;$id == "0-JFIF\0"&#41;&#123;
                        
$this->process_app0_jfif&#40;substr&#40;$data, 5&#41;&#41;; 
            
&#125;elseif&#40;$id == "0-JFXX\0"&#41;&#123;                
                        
$this->process_app0_jfxx&#40;substr&#40;$data, 5&#41;&#41;;  //not tested
            
&#125;elseif&#40;$id == "1-Exif\0"&#41;&#123;        
                        
$this->process_app1_exif&#40;substr&#40;$data, 5&#41;&#41;;  //not converted yet
            
&#125;elseif&#40;$id == "14-Adobe"&#41;&#123;
                        
$this->process_app14_adobe&#40;substr&#40;$data, 5&#41;&#41;;
            
&#125;else&#123;
                        #$info->push_info&#40;0, "App$id", $data&#41;;
                        #$ImageInformation["App$id"&#93; = $data;
                        #printf "  %s\n", Data&#58;&#58;Dump&#58;&#58;dump&#40;$data&#41;;
            
&#125;
        
&#125;
        
        
        
function process_chunk&#40;$mark, $data&#41;&#123;
            
global $ImageInformation;
        
                
$JpegTypes = array&#40;'192' => "Baseline",
                                                      
'193' => "Extended sequential",
                                                      
'194' => "Progressive",
                                                      
'195' => "Lossless",
                                                      
'197' => "Differential sequential",
                                                      
'198' => "Differential progressive",
                                                      
'199' => "Differential lossless",
                                                      
'201' => "Extended sequential, arithmetic coding",
                                                      
'202' => "Progressive, arithmetic coding",
                                                      
'203' => "Lossless, arithmetic coding",
                                                      
'205' => "Differential sequential, arithmetic coding",
                                                      
'206' => "Differential progressive, arithmetic coding",
                                                      
'207' => "Differential lossless, arithmetic coding"&#41;;
        
            
if &#40;$mark == 254&#41; &#123;
                    
$ImageInformation["Comment"&#93; = $data;
            
&#125;elseif&#40;&#40;$mark >= 224&#41; & &#40;$mark <= 239&#41;&#41;&#123;
                    
$this->process_app&#40;$mark, $data&#41;;
            
&#125;elseif&#40;$JpegTypes[$mark&#93;&#41;&#123;
                    
                    
extract&#40;unpack&#40;"Cprecision/nheight/nwidth/Cnum_comp", substr&#40;$data, 0, 6&#41;&#41;&#41;;
                        
$ImageInformation["JPEG_Type"&#93; = $JpegTypes[$mark&#93;;
                        
$ImageInformation["width"&#93; = $width;
                        
$ImageInformation["height"&#93; = $height;
                        
$ImageInformation["SamplesPerPixel"&#93; = $num_comp;
                        #for &#40;1..$num_comp&#41; &#123;
                            #$info->push_info&#40;$img_no, "BitsPerSample", $precision&#41;;
                            //$ImageInformation["BitsPerSample"&#93; = $precision;
                        #&#125;
                        
                        ## XXX need to consider JFIF/Adobe markers to determine this...
                        
if &#40;$num_comp == 1&#41; &#123;
                                
$ImageInformation["color_type"&#93; = "Gray";
                        
&#125;elseif&#40;$num_comp == 3&#41;&#123;
                                        
$ImageInformation["color_type"&#93; = "YCbCr"; # or RGB ?
                        
&#125;elseif&#40;$num_comp == 4&#41;&#123;
                                
$ImageInformation["color_type"&#93; = "CMYK"; # or YCCK ?
                        
&#125;
        
                        #if &#40;1&#41; &#123;
                        #    while &#40;length&#40;$data&#41;&#41; &#123;
                        #                my&#40;$comp_id, $hv, $qtable&#41; = unpack&#40;"CCC", substr&#40;$data, 0, 3, ""&#41;&#41;;
                        #                $comp_id = &#123;1 => "Y",2 => "Cb",3 => "Cr",82 => "R",71 => "G",66 => "B",&#125;->&#123;$comp_id&#125; || $comp_id;
                        #                $info->push_info&#40;0, "ColorComponents", [$comp_id, $hv, $qtable&#93;&#41;;
                    #        &#125;
                        #&#125;
            
&#125;
        
&#125;
        
        
function ProcessFileJPEG&#40;$source&#41;&#123;
                
Global $ImageInformation;
                
$ImageInformation = array&#40;&#41;;
                
rewind&#40;$source&#41;;
                
$soi fread&#40;$source, 2&#41;;
                
if&#40;!$soi == "\xFF\xD8"&#41;&#123;
                        
die &#40;"SOI missing"&#41;;
                
&#125;
                
$ImageInformation["file_media_type"&#93; = "image/jpeg";
            
$ImageInformation["file_ext"&#93; = "jpg";
            
while &#40;1&#41; &#123;
                
extract&#40;unpack&#40;"Cff/Cmark/nlen", fread&#40;$source, 4&#41;&#41;&#41;;
                
if &#40;$ff != 255&#41;&#123;
                        
break;
                &
#125;
                
if&#40;&#40;$mark == 218&#41; | &#40;$mark == 217&#41;&#41;&#123;
                        
break;
                &
#125;
                
if&#40;$len < 2&#41;&#123;
                        
break;
                &
#125;
                
$this->process_chunk&#40;$mark, fread&#40;$source, &#40;$len - 2&#41;&#41;&#41;;
            
&#125;
            
return $ImageInformation;
        &
#125;

&#125;
?>


Then goto: '/include/functions.inc.php' and find this line:
return $url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);


Replace with this:
       return 'logo.php?picturename='.$url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);


That's it...
DJ Axion

STRiDOR

Wow! It's to early for me to make any real comments (still have to install and try it out), but it sounds great.  Thanks for sharing.
STRiDOR

tuxetuxe

i ge this error!!!
QuoteFatal error: Call to undefined function: process_app1_exif() in /var/www/fotos/imageinfo.php on line 435

How do i solve it? I'm using apache and php4 from debian Sid ... :|

mal

Hi
Can't get it to work! if i upload an image it appears to load ok but only a broken image is displayed. The image files have uploaded ok but the logo.png file has not been applied.
Any ideas?

Thanks

Mal

Tarique Sani

Have you considered cacheing the images which are watermarked?

Storage space is cheaper than computing power ;)

and how about using ImageMagick convert when it is available...
SANIsoft PHP applications for E Biz

Dereckson

php must be compiled with --enable-exif option.

Bertez[z]

Yes. it works fine!
Thanks for this great script!!!!!
Greetz Bert

ioneng

I'm getting these errors:

Warning: fread(): supplied argument is not a valid stream resource in D:\htdocs\gallery\imageinfo.php on line 82

Warning: fclose(): supplied argument is not a valid stream resource in D:\htdocs\gallery\imageinfo.php on line 78

Warning: Cannot modify header information - headers already sent by (output started at D:\htdocs\gallery\logo.php:1) in D:\htdocs\gallery\logo.php on line 97

Warning: fread(): supplied argument is not a valid stream resource in D:\htdocs\gallery\logo.php on line 99

Warning: fclose(): supplied argument is not a valid stream resource in D:\htdocs\gallery\logo.php on line 101

anyone knows what's wrong?

curtg

I installed the files and the watermarking is working, however whenever I display an image that has an upper case character in the file name I get the dreaded X broken link image. Any reason why this script doesn't support upper case characters in file names?
Peace,
CurtG
c u r t g r a n g e r . c o m

sheeew

Same as curtg, watermark works, but when maximize, i get a cross, and no image/with logo.png

tumnus

Sure you're sick of hearing this, but:
Watermarking is working (and quite nicely too) on all images until I try and view at full size. Then I get the ol' dreaded red cross.

I'm not having trouble with uppercase chars. some with u/c filenames work, some don't, some with numbers in filename work, some don't, and some simple filenames like 'aromawave.jpg' that refuse. but one thing that does seem definite is that digital camera names such as DCS003.jpg never maximise.

Maybe it's a filename length issue? or a filesize issue?

I don't care for now anyway, cos I'm going to hack out the ability to fully maximise. (btw, could we have an 'disable popup maximising' toggle in the config next version? not just for watermark bug workarounds, i just hate js popups of any nature)

Holding breath & crossing toes for future version with full watermarking...

T
Be kind, man. Don't be mankind.

BoBafeTT

I too tried to alter the  script as it is not the same I tried the best i could
But the problem is this

the code you said to alter
return $url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);

the code that is actually there
$url_prefix = array(0 => $CONFIG['fullpath'],
            );
    }
    return path2url($pic_row['filepath'] . $pic_prefix[$mode] . $pic_row['filename']);

not sure whats up with that

aemege

I have change a little the code of Dj Axion, now you don´t need the class, but still need gd 2
Cambie el codigo de Dj Axion un poco, no se necesita la clase, pero si gd 2

logo.php

<?
// SCHIPT BY DJ AXION
// e-mail: DJ@the-expansion.com
// Enjoy this script!
//Change by Aemege almonare[at]ing[dot]uchile[dot]cl
// GET SOURCE PICTURE FROM FORM INPUT

// These Version Jiggleing is pain in the ass
$datos=exif_read_data($picturename);
$logo= "/paht/to/logo.png"

if (function_exists("imagegif")) {
$gi["GIF Read Support"] = 1;
}elseif (function_exists("imagejpeg")) {
$gi["JPG Support"] = 1;
}elseif (function_exists("imagepng")) {
$gi["PNG Support"] = 1;
}

// Try to dicide
if ($type = $datos[MimeType]) {
 switch ($type) {
    case 'image/jpeg' :
       if ($gi["JPG Support"]){
          $b = imagecreatefromjpeg($picturename);
       } else {
          $image = $picturename;
       }
    break;
    case 'iamge/gif' :
       if ($gi["GIF Read Support"]) {
          $b = imagecreatefromgif($picturename);
       } else {
          $image = $picturename;
       }
    break;
    case 'imag/png' :
       if ($gi["PNG Support"]){
          $b = imagecreatefrompng($picturename);
       } else {
          $image = $picturename;
       }
    break;
 }
}
$bx = $datos[COMPUTED][Width];// source width
$by = $datos[COMPUTED][Height]; // source height

if(!empty($b)) {
 $lm = $b;

 if ($bx > 200)  {  // this ensures no watermark is added to small images (e.g. thumbnails)
 // ADD WATERMARK
    $pos = "bottomleft"; //where is the watermark displayed...


 // THIS IS THE CODE FOR THE WATERMARK
    ImageAlphaBlending($lm, true) or die ("Could not alpha blend"); // Enable when on GD 2+
    $logoImage = ImageCreateFromPNG($logo); // logo.png is a watermark it add's...
    $logoW = ImageSX($logoImage);
    $logoH = ImageSY($logoImage);
    if ($pos == "topleft") {
       $src_x = 0;
       $src_y = 0;
    } else if ($pos == "topright") {
       $src_x = $bx - $logoH;
       $src_y = 0;
    } else if ($pos == "bottomleft") {
       $src_x = 0;
       $src_y = $by - $logoH;
    } else if ($pos == "bottomright") {
       $src_x = $bx - $logoW;
       $src_y = $by - $logoH;
    }
    ImageCopy($lm,$logoImage,$src_x,$src_y,0,0,$logoW,$logoH);
 }
}
if ($datos[MimeType] && !empty($b)) {
 Header("Content-type: $datos[MimeType]");
 switch ($type) {
    case 'image/jpeg' :
       Imagejpeg($lm,'',60); //80 means JPEG quality
    break;
    case 'image/gif' :
       ImageGif($lm);
    break;
    case 'image/png' :
       ImagePnG($lm);
    break;
 }
imageDestroy($lm);

} else {
 // No usable GD image Function ? Ok preventing brocken Images and give back the untouched Image
 Header("Content-type: image/$type");
 $fp = fopen($image,"r");
 $fa = fread($fp,filesize ($image));
 print($fa);
 fclose($fp);
}    
?>


Then goto: '/include/functions.inc.php' and find this line:

Code:
return $url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);


Replace with this:

Code:
      return 'logo.php?picturename='.$url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);


Saludos desde Chile

Tarique Sani

Thanks for your work - which version of  Coppermine did you test this with?
SANIsoft PHP applications for E Biz

aemege

He cambiado una poco el script, se necesita GD && PHP >=4.3.0, lo he probado en Coppermine Photo Gallery v1.3.1 con Apache, PHP 4.3.2 y GD bundled (2.0.12 compatible)
Ahora la marca de agua puede ser un jpeg, gif o png, lo unico que hay que cambiar en el script es la tura relativa al archivo de la marca de agua.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I have change some the script, you need GD && PHP >=4.3.0, I have tested in Coppermine Photo Gallery v1.3.1 with Apache, PHP 4.3.2 and GD bundled (2.0.12 compatible)
Now you can use as watermark jpeg, gif or png files, you only need to alter the relative path to the watermark file.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Salu2 desde Chile

El script se debe llamar logo.php y tiene que estar en la carpeta principal del Coppermine, no en el include/
-------------------------------------------------------------------------------------------------------------------------------------------
The script must called logo.php and must be located in the first folder of  Coppermine, not in include/


<? 
// SCHIPT BY DJ AXION 
// e-mail: DJ@the-expansion.com 
// Enjoy this script! 
// [b]Altered by Aemege almonare[at]ing[dot]uchile[dot]cl[/b]

// Need GD && PHP >= 4.3.0


$logo="../relative/paht/to/watermark.ext";


$datos[foto][tipo]=exif_imagetype($picturename);
$datos[logo][tipo]=exif_imagetype($logo);
$datos[foto][MimeType]=mimetype($datos[foto][tipo]);
if (
function_exists("imagegif")) { 
 
$gi["GIF Read Support"] = 1
}elseif (
function_exists("imagejpeg")) { 
 
$gi["JPG Support"] = 1
}elseif (
function_exists("imagepng")) { 
 
$gi["PNG Support"] = 1


// Try to dicide 
if ($datos[foto][tipo]) { 
  switch (
$datos[foto][tipo]) { 
     case 
'2' 
        if (
$gi["JPG Support"]){ 
           
$b imagecreatefromjpeg($picturename); 
        } else { 
           
$image $picturename
        } 
     break; 
     case 
'1' 
        if (
$gi["GIF Read Support"]) { 
           
$b imagecreatefromgif($picturename); 
        } else { 
           
$image $picturename
        } 
     break; 
     case 
'3' 
        if (
$gi["PNG Support"]){ 
           
$b imagecreatefrompng($picturename); 
        } else { 
           
$image $picturename
        } 
     break; 
  } 

     
$bx ImageSX($b); 
     
$by ImageSY($b); 

if(!empty(
$b)) { 
  
$lm $b

  if (
$bx 200)  {  // this ensures no watermark is added to small images (e.g. thumbnails) 
  // ADD WATERMARK 
     
$pos "bottomleft"//where is the watermark displayed... 

// Try to dicide the Logo Vemos que tipo de archivo es el logo
if ($datos[logo][tipo]) { 
  switch (
$datos[logo][tipo]) { 
     case 
'1' 
  
$logoImage=imagecreatefromgif($logo); 
     break; 
     case 
'2' 
$logoImage=imagecreatefromjpeg($logo); 
     break; 
 case '3' 
    $logoImage=imagecreatefrompng($logo); 
     break; 
  } 

  
// THIS IS THE CODE FOR THE WATERMARK 
     
ImageAlphaBlending($lmtrue) or die ("Could not alpha blend"); // Enable when on GD 2+ 
     
$logoW ImageSX($logoImage); 
     
$logoH ImageSY($logoImage); 
     if (
$pos == "topleft") { 
        
$src_x 0
        
$src_y 0
     } else if (
$pos == "topright") { 
        
$src_x $bx $logoW
        
$src_y 0
     } else if (
$pos == "bottomleft") { 
        
$src_x 0
        
$src_y $by $logoH
     } else if (
$pos == "bottomright") { 
        
$src_x $bx $logoW
        
$src_y $by $logoH
     } 
     
ImageCopy($lm,$logoImage,$src_x,$src_y,0,0,$logoW,$logoH); 
  } 


if (
$datos[foto][MimeType] && !empty($b)) { 
  
Header("Content-type: $datos[foto][Mimetype]"); 
  switch (
$datos[foto][tipo]) { 
     case 
1
        
ImageGif($lm); 
     break; 
     case 
2
        
Imagejpeg($lm,'',60); //80 means JPEG quality 
     
break; 
     case 
3
        
ImagePnG($lm); 
     break; 
  } 
imageDestroy($lm); 
} else { 
  
// No usable GD image Function ? Ok preventing brocken Images and give back the untouched Image 
  
Header("Content-type: $datos[foto][MimeType]"); 
  
$fp fopen($image,"r"); 
  
$fa fread($fp,filesize ($image)); 
  print(
$fa); 
  
fclose($fp); 

function 
mimetype($tipo){
switch($tipo){
case 1:
return "image/gif";
case 2:
return "image/jpeg";
case 3;
return "image/png";
default:
return "";
}
}
   
?>
?>



Then goto: '/include/functions.inc.php' and find this line:

return $url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);

Replace with this:
return 'logo.php?picturename='.$url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);


Joachim Müller

this should be moved to another board then, users will never find you mod here...

GauGau

zpeek

Pls I need this to work with CPG 1.3.2

Casper

There are watermark mods for 1.3.2.

Why are you posting this on the old 1.1 version support boards?  Just search the mods/add ons/hacks board, and they are near the top.
It has been a long time now since I did my little bit here, and have done no coding or any other such stuff since. I'm back to being a noob here

zpeek

Because this one is not really a prement watermark.

Casper

it seems the changes by aemege above allow this to work with 1.3.1, which means it should be ok with 1.3.2 as well.

Try it and let us know.
It has been a long time now since I did my little bit here, and have done no coding or any other such stuff since. I'm back to being a noob here