Facebook Bookmark Bookmark Bookmark Bookmark Bookmark Bookmark Bookmark Bookmark My Bookmark

LorenzGames, All about Flash Games!

Follow me on Twitter!

Best PHP image resizer and converter Jpeg or PNG to GIF

February 20th, 2010 - Category: PHP
Few months ago I created the ultimate image resizer in PHP.
Today I slightly modified it, to make it work in a way that the final image has a better quality and a smaller size.
It transforms any kind of image to a GIF file of the size you want.
Perfect if you have a portal with a non conventional thumbnail size.
As you can see in the image below i transformed a 100x100 Jpeg image into a 80x80 Gif.
Best PHP Image resizer

The usage is very simple: upImg($_FILE, "http://url (if the first parameter doesn't exists)", Size Width, Size Height, "output name ex:temp/name")

upImg('','image.jpg',80,80,'image');
 
function upImg($file,$urlfile,$sizew,$sizeh,$uploadfile){
	$uploadfile="temp/".$uploadfile.".gif";
 
	if($urlfile=="Or write the URL of the IMG here..."){$urlfile="";}
	if($urlfile!=""){
		$file = array("size" => 0, "type" => "");
		$file["size"] = strlen(file_get_contents($urlfile));
		$file["type"] = strtolower(str_ireplace(".", "", substr($urlfile, -4)));
	}
 
	if($file["size"]>200 && ($file["size"]/1024)<300){
		$ft=str_ireplace("image/","",$file["type"]);
		if ($ft=="gif" || $ft=="png" || $ft=="jpeg" || $ft=="pjpeg" || $ft=="jpg"){
			if($urlfile==""){move_uploaded_file($file["tmp_name"],$uploadfile);}
			else{file_put_contents($uploadfile, file_get_contents($urlfile));}			
			if ($ft=="gif"){$simg = ImageCreateFromGIF($uploadfile);}
			if ($ft=="png"){$simg = ImageCreateFromPNG($uploadfile);}
			if ($ft=="jpeg" || $ft=="pjpeg" || $ft=="jpg"){$simg = ImageCreateFromJPEG($uploadfile);}
			$dimg = imagecreatetruecolor($sizew, $sizeh);		
 
			$swdt = imagesx($simg); 
			$shgt = imagesy($simg);			
 
			$difwh=0;
			$difhh=0;
 
			if($swdt>$shgt){
				$difw=$swdt-$shgt;
				$swdt=$shgt;
				$difwh=$difw/2;
			}
 
			if($swdt<$shgt){
				$difh=$shgt-$swdt;
				$shgt=$swdt;
				$difhh=$difh/2;
			}			
 
			ImageCopyResampled($dimg, $simg, 0, 0, $difwh, $difhh, $sizew, $sizeh, $swdt, $shgt);
 
			$sizews	= $sizew * (750.0 / $swdt);
			$a		= 52;
			$b		= -0.27810650887573124;
			$c		= .00047337278106508946;				
			$result = $a + $b * $sizews + $c * $sizews * $sizews;			
			$sharpness	= max(round($result), 0);
 
			$sharpenMatrix	= array(
				array(-1, -2, -1),
				array(-2, $sharpness + 12, -2),
				array(-1, -2, -1)
			);
			$divisor		= $sharpness;
			$offset			= 0;
			imageconvolution($dimg, $sharpenMatrix, $divisor, $offset);
 
 
			$gifimg = ImageCreate($sizew, $sizeh);
			imagetruecolortopalette($dimg, false, 1000);
			ImagePaletteCopy($gifimg, $dimg);			
			$w = 0;
			for ($y = 0; $y < $sizeh; $y++)  {
				$ow = $w; $w = round($y + 1);
				$t = 0;
				for ($x = 0; $x < $sizew; $x++)  {
					$r = $g = $b = 0; $a = 0;
					$ot = $t; $t = round($x + 1);
					for ($u = 0; $u < ($w - $ow); $u++)  {
						for ($p = 0; $p < ($t - $ot); $p++)  {
							$c = ImageColorsForIndex($dimg, ImageColorAt($dimg, $ot + $p, $ow + $u));
							$r += $c['red'];
							$g += $c['green'];
							$b += $c['blue'];
							$a++;
						}
					}
					if($a!=0){ImageSetPixel($gifimg, $x, $y, ImageColorClosest($gifimg, $r / $a, $g / $a, $b / $a));}
				}
			}
 
 
			imageGif($gifimg, $uploadfile);
			imagedestroy($simg);
			imagedestroy($dimg);
			imagedestroy($gifimg);			
			$result=$uploadfile;
		}	
	}	
	return $result;	
}


