gandalf458
IS-IT--Management
I have a script that uploads and if necessary resizes an image. It works fine except that if the uploaded image is not the same aspect ratio as the source image it gets distorted. Can anyone help me work out what I need to add to change the aspect ratio please?
I'm not a number, I'm a free man
PHP:
$image = 'img'.date('U').'.'.$extn;
$destfile = '../video-img/'.$image;
$tempfile = '../temp/'.$image;
$tempname = $_FILES['image']['tmp_name'];
$size = getimagesize($tempname);
$width = 800;
$height = 450;
if ( ($size[0] < $width) || ($size[1] < $height) )
echo '<p class="error">Image ', $filename, ' is too small but will be resized and uploaded.</p>', "\n";
// if image is the right size, just save it
if ( ($size[0] === $width) || ($size[1] === $height) ) {
if ( !(is_uploaded_file($tempname) && move_uploaded_file($tempname, $destfile)) )
echo '<p class="error">Image ', $filename, ' could not be uploaded.</p>', "\n";
}
// if image is not the right size, save it to a temporary are and then resize it
else {
if ( !(is_uploaded_file($tempname) && move_uploaded_file($tempname, $tempfile)) )
echo '<p class="error">Image ', $filename, ' could not be uploaded.</p>', "\n";
$resized = imagecreatetruecolor($width, $height);
if ( $extn == 'jpg' or $extn == 'jpeg' ) {
$srcfile = imagecreatefromjpeg($tempfile);
imagecopyresampled($resized, $srcfile, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagejpeg($resized, $destfile, 80);
}
elseif ( $extn == 'png' ) {
$srcfile = imagecreatefrompng($tempfile);
imagecopyresampled($resized, $srcfile, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagepng($resized, $destfile, 6);
}
imagedestroy($resized);
imagedestroy($srcfile);
unlink($tempfile);
}
I'm not a number, I'm a free man