Open Image, rotate, resize and save...?

I’ve used the imageGD library in the past with php but mainly just to resize and save an uploaded image.

Now I’m trying to open an image (on my server) rotate it, and then save it… and I’m having trouble…


if(isset($_POST['file']) && isset($_POST['degrees']) && isset($_POST['folderID']))
	{
	
	$file = $_POST['file'];
        $degrees = $_POST['degrees'];
         $folderID = $_POST['folderID'];

        $image = SITE_URL.'inventoryImages/'.$folderID.'/source/'.$file;
	$source = imagecreatefromjpeg($image) ;
	$rotate = imagerotate($source, $degrees, 0);
	move_uploaded_file($rotate, $fileLoc);

	$sizes = $dealer->getSizeList("imageSize");
	
	//var_dump($sizes);

	foreach($sizes as $size)
		{
		$width = $size['width'];
		$height = $size['height'];
		unlink(APP_DIR.'/inventoryImages/'.$folderID.'/'.$width.'px/'.$file);
		
		$fileFinLoc = APP_DIR.'/inventoryImages/'.$folderID.'/'.$width.'px/'.$file;
		
		// calls the resizeimage class and it's resize limit width/height method.
		$rimg=new RESIZEIMAGE($fileLoc);
		echo $rimg->error();
		$rimg->resize_limitwh($width, $height, $fileFinLoc); 
		}

	
	}



I’m getting the file path, I’m getting the degrees to rotate, I’m getting my image sizes… no errors… image stays the same.

The flow I’m shooting for is this

Open source image
Rotate
Save source image (over old source image)
resize that source image and save to multiple directories (over existing non-rotated images)

I’ve googled quite a bit looking for a basic tutorial and am open to ideas suggestions…etc

Thanks in advance!

First thing I would do is to call move_uploaded_file immediately after the upload to a temporary location and then work with the temporary file.

Also what is the RESIZEIMAGE class? Where did you get that class?
and I also see you use unlink() from inside the loop, but does this mean you are basically deleting the same original file several times?

You should probably move the unlink() from inside the loop to call it after the loop or if you want to keep it inside the loop at least add the @, like this: @unlink()

Then why do you use echo $rimg->error();?

Also, I can’t understand from your script - does your form accept uploaded file or do you only request a user selects a file name $_POST[‘file’] of existing file?

I just don’t see the $_FILES array anywhere so I am not sure how you handle uploaded files.

If your form does not actually allow uploading of an image, then move_uploaded_file() will not do anything.

I just don’t see the $_FILES array anywhere so I am not sure how you handle uploaded files.

You don’t see it b/c this script is not trying to alter an uploaded file, it’s trying to edit and save a file that’s already on my server (in the /source/ directory).

The only thing I’m receiving from the $_POST is the folderID and the filename $file and the degrees to be rotated.

So, the image is already a file on my server. I just need to rotate, resize and save.

My RESIZEIMAGE class is an external class that assists in resizing (mainly to keep from repeating code)

The RESIZEIMAGE class works 100%, it is used in mulitiple other image uploads for this site.

in that case move_uploaded_file() is not going to do anything. You should use rename() instead

not sure why I’m having trouble with this so much…
let me break this down a little


	$image = APP_DIR.'/inventoryImages/'.$folderID.'/source/'.$file;
	
	$source = imagecreatefromjpeg($image) ;
	
	$rotate = imagerotate($source, $degrees, 0);

This is getting the source file, and rotating it. How do I save the source file over the original at it’s original size/quality?

To save image you use
imagejpeg($rotate, $path);

the $path is where you want to save the image, for example ‘/images/something.jpg’
the $path must be writable to php

you can test to make sure image is saved:

if(true !== imagejpeg($rotate, $path)){
exit(‘unable to save image’);
}

Something like that.

k, I think that’s putting me on the right path.

I have the image folders/files set to 755 for permission. And I’m getting this error…? (where mywebsite.com has been substituted for my domain)

note: I can copy the url it’s displaying in the error message and paste it in the browser and it takes me right to the image.

<b>Warning</b>: imagejpeg() [<a href=‘function.imagejpeg’>function.imagejpeg</a>]: Unable to open ‘http://www.mywebsite.com/inventoryImages/inv4a886367ba1ba/source/520SC_CockpitA_m.jpg’ for writing: No such file or directory in <b>/home/somesite/public_html/logic/ajax/ajax-ImageRotate.php</b> on line <b>38</b><br />

OK, so you currently using the http path to image, not the system path. Probably you using something like SITE_URL constant which is the url of your site.

You need to use a full path to image somethink like
‘/home/somesite/public_html/logic/inventoryImages/someimage.jpg’

Man, I can’t believe I didn’t see that…

Thanks for taking the time to help me work though this bonehead mistake!

Much appreciated! Worked perfect!