steveosoule
7/16/2014 - 11:56 PM

PHP - Dynamic Image Stack & Resize

PHP - Dynamic Image Stack & Resize

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

	// Example Usage: http://www.example.com/mm5/stack/300x350/top/graphics/00000001/sample-top-image.jpg/bottom/graphics/00000001/sample-bottom-image.jpg
	// TODO: Add support for gif & png files

	//
	//
	// Reisze and merge WITH overlap
	//
	//
	$top = get_image_details($_GET['top'], 'top');
	$bottom = get_image_details($_GET['bottom'], 'bottom');

	$final['width'] = ( isset($_GET['width'] ) && $_GET['width'] < 1000 ) ? $_GET['width'] : 100;
	$final['height'] = ( isset($_GET['height'] ) && $_GET['width'] < 1000 ) ? $_GET['height'] : 100;
	$final['path'] = preg_replace('/(.*)(\.[a-z]{3,4})$/', '$1_X_'.$bottom['base_name'].'_'.$final['width'].'x'.$final['height'].'$2', $top['path']);
	$final = array_merge($final, get_image_details($final['path'], 'final') );

	// create merged image
	if( !$final['exists'] || $top['modified'] > $final['modified'] || $bottom['modified'] > $final['modified'] ){
		$final['shell'] = imagecreatetruecolor($final['width'], $final['height']);
		$final['color'] = imagecolorallocate($final['shell'], 255, 255, 255);
		imagefill($final['shell'], 0, 0, $final['color']);

		$top['shell'] = imagecreatetruecolor($top['width'], $top['height']);
		$top['image'] = imagecreatefromjpeg($top['path']);
		imagecopyresampled($top['shell'], $top['image'], 0, 0, 0, 0, $top['width'], $top['height'], $top['width_orig'], $top['height_orig']);

		$bottom['shell'] = imagecreatetruecolor($bottom['width'], $bottom['height']);
		$bottom['image'] = imagecreatefromjpeg($bottom['path']);
		imagecopyresampled($bottom['shell'], $bottom['image'], 0, 0, 0, 0, $bottom['width'], $bottom['height'], $bottom['width_orig'], $bottom['height_orig']);

		imagecopy($final['shell'], $top['shell'], ($final['width']-$top['width'])/2, 0, 0, 0, $top['width'], $top['height']);
		imagecopy($final['shell'], $bottom['shell'], ($final['width']-$bottom['width'])/2, $top['height'], 0, 0, $bottom['width'], $bottom['height']);

		imagejpeg($final['shell'], $final['path'], 80);

		imagedestroy( $final['shell'] );
		imagedestroy( $top['shell'] );
		imagedestroy( $bottom['shell'] );
	}

	// Output Image
	header('Content-Type: '.$final['mime_type']);
	echo file_get_contents( $final['path'] );

	function get_mime_type($ext){
		$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($ext, $mime_types)) {
			$mime_type = $mime_types[$ext];
		}
		return $mime_type;
	}

	function get_image_details($path, $type = NULL){
		$image = array();
		if( $type === 'final' ){
			$image['path'] = $path;
		} else {
			$image['path'] = (file_exists('../mm5/'.$path)) ? '../mm5/'.$path : '../mm5/graphics/00000001/missing-image-75x75.gif';
		}
		$image['exists'] = file_exists($image['path']);
		$image['modified'] = ($image['exists']) ? filemtime($image['path']) : 0;
		$image['ext'] = strtolower(array_pop(explode('.',$image['path'])));
		$image['mime_type'] = get_mime_type($image['ext']);
		preg_match_all('/(.*)(\.[a-z]{3,4})$/', basename($image['path']), $matches);
		$image['base_name'] = $matches[1][0];
		if( $type !== 'final' ){
			// Dimensions
			$image['width'] = (( isset($_GET['width']) && $_GET['width'] < 1000 ) ? $_GET['width'] : 100) / 2;
			$image['height'] = (( isset($_GET['height']) && $_GET['height'] < 1000 ) ? $_GET['height'] : 100) / 2;
			// Make the top image slightly smaller than the bottom image.
			if( $type === 'top' ){
				$image['height'] = $image['height']*.9;
			} elseif( $type === 'bottom' ){
				$image['height'] = $image['height']*1.1;
			}
			list($width_orig, $height_orig) = getimagesize($image['path']);
			$image['width_orig'] = $width_orig;
			$image['height_orig'] = $height_orig;
			$ratio_orig = $width_orig/$height_orig;
			if ($image['width']/$image['height'] > $ratio_orig) {
				$image['width'] = intval($image['height']*$ratio_orig);
			} else {
				$image['height'] = intval($image['width']/$ratio_orig);
			}
		}
		return $image;
	}
?>