crop image in php with htaccess code example

Example 1: image crop in htaccess

// Ratio cropping
	$offsetX	= 0;
	$offsetY	= 0;

	if (isset($_GET['cropratio']))
	{
		$cropRatio = explode(':', (string) $_GET['cropratio']);
		if (count($cropRatio) == 2)
		{
			$ratioComputed = $width / $height;
			$cropRatioComputed = (float) $cropRatio[0] / (float) $cropRatio[1];

			if ($ratioComputed < $cropRatioComputed)
			{ // Image is too tall so we will crop the top and bottom
				$origHeight = $height;
				$height = $width / $cropRatioComputed;
				$offsetY = ($origHeight - $height) / 2;
			}
			else if ($ratioComputed > $cropRatioComputed)
			{ // Image is too wide so we will crop off the left and right sides
				$origWidth = $width;
				$width = $height * $cropRatioComputed;
				$offsetX = ($origWidth - $width) / 2;
			}
		}
	}

Example 2: image crop in htaccess

# BEGIN ImageResizing
<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# scaling small, medium, large
RewriteRule ^img-small/([A-Za-z0-9/_-]+).(jpg|gif|png)$ images.php?max_width=100&imgfile=$1.$2
RewriteRule ^img-medium/([A-Za-z0-9/_-]+).(jpg|gif|png)$ images.php?max_width=230&imgfile=$1.$2
RewriteRule ^img-large/([A-Za-z0-9/_-]+).(jpg|gif|png)$ images.php?max_width=470&imgfile=$1.$2
</ifmodule>
# END ImageResizing

Tags:

Misc Example