<?php
$resOriginalImage='600_x_200.jpg';
$image_details = getimagesize($resOriginalImage);
if ($image_details === false){
echo 'Not a valid image supplied, or this script does not have permissions to access it.';
}else{
$Original_W = $image_details[0];
$Original_H = $image_details[1];
$type = $image_details[2];
$mime = $image_details['mime'];
}
switch ($type){
case 1:
// GIF
$source = imagecreatefromgif($resOriginalImage);
break;
case 2:
// JPG
$source = imagecreatefromjpeg($resOriginalImage);
break;
case 3:
// PNG
$source = imagecreatefrompng($resOriginalImage);
break;
default:
echo 'Unsupported image file format.';
}
$reSizeW = 300;
$reSizeH = 75;
$aspect_ratio = $reSizeW/$reSizeH;
$original_aspect_ratio = $Original_W / $Original_H;
$src_x = $src_y = 0;
$newW = $reSizeW;
$newH = $reSizeH;
if($aspect_ratio > 1){
//New image is wide
$src_x = 0;
$src_y = round(( $Original_H / 2 ) - (($Original_W/$aspect_ratio) / 2));
$src_w = $Original_W;
$src_h = $Original_W/$aspect_ratio;
}else{
//New image is tall
$src_x = round(( $Original_W / 2 ) - (($Original_H * $aspect_ratio)/2));
$src_y = 0;
$src_w = $Original_H * $aspect_ratio ;
$src_h = $Original_H;
}
$centerFitImage = imagecreatetruecolor($newW, $newH);
imagecopyresampled($centerFitImage, $source, 0, 0, $src_x, $src_y, $newW, $newH, $src_w, $src_h);
//Save Center Fix Image.
imagejpeg($centerFitImage, "new_center.jpg");
?>
|