$name) { if($name != '.' && $name != '..') { $source_file = $basefiles . $name; $dest_file = $dest_rep . 'painting_romanticism_' . $count . '.png'; // Change 'painting_romanticism_' for the prefix you want if(substr($name,-4) == '.png') { $img = @imagecreatefrompng($source_file); } elseif(substr($name,-4) == '.jpg' || substr($name,-5) == '.jpeg') { $img = imagecreatefromjpeg($source_file); } elseif(substr($name,-5) == '.webp') { $img = imagecreatefromwebp($source_file); } elseif(substr($name,-5) == '.avif') { $img = imagecreatefromavif($source_file); } $original_x = imagesx($img); $original_y = imagesy($img); // Optimal ratio $ratio = ($original_x / $original_y) > 1 ? 169 : 916; $new_x = floor($original_x / 8) * 8; $new_y = floor($original_y / 8) * 8; if($ratio == 169) { $true_ratio = $original_x / $original_y; if($original_x > $max_size) { $ratio_crop = $original_x / $max_size; $new_x = $max_size; $new_y = round($original_y / $ratio_crop); $new_y = floor($new_y / 8) * 8; } } else { $true_ratio = $original_y / $original_x; if($original_y > $max_size) { $ratio_crop = $original_y / $max_size; $new_y = $max_size; $new_x = round($original_x / $ratio_crop); $new_x = floor($new_x / 8) * 8; } } if($new_x > 300 && $new_y > 300) { $dst_file = imagecreatetruecolor($new_x, $new_y); if(substr($name,-4) == '.png' || substr($name,-5) == '.webp') { imagecolortransparent($dst_file, imagecolorallocatealpha($dst_file, 0, 0, 0, 127)); imagealphablending($dst_file, false); imagesavealpha($dst_file, true); } imagecopyresampled($dst_file, $img, 0, 0, 0, 0, $new_x, $new_y, $original_x, $original_y); imagepng($dst_file, $dest_file); } } } ?>