Jump to content

Php And Image Return / Rotate


Thunderwolf
 Share

Recommended Posts

Let me explain, I've got an ASP.NET script which is sortof the same, but my free webhost isn't accepting ASP.NET... So, php then...I'm getting a file (which contains a few xml-styled parts, but is not just xml) through the img (like this: image.php?img=link to file). The file contains a "<thumbnail.jpg>" and corresponding "</thumbnail.jpg>" tag. Everything in between is a jpg image upside down. What I've got so far is that it's able to return the image, but it's still upside down.So off I went, searching for a script which could vertically flip the image. Now, I have found something, but it's still not working, and I don't really get why.EDIT: Oh, yeah. A version of this can be found at my website: http://www.thunderwolf.123bemyhost.com/ and then clicking one of the tracks. A specific example would be at Example

<?phpheader("Content-type: image/jpg");if (isset($_GET['img'])){$image = $_GET['img'];}else{$image = "img/avvy.png";}$completefile = "";$partfile = "";$startsearch = "<Thumbnail.jpg>";$endsearch = "</Thumbnail.jpg>";$file = fopen($image, "r") or exit("Unable to open file!");while(!feof($file)){	$c = fgetc($file);	$completefile = $completefile . $c;}fclose($file);$sf = (searchdatastring(0, $completefile, $startsearch)+15);$sf2 = searchdatastring($sf, $completefile, $endsearch);$partfile = copydata($sf, $sf2, $completefile);$image2 = image_flip($partfile, 'vert');imagejpeg($image2);print($image2);function copydata($startindex, $stopindex, $inputdata){	 $output = "";	 $start = $startindex;	 $stop = $stopindex;	 for ($start; $start < $stop; $start++)	 {		  $output = $output . $inputdata[$start];		  echo $inputdata[$start];	 }	 return $output;}function searchdatastring($start, $filedata, $stringtofind){$len = strlen($filedata);$i = $start;$poss = false;$found = false;$j = 0;while (!$found && $i < ($len - strlen($stringtofind))){    if ($filedata[$i] == $stringtofind[0])   {	   $poss = true;	   $j = 1;			   while ($poss && ($j < strlen($stringtofind)))	   {		   if ($filedata[($i + $j)] != $stringtofind[$j])		   {			   $poss = false;		   }		   else		   {			   $j++;		   }	   }  	   if ($poss)	   {		   $found = true;	   }	   else	   {		   $i++;	   }   }   else   {	   $i++;   }}if (!$found){ $i = 0;}$st = $i;return $st;}function image_flip($img, $type=''){	$width  = imagesx($img);	$height = imagesy($img);	$dest   = imagecreatetruecolor($width, $height);	switch($type){		case '':			return $img;		break;		case 'vert':			for($i=0;$i<$height;$i++){				imagecopy($dest, $img, 0, ($height - $i - 1), 0, $i, $width, 1);			}		break;		case 'horiz':			for($i=0;$i<$width;$i++){				imagecopy($dest, $img, ($width - $i - 1), 0, $i, 0, 1, $height);			}		break;		case 'both':			for($i=0;$i<$width;$i++){				imagecopy($dest, $img, ($width - $i - 1), 0, $i, 0, 1, $height);						}			$buffer = imagecreatetruecolor($width, 1);			for($i=0;$i<($height/2);$i++){				imagecopy($buffer, $dest, 0, 0, 0, ($height - $i -1), $width, 1);				imagecopy($dest, $dest, 0, ($height - $i - 1), 0, $i, $width, 1);				imagecopy($dest, $buffer, 0, $i, 0, 0, $width, 1);			}			imagedestroy($buffer);		break;	}	return $dest;}?>

EDIT: Got it:

<?phpheader("Content-type: image/jpg");if (isset($_GET['img'])){$image = $_GET['img'];}else{$image = "img/avvy.png";}$completefile = "";$partfile = "";$startsearch = "<Thumbnail.jpg>";$endsearch = "</Thumbnail.jpg>";$file = fopen($image, "r") or exit("Unable to open file!");while(!feof($file)){	$c = fgetc($file);	$completefile = $completefile . $c;}fclose($file);$sf = (searchdatastring(0, $completefile, $startsearch)+15);//echo "<br />" . $sf;$sf2 = searchdatastring($sf, $completefile, $endsearch);//echo "<br />" . $sf2;$partfile = copydata($sf, $sf2, $completefile);$imageNewtest = imagecreatefromstring($partfile);$image2 = image_flip($imageNewtest, 'vert');imagejpeg($image2);imagedestroy($image2);print($image2);function copydata($startindex, $stopindex, $inputdata){	 $output = "";	 $start = $startindex;	 $stop = $stopindex;	 for ($start; $start < $stop; $start++)	 {		  $output = $output . $inputdata[$start];	 }	 return $output;}function searchdatastring($start, $filedata, $stringtofind){$len = strlen($filedata);$i = $start;$poss = false;$found = false;$j = 0;while (!$found && $i < ($len - strlen($stringtofind))){    if ($filedata[$i] == $stringtofind[0])   {	   $poss = true;	   $j = 1;			   while ($poss && ($j < strlen($stringtofind)))	   {		   if ($filedata[($i + $j)] != $stringtofind[$j])		   {			   $poss = false;		   }		   else		   {			   $j++;		   }	   }  	   if ($poss)	   {		   $found = true;	   }	   else	   {		   $i++;	   }   }   else   {	   $i++;   }}if (!$found){ $i = 0;}$st = $i;return $st;}function image_flip($img, $type='vert'){	$width  = imagesx($img);	$height = imagesy($img);	$dest   = imagecreatetruecolor($width, $height);	switch($type){		case '':			return $img;		break;		case 'vert':			for($i=0;$i<$height;$i++){				imagecopy($dest, $img, 0, ($height - $i - 1), 0, $i, $width, 1);			}		break;		case 'horiz':			for($i=0;$i<$width;$i++){				imagecopy($dest, $img, ($width - $i - 1), 0, $i, 0, 1, $height);			}		break;		case 'both':			for($i=0;$i<$width;$i++){				imagecopy($dest, $img, ($width - $i - 1), 0, $i, 0, 1, $height);						}			$buffer = imagecreatetruecolor($width, 1);			for($i=0;$i<($height/2);$i++){				imagecopy($buffer, $dest, 0, 0, 0, ($height - $i -1), $width, 1);				imagecopy($dest, $dest, 0, ($height - $i - 1), 0, $i, $width, 1);				imagecopy($dest, $buffer, 0, $i, 0, 0, $width, 1);			}			imagedestroy($buffer);		break;	}	return $dest;}?>

Edited by Thunderwolf
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...