Using the same basic configuration as I described in my last issue (which was very promptly addressed--Thanks!), I tried to rotate an image. I got a double error like this:

* warning: rename(C:\path\to\tmp\acidfree\ima2C5.tmp.jpg,private/working/0/6_large.jpg) [function.rename]: File exists in D:\path\to\drupaltest\modules\contrib\acidfree\image_manip.inc on line 106.
* warning: rename(C:\path\to\tmp\acidfree\ima2C6.tmp.jpg,private/working/0/6_small.jpg) [function.rename]: File exists in D:\path\to\drupaltest\modules\contrib\acidfree\image_manip.inc on line 106.

I think the permissions on the tmp folder are fine. After trying to rotate several pictures, that tmp folder is full of rotated pictures--one small and one large of each. One odd thing: the pics that are still in that tmp folder appear to be rotated the opposite direction of the way I indicated with the dropdown menu. What am I doing wrong to provoke these errors?

Comments

vhmauery’s picture

why don't you take a look at the latest 4.7 Acidfree and see if it is working any better for you. Also check to make sure that your filemanager 'private' dir is a subdir of the drupal 'file system path' directory.

g.philippot’s picture

I was encountring the same trouble on my server (Windows 2000, Apache 2, PHP 5)

it appeare when you are not using exiftran or jpegtran.

After half a day debugging acidfree and looking in forum, I found that the rename function doesn't work under windows if destination file existe. (cf http://fr2.php.net/manual/en/function.rename.php )

so I updated the source code, as follow, in image_manip.inc to manage this case.

function acidfree_rotate_image($image, $angle) {
    $ext = _acidfree_ext_from_file($image);
    if (($angle == 90 || $angle ==180 || $angle == 270) && $ext == 'jpg') {
        // try to do lossless rotation first
        $exiftran_path = variable_get('acidfree_path_to_exiftran', '/usr/bin/exiftran');
        if (is_executable($exiftran_path)) {
            $command = "{$exiftran_path} -i -{$angle{0}} $image";
            system($command, $ret);
            return true;
        } else if ($angle == 'auto') {
            drupal_set_message('you must have exiftran installed to use automatic rotation', 'error');
            return false;
        }
        $jpegtran_path = variable_get('acidfree_path_to_jpegtran', '/usr/bin/jpegtran');
        if (is_executable($jpegtran_path)) {
            $outfile = acidfree_mktmpfile('imagemanip', _acidfree_ext_from_file($image));
            $command = "{$jpegtran_path} -rotate $angle -outfile $outfile $image";
            system($command, $ret);
            if (!$ret) {
                rename($outfile, $image);
                return true;
            }
            unlink($outfile);
            return false;
        }
    } 
    $outfile = acidfree_mktmpfile('imagemanip', _acidfree_ext_from_file($image));
    if (image_rotate($image, $outfile, $angle)) {
		// updated for windows environment that not support rename (at least in PHP 5.1.4) if dest file exist
		if (!rename($outfile, $image))
		{
			//backuping dest file before any update
			if (copy($image,$image.'.bak'))
			{
				// deleting file
	 			unlink($image);
				// trying to move again
				if (!rename($outfile, $image))
				{
					drupal_set_message(t('Failled to move temp file to image file - rotation failed'));
					// trying to restore to previous stat
					if (copy($image.'.back',$image))
					{
						unlink($image.'.bak');
					}
					else
					{
						drupal_set_message(t('Failled to restore backup file' . $image.'.back You must rename it manually as ' . $image));
					}
				}
				else
				{
					unlink($image.'.bak');
				}
				
			}
			else
			{
				drupal_set_message(t('unable to backup old image file - rotation failed'));
			}
		}

    } else {
        drupal_set_message(t('image rotate failed for %image', array('%image' => $image)));
        unlink($outfile);
        return false;
    }
    return true;
}

Rotation are now working without error but rotation are reversed.
counterclockwise act as clockwise and clockwise as counterclockwise
I will continue to investigate this and post a new topic when I found Why.

Gaël

g.philippot’s picture

Sorry, after checking my code I found that my update is working but I was allways geting the error in drupal log.
Rename error are catched by the drupal error management function before my error management.
so I updated the code as follow.

/**
 * rotate an image the best we can
 *
 * @param $image
 *   path to the image to be rotated
 * @param $angle
 *   angle to rotate the image.  If this is 90, 180, or 270,
 *   we will first attempt lossless rotation.  If that fails,
 *   we will use lossy rotation.  If this is any other angle,
 *   we will skip straight to lossy rotation using convert.
 *
 * @return
 *   nothing
 */

