Функция готова:
php
function scale_image_down_jpg (string $src_img_path, string $dst_img_path, int $longest_image_side) {
/* $src_img_path // Source image file path
* $dst_img_path // Destination image file path
* $longest_image_side // Pixels
*
*/
$src_img = imagecreatefromjpeg ($src_img_path);
$src_img_w = imagesx ($src_img); // Width
$src_img_h = imagesy ($src_img); // Height
//print "Source: ".$src_img_w." x ".$src_img_h."\n"; //Debug
// Preset dimensions - is this really needed?
$dst_img_w = 0;
$dst_img_h = 0;
if ($src_img_w >= $src_img_h) { // Horizontal proportions
if ($src_img_w > $longest_image_side) { // Image resizing required
$dst_img_w = $longest_image_side;
$ratio = $src_img_w / $dst_img_w;
$dst_img_h = floor ($src_img_h / $ratio);
//print "Destination: ".$dst_img_w." x ".$dst_img_h."\n"; //Debug
} else {
$dst_img_w = $src_img_w;
$dst_img_h = $src_img_h;
//print "Destination: ".$dst_img_w." x ".$dst_img_h."\n"; //Debug
}
} else { // Vertical proportions
if ($src_img_h > $longest_image_side) { // Image resizing required
$dst_img_h = $longest_image_side;
$ratio = $src_img_h / $dst_img_h;
$dst_img_w = floor ($src_img_w / $ratio);
//print "Destination: ".$dst_img_w." x ".$dst_img_h."\n"; //Debug
} else {
$dst_img_w = $src_img_w;
$dst_img_h = $src_img_h;
//print "Destination: ".$dst_img_w." x ".$dst_img_h."\n"; //Debug
}
}
$dst_img = imagecreatetruecolor ($dst_img_w, $dst_img_h);
if (imagecopyresampled ($dst_img, $src_img, 0, 0, 0, 0, $dst_img_w, $dst_img_h, $src_img_w, $src_img_h)) {
if (imagejpeg ($dst_img, $dst_img_path, 100)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
Вызываем функцию в приложении.