Bookmark Bookmark Bookmark Bookmark Bookmark Bookmark Bookmark My Bookmark - Become a Fan on Facebook!

LorenzGames, All about Flash Games!

Follow me on Twitter! Follow Me on Twitter

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!";}	
}



Article Comments


play free games - August 5th, 2010
It is really helpful. I know the codes on how to make thumbnails but don't have the knowledge on adjusting the color. That is an informative one. Thanks for sharing it.

Write a Comment: