Image rotate issue in PHP

Hi,

I have an image that's already uploaded onto a server. I'm now trying to rotate that image 180 degrees, in place, without using a GD installation.

Here's my code, combined with some I found online:

$Photo=$row['Photo'];		//image path retrieved from database, in the format "filename.jpg"

$thepath='C:/Users/Andrew/OneDrive/Andrew/Projects/htdocs/test/';

$fullpath = $thepath . 'images/listings/' . $Photo;

//find last .
$extensionbeginning=strrpos($Photo, '.');

//find extension
$ext=substr($Photo,$extensionbeginning+1,4);

if($ext=='jpg' || $ext=='jpeg')
{
	$src_img=imagecreatefromjpeg($fullpath);
}

else if($ext=='png')
{
	$src_img=imagecreatefrompng($fullpath);
}

else if($ext=='gif')
{
	$src_img=imagecreatefromgif($fullpath);
}


//****************************************************************


function ImageFlip ( $src_img, $mode )
{

	$width                        =    imagesx ( $src_img );
	$height                       =    imagesy ( $src_img );

	$src_x                        =    0;
	$src_y                        =    0;
	$src_width                    =    $width;
	$src_height                   =    $height;

	switch ( $mode )
	{
		case '1': //vertical
			$src_y                =    $height -1;
			$src_height           =    -$height;
		break;

		case '2': //horizontal
			$src_x                =    $width -1;
			$src_width            =    -$width;
		break;

		case '3': //both
			$src_x                =    $width -1;
			$src_y                =    $height -1;
			$src_width            =    -$width;
			$src_height           =    -$height;
		break;

		default:
			return $src_img;

	}

	$dst_img = imagecreatetruecolor ( $width, $height );

	if ( imagecopyresampled ( $dst_img, $src_img, 0, 0, $src_x, $src_y , $width, $height, $src_width, $src_height ) )
	{
		return $dst_img;
	}

	return $src_img;
}


//****************************************************************

ImageFlip($src_img,2);

if($ext=='jpg' || $ext=='jpeg')
{
	imagejpeg($dst_img,$fullpath);
}

else if($ext=='png')
{
	imagepng($dst_img,$fullpath);
}

else if($ext=='gif')
{
	imagegif($dst_img,$fullpath);
}

imagedestroy($src_img);

Open in new window


Problem: When I do the save, I get this error message:

Warning: imagejpeg() expects parameter 1 to be resource, null given

This occurs on this line near the end:    imagejpeg($dst_img,$fullpath);

Can someone please advise as to what I've done wrong?

Many thanks
kbitAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Try changing line 80 from
ImageFlip($src_img,2);

Open in new window

To
$dst_img = ImageFlip($src_img,2);

Open in new window

You are referencing dst_img in the imagejpeg call but it does not exist in the global scope. You use dst_img as a local variable in the ImageFlip function but it is not available outside of that function.

Therefore the error is 100% correct - you are giving it a null reference instead of the value returned from ImageFlip

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kbitAuthor Commented:
Thanks very much, you fixed the error! I also changed the mode from 2 to 1 and it looks great
Julian HansenCommented:
You are welcome. Good luck with it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.