Write a comment

How to create rapidly icons for your game.

February 19th, 2010 - Category: Developers
Some one doesn't know that on LorenzGames.com there is a Thumbnail Icon Generator.
It can became a very useful tool if you want to create rapidly icons for your game in any possible size.

Choose the sizes you want, then you have 2 rectangular marker on the screen, the gray one is to localize the portion of image you want to use, the red one is used to manage rectangular icons about what portion you want to use, if top, center or down.
The option "Scale Mode" is used to create similar scaled icon resizing the big one.

As you can see in this image, I used a Scale Mode icon generation and the red marked is moved on the top.
Icon Generator

Then simply click on "Done" and the application will generate PNG and GIFs files you need. You will be able to download a ZIP file containing them all.


Write a comment

Build Apps for IPhone with Flash, finally!

October 6th, 2009 - Category: Developers, Flash
Looks like finally it's possible, thanks to Flash CS5, it will be possible to create Apps for IPhone and IPod Touch with firmware 3.0 or higher.

Flash on Iphone

Great news actually, I was getting tired of all that people telling me: "Hey why don't you learn some Unity, so you can make Apps for IPhone and be a millionaire in few days!!!".

The difference with Unity will be anyway visible, Flash requires more memory to run... but I guess for most of Apps and 2D games, Flash will do great.
For 3D stuff there is still a lot of work to do...

One important thing that many people didn't understand is that, it's not that IPhone will build another firmware that will be able to read Flash files; Flash CS5 will support a new kind of Output that will work on IPhone.
So the firmware of the Iphone is alwasy the same, Flash changes, infact this thing will be possibile only with Flash CS5.
That is why the Safari Browser will not support Flash anyway...
The beta should be released by the end of 2009, you can subscribe here to be notified when it will be ready.

While waiting, please read this interesting this Developer FAQ about Flash CS5 and Iphone.


Write a comment

Add sounds using a dynamic code with Sonoflash.

September 29th, 2009 - Category: ActionScript 3, Developers, Sounds

Sonoflash in amazing, you can basically add sounds to your flash games just writing some code, and the sounds will be distorted and changed however you want.

You can choose from a list they provide on theire site and download, for free, the sound that you need.

SonoFlash

Check this video by Ryan Stewart.



Write a comment

20 useful Flash API's and Tools

July 1st, 2009 - Category: Developers, Flash
Reading the MochiAds Forum I have found an interesting post containing a bunch of useful Flash API's and Tools. I elaborated it a lil bit and here it is, the bible for every Flash Developer! Emoticon

Flash Developers

Mutiplayer APIs SmartFoxServer is probably the most used, it is the core of many Multiplayer Games, probably the biggest one is Club Penguin. ElectroServer powers Webkinz. Either one and the other give you the possibility to create your game from zero, lobby included. They cost probably too much for an independent developer. I personally have used a lot the Nonoba, winning many contest on their site and the 2ND place of the final contest. It is easy to use and it is free but sometimes I found it a lil bit slow and it didn't let me customize the lobby. I didn't like very much to have a white and green lobby for all my games. BlossomServer is new and it has been created by Jiggmin, the author of Platform Racing. It is free and it gives you the possibility to download the source code and change if you want. The only problem is that it is based on a PHP socket server... which is not stable as Java or C.Red5 is a free java server, but probably it crashes too much.

3D APIs I have used all of them and honestly my favourite is Away3D.It is quite simple to use and it has a lot of functions for game designer.I wrote an article about it few months ago, you can read it here .

3D physics APIs This is the only 3D physics engine that I'm aware of for Flash. From looking at a few of the tutorials, it looks like you would have use this physics API in conjunction with a 3D API so you can render what's actually going on. I haven't had a chance to actually use the API, so I can't give much of an opinion.
2D physics APIs I have used Box2D in my last game Dynamic Systems http://www.lorenzgames.com/game/dynamic-systems .
They have a nice community and it is easy to find tutorials on Internet.

