<?php

/////////////////////////////////////////////////////////
//
// image manipulation
//
/////////////////////////////////////////////////////////



function rotate($album_directory,$album,$pic,$degree) {
  $image=$album_directory.'/'.$album.'/'.$pic;

  /*
  $scaled=getScaledURL($image);
  if (file_exists($scaled)){
    $image=$scaled;
  }
  */

  $backup=$image.'.bak';

  if (@rename($image,$backup)){
    if (rotatePic($backup,$image,$degree)){
      if (!file_exists($image)){
	@rename($backup,$image);
      }
      else{
	return true;
      }
    }
  }
}

function filter($album_directory,$album,$pic,$filter) {
  //$image='data/scaled/'.$album_directory.'/'.$album.'/'.$pic.'.jpg';
  $image=$album_directory.'/'.$album.'/'.$pic;
  $backup=$image.'.bak';

  if (($filter == 'undo') && file_exists($backup)){
    //    rename($image,$image.'.redo');
    unlink($image);
    rename($backup,$image);
  }
  elseif (rename($image,$backup)){
    if ($filter == 'grey'){
      create_grey($backup,$image);
    }
    /*
    elseif ($filter == 'contrast'){
      $im=contrast($backup);
      imagejpeg($im, $image);
    }
    */
  }
}

////////////////////////////////////////////////////////////////////////////
// taken from http://fundisom.com/phparadise/php/image_editing/image_rotate
////////////////////////////////////////////////////////////////////////////
function rotatePicGD($sourcefile, $targetfile, $degrees)
{
  $img_size = getImageSize($sourcefile);
  $x = $img_size[0];
  $y = $img_size[1];
  if ($x > $y)
    {
      $dst_x = 0;
      $dst_y = $x - $y;
      $newd = $x;
    }
  else
    {
      $dst_x = $y - $x;
      $dst_y = 0;
      $newd = $y;
    }
  $src_img = ImageCreateFromJPEG($sourcefile);
  $dst_img = ImageCreateTrueColor($newd,$newd);
  if ((($x > y) && ($degrees < 180)) || (($y > $x) && ($degrees > 180)))
    ImageCopyResampled($dst_img,$src_img,0,0,0,0,$x,$y,$x,$y);
  else
    ImageCopyResampled($dst_img,$src_img,$dst_x,$dst_y,0,0,$x,$y,$x,$y);
  $rotated_img = ImageRotate($dst_img, $degrees,0);
  if ($degrees != 180)
    {
      //90 CounterClockWise or Clockwise
      $final_img = ImageCreateTrueColor($y,$x);
      ImageCopyResampled($final_img,$rotated_img,0,0,0,0,$y,$x,$y,$x);
    }
  else
    {
      //180 Degrees
      $final_img = ImageCreateTrueColor($x,$y);
      ImageCopyResampled($final_img,$rotated_img,0,0,0,0,$x,$y,$x,$y);
    }
  ImageJPEG($final_img, $targetfile);     
}



////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// alternative rotating functions ....

   define("IMAGE_FLIP_HORIZONTAL",    1);
   define("IMAGE_FLIP_VERTICAL",    2);
   define("IMAGE_FLIP_BOTH",    3);

function ImageFlip($imgsrc, $type)
{
   $width = imagesx($imgsrc);
   $height = imagesy($imgsrc);

   $imgdest = @imagecreatetruecolor($width, $height);
   if (!$imgdest){return false;}

   switch( $type )
       {
       // mirror wzgl. osi
       case IMAGE_FLIP_HORIZONTAL:
           for( $y=0 ; $y<$height ; $y++ )
               imagecopy($imgdest, $imgsrc, 0, $height-$y-1, 0, $y, $width, 1);
           break;

       case IMAGE_FLIP_VERTICAL:
           for( $x=0 ; $x<$width ; $x++ )
               imagecopy($imgdest, $imgsrc, $width-$x-1, 0, $x, 0, 1, $height);
           break;

       case IMAGE_FLIP_BOTH:
           for( $x=0 ; $x<$width ; $x++ )
               imagecopy($imgdest, $imgsrc, $width-$x-1, 0, $x, 0, 1, $height);

           $rowBuffer = @imagecreatetruecolor($width, 1);
	   if (!$rowBuffer){return false;}
           for( $y=0 ; $y<($height/2) ; $y++ )
               {
               imagecopy($rowBuffer, $imgdest  , 0, 0, 0, $height-$y-1, $width, 1);
               imagecopy($imgdest  , $imgdest  , 0, $height-$y-1, 0, $y, $width, 1);
               imagecopy($imgdest  , $rowBuffer, 0, $y, 0, 0, $width, 1);
               }

           imagedestroy( $rowBuffer );
           break;
       }

   return( $imgdest );
}

//////////////////////////////////////////////////////////////////////
// taken from http://nl3.php.net/manual/en/function.imagerotate.php

