Funcția următoare vă ajută să modificați dinamic dimensiunea imaginilor folosind GD.

Codul de mai jos poate fi adăugat într-un fisier php - image.php

<?php
function resize_image($source_image, $destination_filename, $width = 200, $height = 150, $quality = 70, $crop = true) {

	if( ! $image_data = getimagesize( $source_image ) )
	{
			return false;
	}

	switch( $image_data['mime'] )
	{
		case 'image/gif':
				$get_func = 'imagecreatefromgif';
				$suffix = ".gif";
		break;
		case 'image/jpeg';
				$get_func = 'imagecreatefromjpeg';
				$suffix = ".jpg";
		break;
		case 'image/png':
				$get_func = 'imagecreatefrompng';
				$suffix = ".png";
		break;
	}

	$img_original = call_user_func( $get_func, $source_image );
	$old_width = $image_data[0];
	$old_height = $image_data[1];
	$new_width = $width;
	$new_height = $height;
	$src_x = 0;
	$src_y = 0;
	$current_ratio = round( $old_width / $old_height, 2 );
	$desired_ratio_after = round( $width / $height, 2 );
	$desired_ratio_before = round( $height / $width, 2 );

	if( $crop )
	{
		/**
		 * creaza o imagine goala la dimensiunea dorita
		 */
		$new_image = imagecreatetruecolor( $width, $height );

		/**
		 * landscape
		 */
		if( $current_ratio > $desired_ratio_after )
		{
				$new_width = $old_width * $height / $old_height;
		}

		/**
		 * patrat
		 */
		if( $current_ratio > $desired_ratio_before && $current_ratio < $desired_ratio_after )
		{
			if( $old_width > $old_height )
			{
					$new_height = max( $width, $height );
					$new_width = $old_width * $new_height / $old_height;
			}
			else
			{
					$new_height = $old_height * $width / $old_width;
			}
		}

		/**
		 * portret
		 */
		if( $current_ratio < $desired_ratio_before  )
		{
				$new_height = $old_height * $width / $old_width;
		}

		$width_ratio = $old_width / $new_width;
		$height_ratio = $old_height / $new_height;

		/**
		 * calculez unde sa tai imaginea (bazat pe centru)
		 */
		$src_x = floor( ( ( $new_width - $width ) / 2 ) * $width_ratio );
		$src_y = round( ( ( $new_height - $height ) / 2 ) * $height_ratio );
	}
	/**
	 * nu tai imaginea, doar o redimensionez pastrand proportiile
	 */
	else
	{
		if( $old_width > $old_height )
		{
				$ratio = max( $old_width, $old_height ) / max( $width, $height );
		}else{
				$ratio = max( $old_width, $old_height ) / min( $width, $height );
		}

		$new_width = $old_width / $ratio;
		$new_height = $old_height / $ratio;

		$new_image = imagecreatetruecolor( $new_width, $new_height );
	}

	imagecopyresampled( $new_image, $img_original, 0, 0, $src_x, $src_y, $new_width, $new_height, $old_width, $old_height );

	imagejpeg( $new_image, $destination_filename, $quality  );

	imagedestroy( $new_image );
	imagedestroy( $img_original );

	/**
	 * daca totul este corect
	 */
	return true;
}
?>

Includeți fișierul image.php în codul paginii:

<?php
	include 'image.php';
	resize_image('imagine-originala.jpg', 'imagine-modificata.jpg', 200, 150, 70, $crop = true);
?>