// g.philippot - new void function to catch unwanted error.
function WindowsRenameErrorHandler($errno, $errstr, $errfile, $errline)
{
	
}

function acidfree_rotate_image($image, $angle) {
    $ext = _acidfree_ext_from_file($image);
    if (($angle == 90 || $angle ==180 || $angle == 270) && $ext == 'jpg') {
        // try to do lossless rotation first
        $exiftran_path = variable_get('acidfree_path_to_exiftran', '/usr/bin/exiftran');
        if (is_executable($exiftran_path)) {
            $command = "{$exiftran_path} -i -{$angle{0}} $image";
            system($command, $ret);
            return true;
        } else if ($angle == 'auto') {
            drupal_set_message('you must have exiftran installed to use automatic rotation', 'error');
            return false;
        }
        $jpegtran_path = variable_get('acidfree_path_to_jpegtran', '/usr/bin/jpegtran');
        if (is_executable($jpegtran_path)) {
            $outfile = acidfree_mktmpfile('imagemanip', _acidfree_ext_from_file($image));
            $command = "{$jpegtran_path} -rotate $angle -outfile $outfile $image";
            system($command, $ret);
            if (!$ret) {
                rename($outfile, $image);
                return true;
            }
            unlink($outfile);
            return false;
        }
    } 
    $outfile = acidfree_mktmpfile('imagemanip', _acidfree_ext_from_file($image));

	// g.philippot - added '360 -' for correct clockwise/counterclockwise rotation (at least under PHP5/windows)
    if (image_rotate($image, $outfile, 360 - $angle)) {
		// // g.philippot- updated for windows environment that not support rename (at least in PHP 5.1.4) if dest file exist
		
		// g.philippot - changing error handler to hide system error message from rename
		$old_error_handler = set_error_handler("WindowsRenameErrorHandler");
		if (!rename($outfile, $image))
		{
			// g.philippot - restoring error handler
			set_error_handler($old_error_handler);

			// g.philippot - backuping dest file before any update
			if (copy($image,$image.'.bak'))
			{
				// g.philippot - deleting file
	 			unlink($image);
				// g.philippot - trying to move again
				if (!rename($outfile, $image))
				{
					drupal_set_message(t('Failled to move temp file to image file - rotation failed'));
					// g.philippot - trying to restore to previous stat
					if (copy($image.'.back',$image))
					{
						unlink($image.'.bak');
					}
					else
					{
						drupal_set_message(t('Failled to restore backup file' . $image.'.back You must rename it manually as ' . $image));
					}
				}
				else
				{
					unlink($image.'.bak');
				}
				
			}
			else
			{
				drupal_set_message(t('unable to backup old image file - rotation failed'));
			}
		}
		// g.philippot - restoring error handler
		set_error_handler($old_error_handler);

    } else {
        drupal_set_message(t('image rotate failed for %image', array('%image' => $image)));
        unlink($outfile);
        return false;
    }
    return true;
}

No more error now.
I also added a '360 - ' operation in image_rotate($image, $outfile, 360 - $angle)) {

to correct the clockwise/counterclockwise trouble. it work well now but I haven't investigated the real image_rotate trouble (the rotation angle asked is correct but not correctly managed).

I am new to drupal, so if my error handling is bad let me know.

Gael

vhmauery’s picture

g.philippot,

Just for future reference, a unified diff is much more useful to me and easier to read. Rather than posting a huge chunk of code, post the output of

diff -u acidfree.module.orig acidfree.module

(assuming that acidfree.module is the file you modfied and before you modified it, you copied it to acidfree.module.orig). The resulting text is what is affectionately called a unified diff, or a patch.

I would say the best solution to this problem would be to 1) not use windows and 2) not use GD. But that would be out of line. So I will try and work with you on this one. Give me some time and I will try to roll out a patch that should remedy your problems. I am glad you tracked them down for me.

vhmauery’s picture

Status: Active » Fixed

Okay, I have committed a patch that should fix the file rename problem. Instead of using rename, which evidently is not platform independent, I changed it to use the drupal file handling wrappers -- namely file_move and file_delete. This fix is in image_manip.inc version 1.7.2.1 and should be in a repackaged tarball in 12 hours or so.

As for the rotating the wrong way, that is not Acidfree's problem -- it is either a bug in Drupal's GD wrapper or in GD itself.

vhmauery’s picture

Status: Fixed » Closed (fixed)