How to resize a image in PHP

To resize a image in PHP you can use the following snippet.

Sample PHP

function ImageResize($imageName, $tempName, $maxWidth, $maxHeight)
{
	$extension = explode(".", $imageName);
	$extension = $extension[count($extension)-1];
 
	if($extension == "jpeg" || $extension == "jpg")
	{
		$image = imagecreatefromjpeg($tempName);
	}
        elseif($extension == "gif")
	{
		$image = imagecreatefromgif($tempName);
	}
	elseif($extension == "png")
	{
		$image = imagecreatefrompng($tempName);
	}
	
	$Width = imagesx($image);
	$Height = imagesy($image);
 
	if($Width <= $maxWidth && $Height <= $maxHeight)
	{
		return $image;
	}
 
	if($Width >= $Height) 
	{
		$newWidth = $maxWidth;
		$newHeight = $newWidth * $Height / $Width;
	}
	else 
	{
		$newHeight = $maxHeight;
		$newWidth = $Width / $Height * $newHeight;
	}
 
	$image2 = imagecreatetruecolor($newWidth, $newHeight);
	imagecopyresampled($image2, $image, 0, 0, 0, 0, floor($newWidth), floor($newWidth), $Width, $Height);
	return $image2; 
}

Please rate this snippet

How to resize a image using C# or VB.NET

To resize a image using C# or VB.NET you can use the following snippet.

Sample C#

public static Image ResizeImage(Image img, int width, int height)
{
    var newImage = new Bitmap(width, height);
    using (var gr = Graphics.FromImage(newImage))
    {
       gr.SmoothingMode = SmoothingMode.HighQuality;
       gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
       gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
       gr.DrawImage(img, new Rectangle(0, 0, width, height));
     }
     return newImage;
}

public static Image ResizeImage(Image img, Size size)
{
	return ResizeImage(img, size.Width, size.Height);
}

public static Image ResizeImage(Bitmap bmp, int width, int height)
{
	return ResizeImage((Image)bmp, width, height);
}

public static Image ResizeImage(Bitmap bmp, Size size)
{
	return ResizeImage((Image)bmp, size.Width, size.Height);
}

Sample VB.NET

Public Shared Function ResizeImage(img As Image, width As Integer, height As Integer) As Image
	Dim newImage = New Bitmap(width, height)
	Using gr = Graphics.FromImage(newImage)
		gr.SmoothingMode = SmoothingMode.HighQuality
		gr.InterpolationMode = InterpolationMode.HighQualityBicubic
		gr.PixelOffsetMode = PixelOffsetMode.HighQuality
		gr.DrawImage(img, New Rectangle(0, 0, width, height))
	End Using
	Return newImage
End Function

Public Shared Function ResizeImage(img As Image, size As Size) As Image
	Return ResizeImage(img, size.Width, size.Height)
End Function

Public Shared Function ResizeImage(bmp As Bitmap, width As Integer, height As Integer) As Image
	Return ResizeImage(DirectCast(bmp, Image), width, height)
End Function

Public Shared Function ResizeImage(bmp As Bitmap, size As Size) As Image
	Return ResizeImage(DirectCast(bmp, Image), size.Width, size.Height)
End Function

Here are the same methods as Extensions Methods

Sample C#

public static Image ResizeImage(this Image img, int width, int height)
{
    var newImage = new Bitmap(width, height);
    using (var gr = Graphics.FromImage(newImage))
    {
       gr.SmoothingMode = SmoothingMode.HighQuality;
       gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
       gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
       gr.DrawImage(img, new Rectangle(0, 0, width, height));
     }
     return newImage;
}

public static Image ResizeImage(this Image img, Size size)
{
	return ResizeImage(img, size.Width, size.Height);
}

public static Image ResizeImage(this Bitmap bmp, int width, int height)
{
	return ResizeImage((Image)bmp, width, height);
}

public static Image ResizeImage(this Bitmap bmp, Size size)
{
	return ResizeImage((Image)bmp, size.Width, size.Height);
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function ResizeImage(img As Image, width As Integer, height As Integer) As Image
	Dim newImage = New Bitmap(width, height)
	Using gr = Graphics.FromImage(newImage)
		gr.SmoothingMode = SmoothingMode.HighQuality
		gr.InterpolationMode = InterpolationMode.HighQualityBicubic
		gr.PixelOffsetMode = PixelOffsetMode.HighQuality
		gr.DrawImage(img, New Rectangle(0, 0, width, height))
	End Using
	Return newImage
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function ResizeImage(img As Image, size As Size) As Image
	Return ResizeImage(img, size.Width, size.Height)
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function ResizeImage(bmp As Bitmap, width As Integer, height As Integer) As Image
	Return ResizeImage(DirectCast(bmp, Image), width, height)
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function ResizeImage(bmp As Bitmap, size As Size) As Image
	Return ResizeImage(DirectCast(bmp, Image), size.Width, size.Height)
End Function

Please rate this snippet