PHP interesse |
|
k zal je een stukje geven dat k laatst heb gemaakt (quick&dirty) maar behoud wel de breedte/hoogte ratio
<?php
$uploaddir = '/var/www/images/projects/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
$max = 100;
$file = pathinfo($uploadfile);
$imagetype = exif_imagetype($uploadfile);
switch ($imagetype) {
case IMAGETYPE_GIF:
$img = imagecreatefromgif($uploadfile);
break;
case IMAGETYPE_JPEG:
$img = imagecreatefromjpeg($uploadfile);
break;
case IMAGETYPE_PNG:
$img = imagecreatefrompng($uploadfile);
break;
default:
$img = NULL;
break;
}
unset($nimg);
if (imagesx($img) > $max || imagesy($img) > $max) {
if(imagesx($img) < imagesy($img)) {
$new_height = $max;
$new_width = $max / imagesy($img) * imagesx($img);
} elseif (imagesx($img) > imagesy($img)) {
$new_width = $max;
$new_height = $max / imagesx($img) * imagesy($img);
} elseif (imagesx($img) === imagesy($img)) {
$new_height = $max;
$new_width = $max;
}
$nimg = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($nimg, $img, 0, 0, 0, 0, $new_width, $new_height, imagesx($img), imagesy($img));
}
if(isset($nimg)) {
imagepng($nimg,$uploaddir.$filenamethumb);
} else {
imagepng($img, $uploaddir.$filenamethumb);
}
<?php $uploaddir = '/var/www/images/projects/'; $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); $max = 100; switch ($imagetype) { case IMAGETYPE_GIF: $img = imagecreatefromgif($uploadfile); break; case IMAGETYPE_JPEG: $img = imagecreatefromjpeg($uploadfile); break; case IMAGETYPE_PNG: $img = imagecreatefrompng($uploadfile); break; default: $img = NULL; break; } if (imagesx($img) > $max || imagesy($img) > $max) { if(imagesx($img) < imagesy($img)) { $new_height = $max; $new_width = $max / imagesy($img) * imagesx($img); } elseif (imagesx($img) > imagesy($img)) { $new_width = $max; $new_height = $max / imagesx($img) * imagesy($img); } elseif (imagesx($img) === imagesy($img)) { $new_height = $max; $new_width = $max; } $nimg = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($nimg, $img, 0, 0, 0, 0, $new_width, $new_height, imagesx($img), imagesy($img)); } imagepng($nimg,$uploaddir.$filenamethumb); } else { imagepng($img, $uploaddir.$filenamethumb); }
|