<?php

	include "library.php";

	$cxn = start_code();


			/* get variables */

	$pathroot = val( $_SESSION, "pathroot" );

	$listing_id = val( $_SESSION, "listing_id" );

	$photo_number = val( $_GET, "photonumber" );

	$rotate = val( $_GET, "rotate" );

//	$this_id = val( $_GET, "thisid" );
//
//
//    if ($this_id != "")
//        $id = $this_id;
        
    

			/* start page */


			/* check parameters */

	crash_if_missing( $listing_id, "listing_id missing in rotate_photo_go", $cxn );

	crash_if_missing( $pathroot, "pathroot missing in rotate_photo_go", $cxn );

	crash_if_missing( $photo_number, "photo_number missing in rotate_photo_go", $cxn );


    if ($rotate == "no")
    {
        echo "<br>Rotate cancelled.<br><br><br>";
        
    	echo show_home_link();
    	
    	exit();
    }
    else
    {
    	page_header( "", $cxn );


    		/* get the file extension */
    
    	$query = "SELECT * FROM photos WHERE listing_id = ".$listing_id." AND photo_number = ".$photo_number;
    
    	$result = run_query( $query, $cxn );
    
    	if (query_num_rows($result) == 0)
    		crash( "Internal error photo not found", $cxn );
    	else
    	{
    		$data = get_row( $result );
    
            $file_extension = $data['file_extension'];
            
            $version = $data['version'];
            
            if ($version == "")
                $version_str = "";
            else
                $version_str = "_v".$version;
                
    		$destfile = $pathroot."/photos/photo_".$listing_id."_".$photo_number.$version_str.".".$file_extension;
    
    
                // get the image dimensions
                
    		list( $width, $height, $type, $attr ) = getimagesize( $destfile );
    
    
                // swap the width and height as the image will be rotated
                
    		$x = $height;
    		$height = $width;
    		$width = $x;
    		
    		if ($height == 0)
    			$aspect_ratio = 1;
    		else
    			$aspect_ratio = $width / $height;
    
    
        	$query = "UPDATE photos SET aspect_ratio = ".$aspect_ratio." WHERE listing_id = ".$listing_id." AND photo_number = ".$photo_number;		
    
    	    $result = run_query( $query, $cxn );
    
    
    
                // rotate the image
                
    		$im = imagecreatefromjpeg( $destfile );
    
            $rotated = imagerotate( $im, 270, 0 );
    
    
                // save the image as a new version
    
            if ($version == "")            
                $version = "2";
            else
                $version = $version + 1;
    
            $version_str = "_v".$version;
                
    		$destfile = $pathroot."/photos/photo_".$listing_id."_".$photo_number.$version_str.".".$file_extension;
    		
    		imagejpeg( $rotated, $destfile, 100 );		/* the 100 is the percentage of original image quality (compression) */
    
        	$query = "UPDATE photos SET version = '".$version."' WHERE listing_id = ".$listing_id." AND photo_number = ".$photo_number;		
    
    	    $result = run_query( $query, $cxn );
    	
    	
    			/* create the smaller resolution file */
    
    		$destfile_sm = $pathroot."/photos/photo_".$listing_id."_".$photo_number."_sm".$version_str.".".$file_extension;
    
    		$new_width = 120 * $aspect_ratio;
    		$new_height = 120;
    
    		$image_p = imagecreatetruecolor( $new_width, $new_height );
    
    		imagecopyresampled( $image_p, $rotated, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
    
    		imagejpeg( $image_p, $destfile_sm, 90 );		/* the 90 is the percentage of original image quality (compression) */
    
    
    				// release memory
    				
    		imagedestroy( $image_p );
    
    
    			/* create the larger resolution file */
    
    		$destfile_l = $pathroot."/photos/photo_".$listing_id."_".$photo_number."_l".$version_str.".".$file_extension;
    
    		$new_width2 = 380 * $aspect_ratio;
    		$new_height2 = 380;
    
    		$image_p2 = imagecreatetruecolor( $new_width2, $new_height2 );
    
    		imagecopyresampled( $image_p2, $rotated, 0, 0, 0, 0, $new_width2, $new_height2, $width, $height );
    
    		imagejpeg( $image_p2, $destfile_l, 100 );		/* the 100 is the percentage of original image quality (compression) */
    
    
    				// release memory
    		imagedestroy( $image_p2 );
    		imagedestroy( $rotated );			
    
      
    		echo black( "Photo rotated successfully.<br><br>Please rotate it again if it is not yet the correct orientation.<br><br><br>" );
    		
            show_photo( $listing_id, 100, $photo_number, true, false, $cxn );		
            
            echo "<br><br><br>";
    	}
    
    
    	echo show_home_link();
    }
    
    
		// don't go back to the photos screen because a browser back button will upload the photo twice	
//	echo show_link( "Return", "manage_photos.php" );

?>