function RotatePic($sourcefile, $targetfile, $angle)
{

  $imgSrc = @ImageCreateFromJPEG($sourcefile);
  if (!$imgSrc){return false;}

   // ensuring we got really RightAngle (if not we choose the closest one)
   $angle = min( ( (int)(($angle+45) / 90) * 90), 270 );

   // no need to fight
   if( $angle == 0 )
       return( $imgSrc );

   // dimension of source image
   $srcX = imagesx( $imgSrc );
   $srcY = imagesy( $imgSrc );

   switch( $angle )
       {
       case 270:
           $imgDest = @imagecreatetruecolor( $srcY, $srcX );
	   if (!$imgDest){return false;}
           for( $x=0; $x<$srcX; $x++ )
               for( $y=0; $y<$srcY; $y++ )
                   imagecopy($imgDest, $imgSrc, $srcY-$y-1, $x, $x, $y, 1, 1);
           break;

       case 180:
           $imgDest = @ImageFlip( $imgSrc, IMAGE_FLIP_BOTH );
	   if (!$imgDest){return false;}
           break;

       case 90:
           $imgDest = @imagecreatetruecolor( $srcY, $srcX );
	   if (!$imgDest){return false;}
           for( $x=0; $x<$srcX; $x++ )
               for( $y=0; $y<$srcY; $y++ )
		   imagecopy($imgDest, $imgSrc, $y, $srcX-$x-1, $x, $y, 1, 1);

           break;
       }

   return @ImageJPEG($imgDest, $targetfile);     
}



////////////////////////////////////////////////////////////////////////////
// from http://fundisom.com/phparadise/php/image_editing/grayscale_an_image
////////////////////////////////////////////////////////////////////////////

function create_grey($input,$output)
{
  $bild = imagecreatefromjpeg($input);
  $x = imagesx($bild);
  $y = imagesy($bild);
  for($i=0; $i<$y; $i++)
    {
      for($j=0; $j<$x; $j++)
	{
	  $pos = imagecolorat($bild, $j, $i);
	  $f = imagecolorsforindex($bild, $pos);
	  $gst = $f["red"]*0.15 + $f["green"]*0.5 + $f["blue"]*0.35;
	  $col = imagecolorresolve($bild, $gst, $gst, $gst);
	  imagesetpixel($bild, $j, $i, $col);
	}
    }
  imagejpeg($bild,$output,90);
}

////////////////////////////////////////////////////////////////////////////
// taken from http://fundisom.com/phparadise/php/image_editing/colorizing
////////////////////////////////////////////////////////////////////////////

function imagecolorize (& $im ,& $col ,$pct )
{
  // Get the image's width
  $im_w = imagesx ($im );
  // Get the image's height
  $im_h = imagesy ($im );
  // Set a pixel with the color, so we can get it easily
  $setpixel = imagesetpixel ($im ,$im_w ,0,$col );
  // Get the color
  $index = imagecolorat ($im ,$im_w ,0);
  // Find the color in the index
  $rgb = imagecolorsforindex ($im ,$index );
  // Get the red value
  $r = $rgb ["red" ];
  // Get the green value
  $g = $rgb ["green" ];
  // Get the blue value
  $b = $rgb ["blue" ];
  // Create the layover
  $layover = imagecreate ($im_w ,$im_h );
  // Allocate the color on this image
  $color = imagecolorallocate ($layover ,$r ,$g ,$b );
  // Fill the image with the new color (this really isn't needed)
  $fill = imagefill ($layover ,0,0,$color );
  // Merge the layover on top of the older image
  $merge = imagecopymerge ($im ,$layover ,0,0,0,0,$im_w ,$im_h ,$pct );
  imagedestroy ($layover ); // Destroy the layover
}  	 

////////////////////////////////////////////////////////////////////////////
// from http://nl2.php.net/imagesetpixel
// (slightly modified)
////////////////////////////////////////////////////////////////////////////

function contrast($imgfile) {
   $brightness=0;
   $maxb=0;
   $minb=255;
   $imagesize=getimagesize($imgfile);
   $im=imagecreatefromjpeg($imgfile);
   $w=$imagesize[0];
   $h=$imagesize[1];
   for ($x=0; $x<$w; $x++) {
       for ($y=0; $y<$h; $y++) {
           $rgb=imagecolorat($im, $x, $y);
           $rgb=imagecolorsforindex($im, $rgb);
           $grey=0.2125*$rgb['red']+
               0.7154*$rgb['green']+
               0.0721*$rgb['blue'];
           $brightness+=$grey;
           if ($grey>$maxb) $maxb=$grey;
           if ($grey<$minb) $minb=$grey;
       }
   }
   $brightness=$brightness/($w*$h);
   $minb=$brightness/($brightness-$minb);
   $maxb=(255-$brightness)/($maxb-$brightness);
   $contrast=min($minb, $maxb);
   for ($x=0; $x<$w; $x++) {
       for ($y=0; $y<$h; $y++) {
           $rgb=imagecolorat($im, $x, $y);
           $rgb=imagecolorsforindex($im, $rgb);
           imagesetpixel($im, $x, $y,
               65536*floor(min($rgb['red']*$contrast, 255))+
               256*floor(min($rgb['green']*$contrast, 255))+
               floor(min($rgb['blue']*$contrast, 255)));
       }
   }
   return ($im);
}


?>