steveosoule
7/27/2015 - 10:57 PM

mvThumb - Automatic Image Resizer

mvThumb - Automatic Image Resizer

<?php
	/*
		# Dynamic Image Resize .htaccess
		RewriteCond %{REQUEST_URI} /mm5/resize/(\d+)x(\d+)/(.*)
		RewriteRule ^(.*)$ http://www.example.com/mvthumb/resize.php?width=%1&height=%2&file=%3 [L]
	 */

	// Example Usage: http://www.example.com/mm5/resize/300x350/graphics/00000001/msr-2015-axxis-pant-ywbkbl.jpg

	// Declare Variables
	$source_file_name = (file_exists('../mm5/'.$_GET['file'])) ? '../mm5/'.$_GET['file'] : '../mm5/graphics/00000001/missing-image-75x75.gif';
	$source_file_modified = (file_exists($source_file_name)) ? filemtime($source_file_name) : 0;
	$source_ext = strtolower(array_pop(explode('.',$source_file_name)));
	$mime_types = array('png' => 'image/png','jpe' => 'image/jpeg','jpeg' => 'image/jpeg','jpg' => 'image/jpeg','gif' => 'image/gif');
	$mime_type = FALSE;
	if (array_key_exists($source_ext, $mime_types)) {
		$mime_type = $mime_types[$source_ext];
	}

	// Set a maximum height and width
	// round($_GET['width'], -1); // Round to nearest ten
	$width = ( isset($_GET['width']) && $_GET['width'] < 1000 ) ? $_GET['width'] : 100;
	$height = ( isset($_GET['height']) && $_GET['width'] < 1000 ) ? $_GET['height'] : 100;

	// New dimensions
	list($width_orig, $height_orig) = getimagesize($source_file_name);
	$ratio_orig = $width_orig/$height_orig;
	if ($width/$height > $ratio_orig) {
		$width = intval($height*$ratio_orig);
	} else {
		$height = intval($width/$ratio_orig);
	}

	/* if(	$width_orig < $width || $height_orig < $height ){
		// Don't resize if desired width is greater than source width. Just output source image
		header('Content-Type: '.$mime_type);
		echo file_get_contents( $source_file_name );
		exit();
	}*/

	// New File Name
	$pattern = '/(.*)(\.[a-z]{3,4})$/';
	$replacement = '$1_'.$width.'x'.$height.'$2';
	$new_file_name = preg_replace($pattern, $replacement, $source_file_name);
	$new_file_modified = (file_exists($new_file_name)) ? filemtime($new_file_name) : 0;

	// Resize/Resample
	if( !file_exists($new_file_name) || $source_file_modified > $new_file_modified ){
		$image_p = imagecreatetruecolor($width, $height);
		switch($mime_type){
			case 'image/png':
				$image = imagecreatefrompng($source_file_name);
				imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
				imagepng($image_p, $new_file_name, 80);
				break;
			case 'image/gif':
				$image = imagecreatefromgif($source_file_name);
				imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
				imagegif($image_p, $new_file_name, 80);
				break;
			case 'image/jpeg':
				$image = imagecreatefromjpeg($source_file_name);
				imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
				imagejpeg($image_p, $new_file_name, 80);
				break;
		}
		imagedestroy($image_p);
	}

	// Output Image
	header('Content-Type: '.$mime_type);
	echo file_get_contents( $new_file_name );
?>