Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

imagerotate function not working

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
US
trying to write a function that will rotate an image. I have the GD2 extension loaded, I have PHP 4.3.2 ( I had to upgrade as its said imagerotate() doesnt work on lower than 4.3.0 )

this is the function with some debug echos in it.

Code:
function rotatePix($sourcefile, $targetfile, $degrees)
{
	echo &quot;sourcefile : &quot;.$sourcefile.&quot;<BR>targetfile: &quot;.$targetfile.&quot;<BR>degrees: &quot;.$degrees.&quot;°<BR>&quot;;
	$img_size = getImageSize($sourcefile);
	$x = $img_size[0];
	$y = $img_size[1];
	echo &quot;Image Size Obtained.<BR>&quot;;
	if ($x > $y)
	{
 		$newd = $x;
	}
	else
	{
 		$newd = $y;
	}
	

	$src_img = ImageCreateFromJPEG($sourcefile);
	echo &quot;Source created.<BR>&quot;;
	$dst_img = ImageCreateTrueColor($newd,$newd);
	echo &quot;Destination Prepared.<BR>&quot;;
	$final_img = ImageCreateTrueColor($y,$x);
	
	ImageCopyResampled($dst_img,$src_img,0,0,0,0,$x,$y,$x,$y);
	echo &quot;Copy Created.<BR>&quot;;
	
	$rotated_img = imagerotate($dst_img, $degrees,0);
	echo &quot;Image Rotated.<BR>&quot;;
	ImageCopyResampled($final_img,$rotated_img,0,0,0,0,$y,$x,$y,$x);
	echo &quot;Rotated Image Copied to Destination.<BR>&quot;;
	ImageJPEG($final_img, targetfile);
}

The last echo I see is &quot;Copy Created&quot;, I Cant get any code past the imagerotate function.

Karl Blessing
PHP/MySQL Developer
Envynews.com
 
actually thats the thing, it wont produce any errors at all, the script just stops at that line.

Also when I setup PHP 4.3.2 on my windows box ( the one I was originally trying is the production server on a Redhat Linux box, using Apache 1.3.* , and PHP 4.3.2 ) , which is running apache 1.3.* , I run the same code, but no error is generated, but no stoping of the script, it acts as if it worked, but the image does not end up getting rotated.

Karl Blessing
PHP/MySQL Developer
Envynews.com
 
Code:
Going to Rotate Function.
sourcefile : ../../reviews/images/89/1.jpg
targetfile: ../../reviews/images/89/1.jpg
degrees: 90°
Image Size Obtained.
Source created.
Destination Prepared.
Copy Created.
Image Rotated.
Rotated Image Copied to Destination.

then the rest of the page loads as normal. and I have show_errors and everything turned on my development box ( windows )

Karl Blessing
PHP/MySQL Developer
Envynews.com
 
PHP Version 4.3.2

System Windows NT KBLESS 5.1 build 2600
Build Date May 28 2003 15:06:05
Server API Apache
Virtual Directory Support enabled
Configuration File (php.ini) Path C:\WINDOWS\php.ini
PHP API 20020918
PHP Extension 20020429
Zend Extension 20021010
Debug Build no
Thread Safety enabled
Registered PHP Streams php, http, ftp, compress.zlib

apache
Apache for Windows 95/NT

Apache Version Apache/1.3.24
Apache Release 10324100
Apache API Version 19990320
Hostname:port kb244:80
Timeouts Connection: 300 - Keep-Alive: 15

Directive Local Value Master Value
child_terminate 0 0
engine 1 1
last_modified 0 0
xbithack 0 0


Apache Environment
Variable Value
COMSPEC C:\WINDOWS\system32\cmd.exe
DOCUMENT_ROOT c:/HTTP_ACCEPT image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*
HTTP_ACCEPT_ENCODING gzip, deflate
HTTP_ACCEPT_LANGUAGE en-us
HTTP_CONNECTION Keep-Alive
HTTP_COOKIE PHPSESSID=0ca008823f438e96f1a74af3cf34f0de
HTTP_HOST localhost
HTTP_USER_AGENT Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; Alexa Toolbar)
PATH C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\ATI Technologies\ATI Control Panel;
REMOTE_ADDR 127.0.0.1
REMOTE_PORT 2983
SCRIPT_FILENAME c:/SERVER_ADDR 127.0.0.1
SERVER_ADMIN kb244@kb244.com
SERVER_NAME kb244
SERVER_PORT 80
SERVER_SIGNATURE <ADDRESS>Apache/1.3.27 Server at kb244 Port 80</ADDRESS>
SERVER_SOFTWARE Apache/1.3.27 (Win32) PHP/4.3.2
SystemRoot C:\WINDOWS
WINDIR C:\WINDOWS
GATEWAY_INTERFACE CGI/1.1
SERVER_PROTOCOL HTTP/1.1
REQUEST_METHOD GET
QUERY_STRING no value
REQUEST_URI /php.php
SCRIPT_NAME /php.php


gd
GD Support enabled
GD Version bundled (2.0.12 compatible)
FreeType Support enabled
FreeType Linkage with freetype
GIF Read Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
XBM Support enabled


Karl Blessing
PHP/MySQL Developer
Envynews.com
 
reason it wasnt working on windows , is because targetfile didnt have a $ in front of it.

once I got it working in windows, I had to rewrite the functoin to work with 180 , 90 CW or 90 CCW, since the way the rotation varies from how many degrees you give it.

This is the final function that works, now I just got to figure out why its not working on the linux box( production server ) , as it wont even get past the imagerotate function on the production server.

Code:
function rotatePix($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);	
}

Of couse as you can see, the function is only intended for a 90 CW/CCW and 180 degree rotation, not for any kind of skewed rotation.

We might have to re-do the linux box soon anyways.

Karl Blessing
PHP/MySQL Developer
Envynews.com
 
If possible install ImageMagick and get it to do the rotation, and basically every other image manipulation process you can think of.
 
I really don't like the idea of giving up just yet.

Karl Blessing
PHP/MySQL Developer
Envynews.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top