Forum Moderators: coopster
240x320 then mod it to a resolution of 640x240
<?php
$original_image = '/path/to/the/original/image/image.jpg';
$req_width = 640;
$req_height = 240;
$ext = '.jpg';
$img = NULL;
switch($ext) {
case '.jpg' :
case '.jpeg' :
$img = imagecreatefromjpeg($new);
break;
case '.png' :
$img = imagecreatefrompng($new);
break;
case '.gif' :
$img = imagecreatefromgif($new);
break;
default:
break;
}
if ($img) { // Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(($req_width/$width),($req_height/$height), 1 );
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
$tmp_img = imagecreatetruecolor($req_width, $req_height); // Create a new temporary image
$white = imagecolorallocate($tmp_img, 255, 255, 255);
imagefill ($tmp_img, 0, 0, $white);
$posx = round(($req_width-$new_width)/2);
$posy = round(($req_height-$new_height)/2);
imagecopyresampled($tmp_img, $img, $posx, $posy, 0, 0, $new_width, $new_height, $width, $height); // Copy and resize old image into new image
imagedestroy($img);
ob_start(); // remove this line if you specify a filename in imagejpeg() next line
imagejpeg($tmp_img); /// to save it straight to disk change to imagejpeg($tmp_img, 'new_image_path/and_filename.jpg');
$imageData = ob_get_clean(); //// this is the final image, save it or display it, remove this line if you specified a file name in imagejpeg() previous line
imagedestroy($tmp_img);
}
?>