Skip to content

Instantly share code, notes, and snippets.

@steveosoule
Last active March 2, 2018 01:38
Show Gist options
  • Save steveosoule/9e43f1490e356aac2f4a to your computer and use it in GitHub Desktop.
Save steveosoule/9e43f1490e356aac2f4a to your computer and use it in GitHub Desktop.
mvThumb - Automatic Image Resizer
<?php
/*
# Dynamic Image Resize .htaccess
RewriteCond %{REQUEST_URI} /resize/(\d+)x(\d+)/(.*)
RewriteRule ^(.*)$ http://www.example.com/resize/index.php?width=%1&height=%2&file=%3 [L]
*/
// Example Usage: http://www.example.com/resize/300x300/graphics/00000001/path-to-product-image.jpg
// Declare Variables
$missing_path = '../mm5/themes/luxe/images/img_coming_soon.png';
$source_file_name = file_exists('../'.$_GET['file']) ? '../'.$_GET['file'] : $missing_path;
$source_file_modified = (file_exists($source_file_name)) ? filemtime($source_file_name) : 0;
$source_ext = strtolower(array_pop(explode('.',$source_file_name)));
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime_type = $finfo->file($source_file_name);
$allowed_mime_types = array('image/png', 'image/jpeg', 'image/gif');
$is_allowed = array_search($mime_type, $allowed_mime_types);
if( !$is_allowed ){
$source_ext = 'png';
$mime_type = 'image/png';
$source_file_name = $missing_path;
$source_file_modified = filemtime($source_file_name);
}
// Set a maximum height and width
$width = ( isset($_GET['width']) && $_GET['width'] < 2000 ) ? $_GET['width'] : 100;
$height = ( isset($_GET['height']) && $_GET['width'] < 2000 ) ? $_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);
}
// 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);
$image_p = imagecreatetruecolor($width, $height);
// handle the alpha transparency stuff
imagealphablending($image_p, false);
imagesavealpha($image_p, true);
$transparent = imagecolorallocatealpha($image_p, 255, 255, 255, 127);
imagefilledrectangle($image_p, 0, 0, $width_orig, $height_orig, $transparent);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagepng($image_p, $new_file_name, 6);
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 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment