The imagecopyresampled function is useful for creating thumbnails or resizing images while maintaining aspect ratio:
$fn = $_FILES['data']['tmp_name']; $size = getimagesize($fn); $width = $size[0]; $height = $size[1]; $ratio = $width / $height; if ($ratio > 1 && $size[0] > 500) { $width = 500; $height = 500 / $ratio; } else { if ($ratio <= 1 && $size[1] > 500) { $width = 500 * $ratio; $height = 500; }} $src = imagecreatefromstring(file_get_contents($fn)); $dst = imagecreatetruecolor($width, $height); imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); imagedestroy($src); imagejpeg($dst, 'test.jpg'); imagedestroy($dst);
How to choose the resizing algorithm used by PHP?
Note: as indicated in this question , setting imagesetinterpolation($dst, IMG_BILINEAR_FIXED); or such things do not work.
According to the tests I did (in another language), "bilinear resizing" sometimes gives a better result than bicubic, and sometimes vice versa (it depends on its decrease or increase).

php image-processing image-resizing
Basj
source share