IDEs From what i have heard FlashDevelop is great. It has great syntax highlighting, auto completion, and it's just overall a whole lot better to use compared to the built in Actionscript editor in the Flash authoring tool, plus it's free! FDT should be good too, but it's not free.

TweenersA Tweener is basically a tool that creates transactions in an object.
For example if you need to move an object from point A to point B with an animation.
The Caurina one is one of my favourite, probably because i have used it for so long.
They have surely saved me a lot of work.

Debuggers

For now this is all, if you have any other interesting Tool please contact me. Emoticon


Write a comment

How to import games to your site from MochiAds

June 27th, 2009 - Category: MochiAds, PHP, Publishers
MochiAds gives you the possibility to directly download the games to your site through JSON.
The problem is that many people has no clue how this thing works, that's why i have created a script that not only allows you to Download how many games you want from theire site, but gives you the possibility to use the button "Post to my site" that is on the page of every game on MochiAds for manuale downloading.

Mochiads Logo

This script will only unpack the JSON, then you will have to put everything in your own Database and downloading the files the way you want wherever you want. For example you can use the method:
file_put_contents("where you want the file to be", file_get_contents("url of the file on mochiads"));

The only thing you have to change in the code is the Publisher ID, you can find your own here.
If you are going to use the manual submission from MochiAds dont forget to add the link to the file where you put this script in "Auto POST URL".
I set the type of games to download to only featured games, if you want to download all just change "featured_games" to "all".
Please enjoy and share. Emoticon

PHP
$pubID = "Here put your Publisher ID";
$gt=$_POST['game_tag'];
$extrag=$_POST['extrag'];
$typeGame="featured_games"; // Change it to 'all' if you want to import all the games.
 
if($gt!=""||$extrag!=""){	
	if($gt!=""){
		$json=file_get_contents("http://www.mochiads.com/feeds/games/".$pubID."/".$gt."/?format=json");
		$n=1;
	}
 
	if($extrag!=""){
		$json=file_get_contents("http://www.mochiads.com/feeds/games/".$pubID."/".$typeGame."/all?format=json&limit=".$extrag);
		$n=$extrag;
	}
 
	$jsonArr=json_decode($json,true);
 
	$g=0;
	while($g<$n){
		$gameArr=$jsonArr["games"][$g];
		$name=$gameArr["name"];
		$description=$gameArr["description"];
		$instructions=$gameArr["instructions"];
		$swfUrl=$gameArr["swf_url"];
		$imgUrl=$gameArr["thumbnail_url"];	
		$category=$gameArr["categories"][0]; // This is an Array [0] will take the first catgeory choosen.		
		$keywords="";
		$nTags=count($gameArr['tags']);
		for($i=0;$i<$nTags;$i++){
			$keywords=$keywords.$gameArr['tags'][$i].' ';
		}
		$width=$gameArr["width"];
		$height=$gameArr["height"];
		$g++;
 
		echo "<a target='_blank' rel='nofollow' href=".$swfUrl." target="_blank"><img style='margin:10px 0px 10px 0px; border: solid 2px #FFFFFF;' src=".$imgUrl."> ".$name."</a><br>";
	}
}


HTML
 <br><input name="extrag" value="5" size="5" type="text"><br>MochiAds Games <input value="Download" name="Submit" type="submit"><br> 


Write a comment

Make a game with PlayCrafter without a line of code!

June 25th, 2009 - Category: Developers
If you have alwasy wanted to make a game but you have zero clue about programming, today to help you there is PlayCrafter!
This brand new site is like a huge level editor, very similiar to Splooder that has tons of objects to just drag and drop on the stage.
It has the potential to make really good games.
It's like the TorqueX of Flash. So in some sorts, yes, it is more like a powerful level editor...

Watch this teaser to understand better:



That's PlayCrafter! not a single line of code needed...
As you can see you have a big list of objects divided in categories, plus you can use game templates to make just the game you want.

There are almost 20 game templates to choose.

PlayCrafter Template

On LorenzGames we have accepted 2 games made with PlayCrafter, one is Matcheroo and the other one is Buggy Bricks.

