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 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

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