PHP: Resize Image on Upload | Thumbnail Image Resizing

The script below allows you to resize an image while keeping proportions.
The new width and height that you pass into the parameters are the maximum dimensions allowed.

As an example, you want profile images to be NO larger than 120x150px and the uploaded images have to adhere to these dimensions without being stretched. If your user uploads a 300x500px image, that image will be resized to 90x150px.

	Function resize($filename_original,$filename_resized,$new_w,$new_h)
	creates a resized image
	$filename_original	Original filename
	$filename_resized	Filename of the resized image
	$new_w		width of resized image
	$new_h		height of resized image
public static function resize($filename_original, $filename_resized, $new_w, $new_h) {
	$extension = pathinfo($filename_original, PATHINFO_EXTENSION);

	if ( preg_match("/jpg|jpeg/", $extension) ) $src_img=@imagecreatefromjpeg($filename_original);

	if ( preg_match("/png/", $extension) ) $src_img=@imagecreatefrompng($filename_original);

	if(!$src_img) return false;

	$old_w = imageSX($src_img);
	$old_h = imageSY($src_img);

	$x_ratio = $new_w / $old_w;
	$y_ratio = $new_h / $old_h;

	if ( ($old_w <= $new_w) && ($old_h <= $new_h) ) {
		$thumb_w = $old_w;
		$thumb_h = $old_h;
	elseif ( $y_ratio <= $x_ratio ) {
		$thumb_w = round($old_w * $y_ratio);
		$thumb_h = round($old_h * $y_ratio);
	else {
		$thumb_w = round($old_w * $x_ratio);
		$thumb_h = round($old_h * $x_ratio);


	if (preg_match("/png/",$extension)) imagepng($dst_img,$filename_resized); 
	else imagejpeg($dst_img,$filename_resized,100); 


	return true;

Post a Comment