Just remember that not a line of code means you have the control they gave you.
You will not be able to earn anything from them and of course you will not be able to sell licenses of them or place the logo of your site.
I think the idea is quite good, people may start with this and then as they strive for more options they will eventually have to turn to programming in Flash!


Write a comment

The ultimate PHP image resizer to create perfect thumbnails!

June 23rd, 2009 - Category: PHP
I have recently created a new image resizer in PHP.
This not only scale and resize with an amazing quality, but I created some parameters to give effects as you can see in this image.
Best PHP Image resizer
You can:
  1. Simply resize your image the dimension you want and it will AutoCrop too.
  2. Darken the image.
  3. Make it brighter.
  4. Invert the colors.
  5. Use less colors.
the usage is very simple: upImg($_FILE, or "http://url", Size Width, Size Height, "output name ex:temp/name", "darker", "brighter", "colors", Invert: 0 or 1)

Here is th code, enjoy! Emoticon
upImg('','ds.jpg',80,80,'namefile',0,0,256,0);
 
function upImg($file,$urlfile,$sizew,$sizeh,$uploadfile,$squared=0,$squared2=0,$pal=256,$inv=0){
	$uploadfile.=".gif";	
 
	if($urlfile!=""){
		$file = array("size" => 0, "type" => "");
		$file["size"] = strlen(file_get_contents($urlfile));
		$file["type"] = strtolower(str_ireplace(".", "", substr($urlfile, -4)));
	}
 
	if($file["size"]>200 && ($file["size"]/1024)<3000){
		$ft=str_ireplace("image/","",$file["type"]);
		if ($ft=="gif" || $ft=="png" || $ft=="jpeg" || $ft=="pjpeg" || $ft=="jpg"){
			if($urlfile==""){move_uploaded_file($file["tmp_name"],$uploadfile);}
			else{file_put_contents($uploadfile, file_get_contents($urlfile));}			
			if ($ft=="gif"){$simg = ImageCreateFromGIF($uploadfile);}
			if ($ft=="png"){$simg = ImageCreateFromPNG($uploadfile);}
			if ($ft=="jpeg" || $ft=="pjpeg" || $ft=="jpg"){$simg = ImageCreateFromJPEG($uploadfile);}			
 
			$swdt = imagesx($simg); 
			$shgt = imagesy($simg);
			if($swdt<$sizew){$sizew=$swdt;}
			if($shgt<$sizeh){$sizeh=$shgt;}
			$dimg = ImageCreate($sizew, $sizeh);
			$rX = $swdt / $sizew;
			$rY = $shgt / $sizeh;	
 
			if($sizeh>$shgt*($sizew/$swdt)){		
				$rX = $swdt /($swdt *($sizeh/$shgt));
				$Rx=((($swdt*($sizeh/$shgt))-$sizew)/2)*$rX;
			}			
 
			if($sizeh<$shgt*($sizew/$swdt)){
				$rY = $shgt /($shgt*($sizew/$swdt));
				$Ry=((($shgt*($sizew/$swdt))-$sizeh)/2)*$rY;			
			}			
 
			imagetruecolortopalette($simg, false, $pal);			
			ImagePaletteCopy($dimg, $simg);
 
			$w = 0;
			for ($y = 0; $y < $sizeh; $y++)  {
				$ow = $w; $w = round(($y + 1) * $rY+$Ry);
				$t = 0;
				for ($x = 0; $x < $sizew; $x++)  {
					$r = $g = $b = $squared2; $a = $squared;
					$ot = $t; $t = round(($x + 1) * $rX+$Rx);
					for ($u = 0; $u < ($w - $ow); $u++)  {
						for ($p = 0; $p < ($t - $ot); $p++)  {
							$c = ImageColorsForIndex($simg, ImageColorAt($simg, $ot + $p, $ow + $u));
							$r += $c['red'];
							$g += $c['green'];
							$b += $c['blue'];
							$a++;
						}
					}
					ImageSetPixel($dimg, $x, $y, ImageColorClosest($dimg, abs(($inv*256)-($r / $a)), abs(($inv*256)-($g / $a)), abs(($inv*256)-($b / $a))));
				}
			}			
 
			imageGif($dimg, $uploadfile);
			imagedestroy($simg);
			imagedestroy($dimg);			
			return $uploadfile;			
		}
		else{echo "Error: The file must be a GIF, PNG, or JPG!";}
	}
	else{echo "Error: File Size incorrect!";}	
}


Write a comment

How to add MochiAds to your game and earn money from it

June 21st, 2009 - Category: Developers, Flash, Monetize
As many of you know it, Mochiads as improved a lot in the past 2 years.
One of its competitors, GameJacket, failed last week.
Many developers are doing good money with the Mochi Advertising right now.
Are you doing it? Probably you should give it a try!

Mochiads Logo

Mochiads Version Control not only works as advertising but also protect the code of your game.
Here I am going to explain you how to add it to one of your games in ActionScript 3.

First important thing to do is, if you don't have an account, click here to Sign-Up MochiAds Developers Program.
Login in the dashboard and click on the yellow button Setup New Game.
Now it will ask you:
  1. The Title of your game.
  2. The dimensions of you game, write it how big it is, like 640x480 for example (The game must be not smaller than 300x300).
  3. Version Controll click Yes.
  4. Create Game.
In the Version Controll version you dont need to download the Include files from MochiAds.
Now it will ask you to Authenticate your game, write that string of code on the first frame of the time line, or in the document class. The string will be something like this:
var _mochiads_game_id:String = "your code";


If you are using a Document Class remember to clear everything from the Main Function.
For example let's pretend your Document Class is named MyGame.As inside you need to be sure it will look something like this:
package {
	//...imports
 
	public dynamic class MyGame extends MovieClip{
		//...declarations of variables
 
		public function MyGame () {
			//Here nothing!
		}
 
		//... other functions.
	}
}


Now select the colors of you preloader, you can even upload a custom image if you want, make a custom JPG the same size of your game and upload it to make it appear under the preloader line. It will look more fancy!

Then upload your SWF, give it a version number, like "1.0" or" 0.5alpha"... as you want... Emoticon
And eneter a little description only for you, like "First upload".

OK, now continue, download the file they just created for you, it contains the SWF you will have to distribute.
It's almost over Emoticon.
You need to fill out the game profile.

In Game URL write the Link of a page where people will be able to see your game and then upload the swf you just got there.

Add Description, an icon of 100x100 (I suggest you to make a very cute icon becouse it is the first thing people will see), Catoegories, Keywords and Save.
Great, now you are DONE!

Last thing to do if you want is to enable your game for distribution so that Mochiads will help you to spread it on internet.


Write a comment

Moving Bones in AS3 and Flash CS4

June 19th, 2009 - Category: ActionScript 3, Developers, Flash
This tutorial will teach you the basics to add bones to an object and move them using ActionScript 3.

It is quite simple, those are the steps to do:
  1. Design something ( to test i designed something like a snake Emoticon ).
  2. Click on the button Bone Tool (X).
  3. Draw a bunch of bones on the object making sure they are attached.
OK, now you should have something like this:
A Snake moved with bones in AS3
Make sure the last bone on the head is called: ikBoneName6

As you can see in the TimeLine now you have a new layer called Armature_(Number), rename it to Armature_1 if it has another number.

Now click on the Armature_1 layer and you should see in the properties something like this:
Armature Bones setting in AS3
Make sure to change the Type in Runtime.

Now create an empty layer where to place the ActionScript on the TimeLine (to keep the tutorial short i placed everithing on the timeline).
import fl.ik.*;
 
var tree:IKArmature = IKManager.getArmatureByName("Armature_1");
var bone:IKBone = tree.getBoneByName("ikBoneName6");
 
// tailJoint will manage the point of your bone, if you want to manage the head of your bone you need to use headJoint
var tJ:IKJoint = bone.tailJoint;
var posT<img src='http://www.lorenzgames.com/smiles/P.gif' alt='Emoticon' width='14' height='14' />oint=tJ.position;
 
var ikMover:IKMover = new IKMover(tJ, posT);
 
addEventListener(Event.ENTER_FRAME, onFrame); 
function onFrame(event:Event){
	posT.x=mouseX;
	posT.y=mouseY;
	ikMover.moveTo(posT);
}


Now you are set, Test the movie and you should get something like this:



Of course spending lil bit more of time will make this a lot better! Emoticon


Write a comment